3

For the entire night, I've been looking around the internet, both stackoverflow and elsewhere, to find something to say how to print text on GLUT. While I've found places that say how, none have explained it well, saying which parts of the function is neccessary, which parts aren't. I've also tried to copy in some of the code with the closest to a success is something that made my entire screen white except for some blue pixels. So I've given up, and I'm hoping this will clear up confusion for me and the many people who are confused, like me.

So, I have found this code:

glColor3f(1.0f, 0.0f, 0.0f); glRasterPos2f(1280, 720); int len = menu.length(); for (int i = 0; i < len; i++) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]); } 

and I have placed it in my code:

void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); glTranslatef(0.0f, 0.0f, -9.0f + zoom); glTranslatef(0.0f, -1.0f, 0.0f); string menu = "Hello!"; glColor3f(1.0f, 0.0f, 0.0f); glRasterPos2f(1280, 720); int len = menu.length(); for (int i = 0; i < len; i++) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]); } /*if I need to post the rest of drawScene(), which is the function delegated as the Display Func, tell me. I don't want to because it's long 

What I want to know is what am I doing wrong, and what do future readers in my position need to do in order to get good results.

1 Answer 1

4

You don't say what's specifically wrong, but I'm suspecting that your text is not showing up. The reason is likely that the raster position is being clipped, and this is causing your text to not be rendered.

The raster position is the "anchor point" of where a bitmap will be drawn. Usually, this is the lower-left corner of the bitmap (the glBitmap can change that with by setting the x and y parameters to something other than zero, but assume you're not doing that). The raster position is transformed by the model-view matrix, just like a vertex in a geometric primitive. And just like a vertex, if the transformed raster position lies outside of the viewport, it's clipped, and nothing is rendered. What's important to know here is that any rendering of a bitmap - regardless of its size - is predicated on the raster position being inside of the viewport.

In your example, you don't show the viewport you're using, nor the projection transformation (the matrix on the GL_PROJECTION stack), but you set the raster position to (1280, 720), which may well be outside of the viewport.

Let's say you want to render your text in the lower-left corner of your window (and for the sake of argument, let's say your window is 1280 x 1024). When it's time to render your text, drop the following into your rendering routine:

glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadIdentity(); gluOrtho2D( 0, 1280, 0, 1024 ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); glRasterPos2i( 10, 1014 ); // move in 10 pixels from the left and bottom edges for ( int i = 0; i < len; ++i ) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]); } glPopMatrix(); glMatrixMode( GL_PROJECTION ); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); 

Depending on which version of OpenGL you're using, you may be able to use a simpler routine glWindowPos2i() (the 2i can be replaced with other dimension-type pairs like other OpenGL functions), which bypasses transforming the raster position by the model-view and projection matrices, and works directly in window coordinates. In that case, you'd write the above code as:

glWindowPos2i( 10, 1014 ); // move in 10 pixels from the left and bottom edges for ( int i = 0; i < len; ++i ) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, neither of those work. Is there a way to test what's wrong with it in a situation like this?
Just start from the most controlled situation, like rendering in normalized device coordinates, and position things at the origin and see if anything shows up. I'd still bet it's related to viewport or clipping issues, but we'd need more information to determine that.
I saw a bit of text in the top left corner, it turned out 1014 was too high of a number. Thank you so much, I've been trying to do this for so long.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.