Advertisement

specifying texture coordinate (glTexcoord2f) to cover quad

Started by February 13, 2004 04:32 PM
2 comments, last by cheeseh 21 years ago
alright, well since my fonts failed, I''ve decided to make up my own fonts class which reads in a bitmap that has fonts layed out in a grid. Now all I want to do is print a quad for each character in a string, that''s easy. But getting the texture right for the character is a nightmare. I''ve searched the depth of this forum and checked documentation on glTexCoord2f, but I still get confused. The problem is this, for each character I must prtint out a quad which has only the letter in the bitmap on its quad face. This means getting texture co-ordinates for top, left, right, bottom of this letter and using glTexCoord2f to sort out what part of the fonts bitmap I want on this quad, correct? Done that, but whenever the quad is displayed, the WHOLE font bitmap (not just the letter which I want) is wrapped across the quad THOUSANDS of times! wtf!!? So this means it looks realy corrupt and if you zoom really far into the quad you can see the whole font bitmap turning into shape. Here is my code:

	void Render ( Vector vPoint, Vector vRight, Vector vUp, int iTexStartX, int iTexStartY )
	{ 
		float fSize = ((float)m_wantedsize/2);	// size from middle
		Vector vNewPoint;	

		glPushMatrix();
			// move to origin (?)
			glTranslatef(vPoint.x,vPoint.y,vPoint.z);
			// use font texture
			glBindTexture(GL_TEXTURE_2D, m_texture[0]);
			// use white colour
			glColor3f(1.0f,1.0f,1.0f);

			// texture points
			float fTexTop		= (float)(iTexStartY+m_fontheight);
			float fTexBottom	= (float)(iTexStartY);
			float fTexLeft		= (float)(iTexStartX);
			float fTexRight		= (float)(iTexStartX+m_fontwidth);

			glBegin(GL_QUADS);	// Build Quad
		
					vNewPoint = vPoint + ((-(vRight + vUp)).Normalize())*fSize;
					glTexCoord2f(fTexLeft,fTexBottom);
					glVertex3fv((float*)vNewPoint); // Bottom Left

					vNewPoint = vPoint + ((vRight - vUp).Normalize())*fSize;
					glTexCoord2f(fTexRight,fTexBottom);
					glVertex3fv((float*)vNewPoint); // Bottom Right

					vNewPoint = vPoint + ((vRight + vUp).Normalize())*fSize;
					glTexCoord2f(fTexRight,fTexTop);
					glVertex3fv((float*)vNewPoint); // Top Right

					vNewPoint = vPoint + ((vUp - vRight).Normalize())*fSize;
					glTexCoord2f(fTexLeft,fTexTop);
					glVertex3fv((float*)vNewPoint); // Top Left
		


			glEnd();
		// Done
		glPopMatrix();	
	}
 
and this is how I load my texture...

	void createFonts ()
	{
		found_texture = false;						// Status Indicator

		AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Textures
		memset(TextureImage,0,sizeof(void *)*1);			// Set The Pointer To NULL
		
		char bmp_filename[MAX_PATH];
		
		sprintf(bmp_filename,"%s\\Data/fonts.bmp",winamp_folder);
		
		if ( (TextureImage[0]=LoadBMP(bmp_filename)) != 0 ) // Load Particle Texture
		{
			found_texture=true;								// Set The Status To TRUE
			glGenTextures(1, &m_texture[0]);				// Create One Texture
			
			glBindTexture(GL_TEXTURE_2D, m_texture[0]);
			// take WRAPPING out!
			//glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
			//glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
			glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			m_texwidth = TextureImage[0]->sizeX;
			m_texheight = TextureImage[0]->sizeY;
			glTexImage2D(GL_TEXTURE_2D, 0, 3, m_texwidth, m_texheight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, TextureImage[0]->data);

			m_fontwidth = m_texwidth/16;
			m_fontheight = m_texheight/16;
		}

		if (TextureImage[0])								// If Texture Exists
		{
			if (TextureImage[0]->data)						// If Texture Image Exists
			{
				free(TextureImage[0]->data);				// Free The Texture Image Memory
			}
			free(TextureImage[0]);							// Free The Image Structure
		}
	}
 
Is there anything at all possibly wrong with that? I think glTexCoord2f is doing something strange, is it only values from 0 to 1 that can go into glTexCoord2f as parameters?? (by reading aeverything no-one has ever mentioned this) but by reading gamedev''s opengl Game programming book I would have assumed so, I think maybe the value should be more than 1 if I want to shift the texture points, which is what I really want do do!!! Thanks, and remember this has been wrecking my brain
[rcbot] http://rcbot.bots-united.com
Oops, sorry, Yes it must be a value between 0 and 1, I forgot to divide the found texture coords by the actual texture size to get the proportional size!! (now I am only getting one letter woohoo. but not the right ones, lol, something wrong in finding the texture coordinates then, no worries I figure it out)
[rcbot] http://rcbot.bots-united.com
Advertisement
You do know that NeHe has an excellent tutorial on this very subject? Here it is.

When using glTexCoord2f you can think of the number you specify like a percentage (it''s not really, but it makes it easier to visualise), where a value of 1 will use the full dimension of the texture, a value of 0.5 will use half and a value of 2 will repeat the texture twice across the dimension (providing you use glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT) ).

Try The Red Book for clarifiation.



--
Cheers,
--
Cheers,
Darren Clark
yeah, didn''t know about nehe''s tutorial, I took most of the stuff from the gamedev''s book and some ideas from the source in the demos that come with the books CD But anyway I sorted out the fonts, I forgot to invert the Y-axis for the tex coord on the bitmap. Another problem is how to find the best co-ordinate to place the start of the fonts so that I can see the string well and fits on the screen, well that''s another issue
[rcbot] http://rcbot.bots-united.com

This topic is closed to new replies.

Advertisement