Jump to content
  • Advertisement
Sign in to follow this  
Fahrenheit451

OpenGL Upside down texture font in orthographic

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I admit to not doing a thorough search, but what I have done only explains OpenGL screen coordinates in orthographic mode as having 0,0 in the lower left, even though 0,0 places my text top-left corner. This problem seems different. My texture fonts all get displayed fandiddlytastic in perspective mode, but when I switch to orthograpic mode to display the hud text, they're upside down! I actually changed the texture font creation code to render quad so the 3D text was not upside down, but now my 2D text went from being correct to being upside down :-( I can't figure it out. screenie: see top left (bit small, sorry) Code to build the textures hasn't changed dramatically, but as I say, I did change the quad render to fix the 3D text being upside down. Do you guys see anything obvious? Even if this has to do with texture coordinate calculations, I would expect to see the same issues in both perspective and orthographic modes. Anyway...
// Build a font of type and size specified
bool FTXFont::Build()
{
	// build texture if not already built
	if (!fontBuilt)
	{
		// check if file exists

		// load image and create texture and get it's OpenGL ID
		fontTexture = new FTexture(texFilename);
		//if (!fontTexture->GetIsLoaded() || !fontTexture->GetIsCreated())
		if (!fontTexture->GetIsCreated())
		{
			PrintDebug("[FTXFont] Texture not created for font %s. Exiting.\n",texFilename);
			return false;
		}
		// get texture specification
		textureSpec = fontTexture->GetTextureSpec();

		// if not provided, assume image is power of 2 square
		// and get number of characters horizontally and vertically
		// by taking the square root of the image dimentions
		if (numCharsV == 0)
		{
			numCharsV = (unsigned int)sqrt((float)image->GetWidth());
		}
		if (numCharsU == 0)
		{
			numCharsU = (unsigned int)sqrt((float)image->GetHeight());
		}

		// generate a series display list for characters of texture
		unsigned int totalChars = numCharsV*numCharsU;
		PrintDebug("[FTXFont] Font %s is %d chars wide x %d chars high\n",texFilename,numCharsV,numCharsU);
		PrintDebug("[FTXFont] Generating %d OpenGL display lists\n",totalChars);
		callList = glGenLists(totalChars);

		// bind our texture
		fontTexture->Bind();

		// traverse the image and generate the texture coordinates for each character and
		// build a quad and assign those coordinates for padded textures the texture
		// coordinates will be offset slightly hence the real (non-power of two) image width
		// is divided by the padded width (power of two)

		// get our increment based on how many characters we are seeing horizontally and vertically
		float incV = textureSpec.vRatio / (float)numCharsV;
		float incU = textureSpec.uRatio / (float)numCharsU;
		PrintDebug("[FTXFont] Font Specification:\n");
		PrintDebug("[FTXFont]   slots = %d   base = %d  range = %d ~ %d\n",totalChars,callList,callList,callList+totalChars-1);
		PrintDebug("[FTXFont]   incU  : (%d/%d) = %1.5f / %d chars high = %1.5f\n",image->GetOrigHeight(),image->GetHeight(),textureSpec.uRatio,numCharsU,incU);
		PrintDebug("[FTXFont]   incV  : (%d/%d) = %1.5f / %d chars wide = %1.5f\n",image->GetOrigWidth(),image->GetWidth(),textureSpec.vRatio,numCharsV,incV);

		// traverse texture calculating texture coordinates for each char
		// and build a quad in a display list for that character
		float v1,v2,u1,u2 = 0.0f;
		for (unsigned int u=0; u < numCharsU; u++)
		{
			for (unsigned int v=0; v < numCharsV; v++)
			{
				// grab our fraction of the texture relevant to the character and list
				//   cx works from low to high, whilst cy works down from high to low
				u1 = textureSpec.lULimit + (incU * (float)u);
				u2 = u1 + incU;
				v1 = textureSpec.lVLimit + (incV * (float)v);
				v2 = v1 + incV;

				// create the list for this texture portion (character)
				glNewList(callList+((u*numCharsV)+v), GL_COMPILE);

				// define our quad for the character
				glBegin(GL_QUADS);
					// bottom left
					glTexCoord2f(v1,u2);
						glVertex2f(0.0f,0.0f);
					// bottom right
					glTexCoord2f(v2,u2);
						glVertex2f(size,0.0f);
					// top right
					glTexCoord2f(v2,u1);
						glVertex2f(size,size);
					// top left
					glTexCoord2f(v1,u1);
						glVertex2f(0.0f,size);
				glEnd();

				// include spacing as part of character display
				// otherwise all characters appear ontop of one another
				glTranslated(spacing,0,0);

				// end our list
				glEndList();
			}
		}

		// indicate font built and return status
		fontBuilt = true;
		PrintDebug("[FTXFont] Built font for texture ID : %d\n",this->GetTexID());
	}

	// all ok
	return fontBuilt;
}

// Print function
void FTXFont::Print(const char *str, ...)
{

	// initialise variables
	va_list args;

	// check for empty string
	if (str == NULL)
		return;

	// format our string
	va_start(args, str);
		vsprintf_s(text, sizeof(text), str, args);
	va_end(args);

	// setup OpenGL environment for printing
//	EnableStates();

	// setup our alignments of entire text string for 3D
	if (!display2D)
	{
		hTOffset = 0.0f;	vTOffset = 0.0f;
		// if not default right align then assume left and set offset to size of text
		if (hTAlign != HRIGHT)
		{
			// offset left is length of current text multiplied by the size of the text
			hTOffset = (float)GetTextLength() * size;
			// if actually it's center and not left, then halve the offset to give us center align
			if (hTAlign == HCENTER)
				hTOffset *= 0.5f;
		}
		// if not default bottom align, then assume top and move up size of text
		if (vTAlign != VBOTTOM)
		{
			vTOffset = size;
			// if actually it's center then halve the offset to give us center align
			if (vTAlign == HCENTER)
				vTOffset *= 0.5f;
		}
	}

	// set desired color
	glColor4f(r, g, b, a);

	// print the text with any alignments
	glPushMatrix();

		// set the list base
		glListBase(callList-32);

		// move to desired location and call lists
		if (display2D)
		{
			// move to specified screen coordinates
			glTranslated((double)screenX,(double)screenY,0.0);
		} else {
			// move to 3D space coordinates (!= to 2D screen coordinates!)
			glTranslatef(xpos,ypos,zpos);

			// apply alignment offsets. use negative on the horizontal
			// because default alignment is right so we need to move left
			glTranslatef(-hTOffset,vTOffset,0.0f);
		}

		// scale text accordingly. Rendering in 3D space does not use screen pixels
		// as a unit of measurement, so we need to scale the text before displaying
		if (display2D)
		{
			//glScalef(1.0f,1.0f,1.0f);
		} else {
			//glScalef(size/1000.0f,size/1000.f,size/1000.0f);
			//glScalef(size,size,size);
		}

		// display the text
		glCallLists((GLsizei)strlen(text),GL_BYTE,text);

	glPopMatrix();

	// disable OpenGL states
//	DisableStates();
}







Cheers, and thanks for any hints. F451 [Edited by - Fahrenheit451 on January 26, 2006 11:07:31 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hints are coming up for you: Don't use ortho mode to draw, keep your texts floating around in a 3D environment.

There're basically three types of fonts: font texture, font bitmap and font outline. Except font bitmap doesn't have the ability to scale and rotate, the two remaining types are theoretically free to draw in perspective mode. Have you considered drawing your text flying around a 3D model ?.

To draw your texts facing toward the user as normal, you need to translate them to an appropriate depth, as opposed to switching to ortho mode. Scaling and rotating the text are also a snap.

(font bitmap should also be rendered in perspective mode too, if you're curious, I'll post a sample).

Share this post


Link to post
Share on other sites
You are clearly setting up glOrtho to have 0,0 at the top-left, whereas in your perspective, 0,0 is at the bottom-left.

For consistency it might be easier to have them the same way around.

Alternatively, enable your text drawing routine to draw text both ways.

Opengl is not drawing the fonts upside down, it's drawing them the right way up, you're just looking at them upside down :)

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by Skeleton_V@T
Hints are coming up for you: Don't use ortho mode to draw, keep your texts floating around in a 3D environment.


You sure about that? Sure seems like ortho mode is the right mode for displaying an overlay like a HUD, GUI, or menu system, which is what I am trying to achieve with the FPS counter in the top left.

Quote:

There're basically three types of fonts: font texture, font bitmap and font outline. Except font bitmap doesn't have the ability to scale and rotate, the two remaining types are theoretically free to draw in perspective mode. Have you considered drawing your text flying around a 3D model ?.


Everything on the screen except the HUD components (top left & bottom right) are in perspective mode (I've just moved the camera close up for the screenshot). The perspective draws are fine (scaling, rotating, spinning, billboarding etc). I have a bitmap font class that I can use for the overlay, but I have some nice texture fonts I want to use on the overlay. I shouldn't have to give up ortho mode without first understanding more about why it's not working the way I want - which it appears is my setup of ortho mode.

F451

Share this post


Link to post
Share on other sites
Quote:
Original post by markr
You are clearly setting up glOrtho to have 0,0 at the top-left, whereas in your perspective, 0,0 is at the bottom-left.

For consistency it might be easier to have them the same way around.

Alternatively, enable your text drawing routine to draw text both ways.

Opengl is not drawing the fonts upside down, it's drawing them the right way up, you're just looking at them upside down :)

Mark


Ok. I will check into that. Thanks.

F451

Edit: You were correct markr, I had used a different Switch2D() routine recently that had the glOrtho command as (0,w,h,0,..) instead of (0,w,0,h). Now everything is consistent again and working! Thanks.

[Edited by - Fahrenheit451 on January 27, 2006 6:11:46 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!