Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL 2D Text in 3D Space

Started by alexgsmith May 25, 2002 at 7:23 AM 1 replies 17.7k views
Original Post
alexgsmith
alexgsmith
Hi, this is my first post so please be gentle I am using OpenGL with GLUT. I have a gluPerspective viewport with which I wish to output 2D text ''over the top of it'', so that if I print text at 10,10 it will print it at pixel coord 10,10, not world coordinate 10,10. I can successfully do so in an orthographic viewport, but not in a 3D viewport. Can anybody help or point me in the right direction!? Thanks!
Yann L
Yann L
Since this is your first post: try to post OpenGL specific questions in the OpenGL forum.

To your question: You could do it in a 3D viewport, but it''s not that trivial. The simplest way is to use orthographic projection, esp. if you already have it working there.

Here is some code snippet:

// assume, you are currently in perspective 3D projection.
// And that the current matrix mode is GL_MODELVIEW

// Draw your 3D scene here

// Save your projection matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();

// Switch to orthographic projection
glLoadIdentity();
glOrtho(...)

// Back to the modelview matrix mode, so that you can translate/scale your text
glMatrixMode(GL_MODELVIEW);

// Render your 2D text

// get back the old prespective projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

// And you are in perspective mode again.

/ Yann
invective
invective
Modify as necessary to suit your needs:


void print(int x, int y, const char *string)
{
//Assume we are in MODEL_VIEW already
glPushMatrix ();
glLoadIdentity ();
glMatrixMode(GL_PROJECTION);
glPushMatrix ();
glLoadIdentity();

GLint viewport [4];
glGetIntegerv (GL_VIEWPORT, viewport);
gluOrtho2D (0,viewport[2], viewport[3], 0);

glDepthFunc (GL_ALWAYS);
glColor3f (1,1,1);
glRasterPos2f(x, y);
for (int i = 0; string!= '\0'; ++i)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
glDepthFunc (GL_LESS);
glPopMatrix ();
glMatrixMode(GL_MODELVIEW);
glPopMatrix ();
}


[edited by - invective on May 25, 2002 1:03:53 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.