OpenGL 2D Text in 3D Space

Started by May 25, 2002 07:23 AM
1 comment, last by alexgsmith 17 years, 11 months ago
alexgsmith
Author
148
May 25, 2002 07:23 AM
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
1,802
May 25, 2002 08:11 AM
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
May 25, 2002 12:02 PM

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);<br>	glDepthFunc (GL_LESS);<br>	glPopMatrix ();<br>	glMatrixMode(GL_MODELVIEW);<br>	glPopMatrix ();<br>}<br>  </pre>     </i> <br><br><SPAN CLASS=editedby>[edited by - invective on May 25, 2002 1:03:53 PM]</SPAN>    
Share:

This topic is closed to new replies.