2D text in OpenGL scene

Hi!

I want to display text in an OpenGL scene.

The text should be

  1. “2D”, always the same size, always fully visible (not depended on camera
    distance or position)
  2. hidden, if there is an object between the 3D origin point of the text and
    the camera (this object obscures the text)

If I use for example wglUseFontBitmaps then 1. works but 2. is a problem.

If I use a 3D objects (e.g. polygons) to represent the characters then 2.
works but 1. will be the problem.

Does anybody have a suggestion to solev the problem?

Thank you very much!

Kind regards,

Horst

1.To represent the 2D fonts, you need an orthographic projection. So you can use from the following code:

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(…);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
DrawText(…);
glMatrixMode( GL_PROJETION );
glLoadIdentity();
glFrustum(…); //Or gluPerspective();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Drow your 3D objects
-Ehsan-

link:

http://nehe.gamedev.net/lesson.asp?index=03

I, personally, learned how to do 2D fonts from the site above years ago. also, search engines rock [don’t be afraid to use it next time :wink: ]

hope that helps
:regards:

Originally posted by HorstStech:
[QB]I want to display text in an OpenGL scene.

The text should be

  1. “2D”, always the same size, always fully visible (not depended on camera
    distance or position)
  2. hidden, if there is an object between the 3D origin point of the text and
    the camera (this object obscures the text)

If I use for example wglUseFontBitmaps then 1. works but 2. is a problem.

If I use a 3D objects (e.g. polygons) to represent the characters then 2.
works but 1. will be the problem.

Please note that this information is using my understanding of how OpenGL and 3d graphics work and it may not be 100% correct.

First it depends on how you are drawing your fonts. Since you say you want your fonts to be 2d I am going to assume that you are using Bitmap or pixmap fonts. Using this assumption, you can then use glRasterPosx() function to move your font around in screen coordinates.

What may work for you is if you first change to the projection matrix, using glMatrixMode( GL_PROJECTION ) and then set up gluOrtho2D() using your window height and width and a glPushMatrix(). At this point, you will be drawing in screen coordinates. Then, do a glRasterPos3d() function and pass in the x and y in screen coordinates and an x value. Then draw your font. After that do a glPopMatrix() and change to the model view matrix using glMatrixMode(GL_MODELVIEW). I belive that this will give you the desired results. Below is a sample of what I am saying:

  
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0f, winWidth, 0.0f, winHeight);
glPushMatrix();
glRasterPos3d(xPos, yPos, zPos);
//draw text code
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Let me know if this helps you out or not. :slight_smile:

Good luck.

Oh, and don’t pay attention to the person telling you to “google” for an answer. Because what else is the OpenGL coding:beginners forum for if not to ask questions and try to get people to help you?

Oh, and don’t pay attention to the person telling you to “google” for an answer.
Oh? What could be better than the sum of man’s knowledge at the tip of your fingers?