This topic is now archived and is closed to further replies.

2D text on screen

This topic is 5015 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 all, I know NeHe tutorial chapter 17 talks about it all, but its too mixed up. I get lost in the middle and dont get a word. All I need to do is display a stationary 2D text on one side of the screen and overwrite/change it when required. Is there an easier way? Please help. Thanks.

Share this post

Link to post
Share on other sites
I''m guessing you want text that you can place using 2D pixel screen coordinates, and you don''t want the text to change position just because the camera has changed. The answer to your problem is an Orthographic projection, OpenGL''s "2D mode"

Here''s a way of going in and out of Ortho mode:

void Enable2D()
int viewPort[4]; // Get the current viewport dimensions

glGetIntegerv(GL_VIEWPORT, viewPort); // Select the projection matrix

glMatrixMode(GL_PROJECTION); // Push the current projection matrix

glPushMatrix(); //save matrix

glLoadIdentity(); // Setup ortho mode

glOrtho(0, viewPort[2], 0, viewPort[3], -1, 1); // Select the modelview matrix

glMatrixMode(GL_MODELVIEW); // Push the current modelview matrix

glLoadIdentity(); //reset modelview matrix



// Disables orthographic mode and returns back to

// perspective projection mode for 3D rendering

void Disable2D()
// Select the projection matrix

// Pop the current projection matrix to return to

// perspective mode, our original projection matrix


It isn''t that important if you don''t totally understand this code right away. Just understand that it puts you into a "2D mode" where everything you do is in pixel coordinates on the one screen. So If you wanted to do stuff in 2D pixel coordinates, you''d go like this:


//code here

Disable2D(); //go back to perspective projection

Here''s some good code for placing text on the screen:

void glPrintf(const char *fmt, ...) // Custom GL "Print" Routine

char text[256]; // Holds Our String

va_list ap; // Pointer To List Of Arguments

if (fmt == NULL) // If There''s No Text

return; // Do Nothing

va_start(ap, fmt); // Parses The String For Variables

vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers

va_end(ap); // Results Are Stored In Text

glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits

glListBase(g_listBase - 32); // Sets The Base Character to 32

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text

glPopAttrib(); // Pops The Display List Bits


Note the g_listBase thing? That''s a global variable, sort of an "identifier" for your font, which you get when you use the CreateBitmapFont() function from one of the tutorials. You''ll probably want to change this function so that it takes in a listbase (an unsigned integer) instead of using a global variable. Here''s how you would use this function to put text on the top left corner of the screen:


glRasterPos2f(20, 20);
glPrintf("X location: %.2f", x);
glRasterPos2f(20, 10);
glPrintf("Y location: %.2f", y);


Seems kind bulky just for placing text on the screen, so you might want to make one function that does all this in one shot.

Share this post

Link to post
Share on other sites
thanks a lot EGD Eric. That was really helpful. Another thing, I only want to display float numbers....that would mean changes in the CreateBitmap function right (loading only numbers and the decimal point)?? I dont want to load the whole characters which I wont be using anyways, gotta save as much memory as possible. Any idea where I can get or how to create the text bmp like the one NeHe was using?

[edited by - SuperDuck on March 24, 2004 5:05:48 PM]

Share this post

Link to post
Share on other sites