🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

nehe bitmap fonts

Started by
4 comments, last by Endar 18 years, 4 months ago
I'm attempting to use bitmap fonts to write text to the screen.

/**
 * Draw some text on the window
 * The majority of code from this function is from the NeHe tutorial 13.
 * \param text A char pointer to the string
 * \param x The x opengl coord to draw the text at
 * \param y The y opengl coord to draw the text at
 * \param len The length of the string (if set to -1, it will be counted inside the function using 'strlen')
 */
void COpenGLDriver::drawText(const char* text, int x, int y, int len = -1)
{
	int length;

	// Don't print anything
	if( text == NULL )
		return;

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

	glColor3f(1.0f, 1.0f, 1.0f);
	// move the window position to the position to draw the text
	//glRasterPos3f(x, y, -2);
	glRasterPos3f(0, 0, -2);

	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
}

/**
 * Set up our windows bitmap font
 * The majority of code from this function is from the NeHe tutorial 13.
 */
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(		-15,	// 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
}

It works in a previous project, but not in this one (lovely how even simple code does that :D ). All I've really done it cut and paste and change the name of a variable to the name of a class member variable, all of which seems to work. OpenGL doesn't throw up any errors. I have a feeling that the problem is with the glRasterPos function. Any way, if someone sees something that could be a problem, or has a better way of drawing text to the screen, or simply wants to give their 2 cents, go for it.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
If you set your raster position outside of the current viewing window then all following draw calls are discounted; try looking at The Red Book, Chapter 8, "Bitmaps and Fonts". In particular, under the heading "The Current Raster Position" there are details on how to test if your raster position is valid or not. Here's a quick link to Chapter 8 online, clicky.

Sorry for the brief reply but I'm in a rush, hope that helps though!
--
Cheers,
Darren Clark
It lets me see if the position is valid, which counts for a lot.

Anyway, when I attempt to set the raster position like so: "glRasterPos3f(0, 0, -1);", when I use glGet and get the raster position immediately after the call to glRasterPos, I get (400, 300, 0, 1), which is slightly confusing.

I thought that maybe the current matrix transform was having an effect, so I added code that push another matrix onto the stack and reset it to the identity matrix, but it made no difference.

Edit:: It seems that no matter what rasterpos function that I use, unless I input a valid z-value it will only show 0.0f for each of the xyzw values, and once I put in a valid z-value the x and y values rocket to 400 and 300.

I am fairly confused.

[Edited by - Endar on February 21, 2006 9:01:24 PM]
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Also, add to the above that the modelview matrix, which apparently the position passed to glRasterPos is transformed by, is the identity matrix, the whole time.

After setting the raster position to (0.0f, 0.0f, -2.0f), using glRasterPos3f, when I call "glGetFloatv(GL_CURRENT_RASTER_POSITION, tempf)" I get (400.0, 300.0, 0.501002, 2.0) back from open gl.

Edit:: also turned off lighting to no effect (not that I really thought it would have one. The less variables in play, the better).



Edit::Again::

Okay, hold on a second. I just took a look at the line of code to create the engine, and I'm making a window 800x600. So, now that raster position of (400.0, 300.0, 0.501002, 2.0) makes a little more sense when I'm attempting to draw text at 0,0 open gl coords.

But, what about the other two, the z and w coords? If I'm attempting to draw at a z value of -2, why does the raster pos show 0.501002? And how does the w value affect it?

... 2 minutes later

Yeah, I just changed the window size and the 400,300 changed to half each of the sizes. But, the z and w stayed the same.

[Edited by - Endar on February 22, 2006 5:58:45 AM]
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
bumpety bump
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
hmmmm. bump.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper

This topic is closed to new replies.

Advertisement