Jump to content
  • Advertisement
Sign in to follow this  
Endar

OpenGL bitmap fonts

This topic is 4640 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

Having some problems with bitmap fonts and glRasterPos. I've used the NeHe tutorial on bitmap fonts and that went well when it was only a project for bitmap fonts, but incorporating it for use in my engine has become a little harder.
void COpenGLDriver::buildFont()
{
	HFONT font;		// Window font id
	HFONT oldfont;		// Used for good house keeping

	m_fontList = glGenLists(96);		// Storage for 96 characters

	font = CreateFont(	-24,	// Height of font
				0,	// Width of font
				0,	// Angle of escapement
				0,	// Orientation Angle
				FW_BOLD,// Font weight
				FALSE,	// Italic
				FALSE,	// Underline
				FALSE,	// Strikeout
				ANSI_CHARSET,	// Character set identifier
				OUT_TT_PRECIS,	// Output precision
				CLIP_DEFAULT_PRECIS,	// Clipping precision
				ANTIALIASED_QUALITY,	// Output quality
				FF_DONTCARE|DEFAULT_PITCH,// Family and pitch
				"Courier New");	// Font name

	oldfont = (HFONT) SelectObject(m_hdc, font);	// Selects the font we want
	wglUseFontBitmaps(m_hdc, 32, 96, m_fontList);	// Builds 96 characters starting at character 32
	SelectObject(m_hdc, oldfont);		// Selects the font we want
	DeleteObject(font);			// Delete the font
}

Where m_fontList is an 'unsigned int' member variable for the COpenGLDriver class. Drawing the text should be fairly simple as well.
void COpenGLDriver::drawText(const char* text, float x, float y, int len = -1)
{
	// no string == don't print anything
	if( text == NULL )
		return;

	// store the length of the string (either supplied or we count it using 'strlen')
	int length = (len == -1) ? strlen(text) : len;

	glTranslatef(0.0f, 0.0f, -2.0f);
	glColor3f(0.0f, 0.0f, 0.0f);

	// temporary raster position just to get it working
	float tempff[3] = {0.0f, 0.0f, -1.0f};
	glRasterPos3fv(tempff);
	
	glPushAttrib(GL_LIST_BIT);	// Pushes the display list bits
	glListBase(m_fontList - 32);	// Sets the base character to 32

	glCallLists(length, GL_UNSIGNED_BYTE, text);	// Draws the display list text
	glPopAttrib();		// Pops the display list bits
}

I also have a bunch of debug messages in there which do a variety of things: prints the raster position, states whether the raster position is valid, prints out the modelview matrix and prints out the opengl error string. The following applies when I set the raster position to what it is in the code (0,0,-1):
  • I get the raster position from open gl, I get (400, 300, 0.668, 3). My window is 800x600, so the 400 and 300 make sense.
  • When checking if the raster position is valid, open gl returns 'true'.
  • The modelview matrix is set to the identity except where the translation vector is set. It is set to (0,0,-2).
  • The opengl error string is set to "no error"
So, I'm looking for help. I've already checked the code for 'buildFont' and the code to draw the text against NeHe and except for the fact that I'm not using a function with variable arguments, and the font list id is a member variable, it's not really different. Don't get bogged down by my assumptions, but I am assuming that the problem is in how I set the raster position. I'm just not sure where to go from here.

Share this post


Link to post
Share on other sites
Advertisement
Hi Endar, I recently integrated NeHe bitmap font routines into my own engine - a bitmap font class. What you have going there looks fine. You don't describe any particular problems you might be having. Is the text not showing?

F451

Share this post


Link to post
Share on other sites
Quote:
Original post by Fahrenheit451
Hi Endar, I recently integrated NeHe bitmap font routines into my own engine - a bitmap font class. What you have going there looks fine. You don't describe any particular problems you might be having. Is the text not showing?

F451


Wow. How did that happen? I just wrote this huge post and I actually left out what the problem was. [grin]

Yeah, the text isn't showing. The background color is grey (0.5, 0.5, 0.5) and I've tried setting the color to pure black (0,0,0) and pure white (1,1,1) just before setting the raster pos and drawing the text. Nothing.

The size is set to 24, and the translation is only -2 on the z axis, so I'm assuming that the text hasn't moved past the clipping range.

Pretty much I haven't seen the text since I've tried incorporating it into my engine.

I just know that it's going to be something really, really, really small. Something so tiny, the whole problem will probably be a character in the wrong spot. It always is with my programs. [smile]

Share this post


Link to post
Share on other sites
Comparing your code and mine, I would guess your device context is not valid in the create routine. Define a local context and grab the current one with the wglGetCurrentDC() call:


void COpenGLDriver::buildFont()
{
HFONT font; // Window font id
HFONT oldfont; // Used for good house keeping

HDC m_hdc; // local device context
m_hdc = wglGetCurrentDC(); // get current device context

m_fontList = glGenLists(96); // Storage for 96 characters

font = CreateFont( -24, // Height of font
0, // Width of font
0, // Angle of escapement
0, // Orientation Angle
FW_BOLD,// Font weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character set identifier
OUT_TT_PRECIS, // Output precision
CLIP_DEFAULT_PRECIS, // Clipping precision
ANTIALIASED_QUALITY, // Output quality
FF_DONTCARE|DEFAULT_PITCH,// Family and pitch
"Courier New"); // Font name

oldfont = (HFONT) SelectObject(m_hdc, font); // Selects the font we want
wglUseFontBitmaps(m_hdc, 32, 96, m_fontList); // Builds 96 characters starting at character 32
SelectObject(m_hdc, oldfont); // Selects the font we want
DeleteObject(font); // Delete the font
}







Also, if you are in orthographic mode, then check that your glTranslatef and glRasterPos3fv are within the near and far clip planes.


hth
F451

[Edited by - Fahrenheit451 on March 4, 2006 8:38:13 AM]

Share this post


Link to post
Share on other sites
Not sure if this will help you or not, but I have had similar problems displaying bitmaps after I have done normal opengl drawing routines. The way I fixed the problem was to turn off anything I had called glEnable with like depth testing or texturing or whatever else you might enable. I did my bitmap drawing last because I wanted it to appear on top of what I already drew so I didn't need that stuff enabled anymore anyway.

Hope this helps.

Share this post


Link to post
Share on other sites
Well, the good news is, I found the problem.

The bad news is, I'm a moron.

Up until about 5 minutes ago, here's what the main drawing loop looked like:

while( engine is still running (hasn't recieved WM_QUIT) ){
begin the scene (reset misc vars)
reset the modelview matrix to identity
render scene graph
end scene ( glFlush() and SwapBuffers() )

if fps is different from last loop iteration
print fps in window caption
draw text
}


That's right. The text drawing is after the buffer is copied to the window. So, I'm basically printing to the back buffer after it gets shown in the window and just before I clear it.

So as you can see, the bad news, is pretty bad. [grin]

Share this post


Link to post
Share on other sites
I should really be looking out for them more. It just seems that all my mistakes that take a really long time to fix are the tiny little ones like this. Like, everything would work if I moved a particular line of code up a couple of lines, or if the > was >=.

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!