4
votes

i need some help to print a string over a opengl 3d scene. I have some problems using modelview matrix and projection matrix. As I understand, projection matrix is used for the scene setup, and modelview matrix is used for drawing and transformating an object in the scene. So, to paint something over a 3D scene I understand that the next procedure most be done:

  • setup projection matrix for 3d scene
  • setup modelview matrix for 3d scene
    • draw opengl stuff
  • setup projection matrix for 2d overlay
  • setup modelview matrix for 2d overlay
    • draw opengl stuff for hud

Is this the right procedure?

1
Yes, for the most part that is the correct thing to do. However you ask about printing strings, which opengl has no real support for, so I'm not sure how much that will help you (unless you already have your string rasterized into a texture). - Tim
Thanks, yeah, I was a bit confused. - Hermandroid

1 Answers

3
votes

For 2D rendering you can think of the projection matrix as you canvas and the movelview as the position of your text. You want to setup an orthogonal projection like this:

glOrtho(0.0F, windowWidth, 0.0F, windowHeight, -1.0F, 1.0F);

With this projection each unit means one pixel (glTranslate2f(120.0, 100.0) would position your text at pixel (120, 100). This projection maps the (0,0) in the bottom-left corner of your screen. If you want to map it to top-left corner you can do:

glOrtho(0.0F, windowWidth, windowHeight, 0.0F, -1.0F, 1.0F);

As of how to render text, OpenGL doesn't support it by default which means either you have to create a font rendering lib or using a existing one. Before doing that, you'll need to have in mind your needs for text rendering: Is is flat or 3D? It will be static or transformed(scaled, rotated), etc.

Here you can find a survey on opengl text rendering techniques with pros and cons and libs: http://www.opengl.org/archives/resources/features/fontsurvey/

I started using FTGL to render TTF font, but the lib was destroying my game performance. So I decided to implement my own font lib and went with texture fonts, which are not better than TTF, but it was ok for my needs.

I used this program: http://www.angelcode.com/products/bmfont/ It takes a TTF font and exports a texture and a file descriptor containing each letter position, size and padding in the texture that you will use to render.

Hope that helps.