Sign in to follow this  
Beaverbutt8

OpenGL text problem

Recommended Posts

Hey guys, I got OpenGL text to work. The problem is that it functions the same way as a 3d model. If i turn the camera away - i can't see it. So, here's my question. How can i keep the text fixed on one position ( bottom left corner ) of the screen, regardless of what the camera does? A related question is how can i do the same thing with a .bmp that is the "Interface" of the game - what i mean is, like the ones you see in rpg's, a bar on the side, or top or whatever that shows your health, items, etc, etc. Thanks :D Mike

Share this post


Link to post
Share on other sites
what i do because i suck is keep a relative coordinate which is always at the middle of the screen thats being rendered, but i believe there are ways to do overlays and stuff independant of the actual coordinates that are being looked at.
still, relative origin coordinates seem to work pretty well for me, might be a good started o_O. just make them move the same rate as your camera does and it should be ok

Share this post


Link to post
Share on other sites
Make overlays the last step in rendering the scene.
Turn depth testing off.
Set the camera to an orthographic projection.
Load the identity matrix.
Begin rendering the text.


// revert the camera to orthographic mode
g_pCamera->SwitchProjections();

if (g_bHideText)
{
// push the matrix
glPushMatrix();

// load the identity matrix
glLoadIdentity();

// draw the text
g_pFPSText->Draw(rElapsedTime);
g_pStatText->Draw(rElapsedTime);
g_pHelpText->Draw(rElapsedTime);

// pop the matrix
glPopMatrix();
}

// revert the camera to a perspective mode
g_pCamera->SwitchProjections();

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
render your text as the last thing - overlays (correct me if i'm wrong)

and do a search here on game dev or google for glOrtho or gluOrtho

Share this post


Link to post
Share on other sites
Alright - got it. Now, how do i keep the text in the bottom left corner of the screen at all times. The camera can rotate by the mouse or keys, and go forward and back, and throughout all of this, i would like for the text to stay put :)


Note: It will probably help to know that i'm using the Apron tutorials camera setup

Share this post


Link to post
Share on other sites
That's what glOrtho is for. Here's some sample code (in Java):


GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glOrtho(0,width,32,height,-1,1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();

// draw all your text here
glPrint(width - 100, height - 20, "FPS: " + (int)time.getFrameRate() );

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_LIGHTING);

Share this post


Link to post
Share on other sites
I'm assuming you're using outline fonts here, so if you're not, please excuse me.

Now, you want to keep the Z axis for the outline font stationary. Outline fonts will always have a Z axis that can rotate, so instead of using them you'd be so much better off using bitmap fonts which are 2D. Use some of the code from this tutorial.

Good luck!

Share this post


Link to post
Share on other sites
Maybe this code can help you.


// render 3d scene
...

// switch the projections
glMatrixMode(GL_PROJECTION);
// clear the current projection
glLoadIdentity();
// create an ortho matrix bound to the size of the window
glOrtho(0.0, nWndWidth,
0.0, nWndHeight,
-1.0, 1.0);
// switch back to the modelview matrix
glMatrixMode(GL_MODELVIEW);

// enable / disable states
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);

// push a model view matrix
glPushMatrix();

// load the identity matrix
// this step can be skipped if the model view matrix
// is going to be the identity matrix
glLoadIdentity();

// draw the text between: [0.0, nWndWidth] ---> X
// [0.0, nWndHeight] ---> Y
// [0.0] ---> Z
glBindTexture(GL_TEXTURE_2D, nTexUnit);

// begin drawing quads
glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(11.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(11.0f, 11.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 11.0f, 0.0f);

// stop drawing quads
glEnd();

// pop a model view matrix
glPopMatrix();

// enable / disable states
glEnable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);



Assuming I haven't messed the code up, this should get you want you want. I haven't looked at the Nehe tutorials, but I prerender all the text before it is displayed. I'm sure it is along the same lines as this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this