1

What I am trying to do is that as I draw every object, I check to see if it is tagged, and if tagged, write some text near the object, using glRasterPos2f/i to set location on screen, and then glutBitmapCharacter.

For glRasterPos2, I see a lot of examples with pixel coordinates passed as arguments (example: 100,100). Pixel coordinates do not work for me, and instead I have to translate to coordinates on a [-1.0 1.0] scale. [-1 -1] is bottom left of screen, [1 1] is top right.

One thing that I have noticed in the examples is a change to an orthographic projection. Is this the reason for my issue?

I have gluPerspective(); set once at the start, and it is not changed (for now). If I have to change to gluOrtho every time I want to write text at pixel coordinates, it seems messy. For each object, I start in gluPerspective(), and then change if needed to gluOrtho(), and then back again.

Another option I have thought about, is since things seem to work on the [-1.0 1.0] scale, I can manually translate screen coordinates onto the scale. But this also seems messy and bad for performance.

Any advice on how to proceed?

M F
  • 7
  • 4
  • What is the problem you are having? You ask about the "reason for my issue", but I don't see anything describing what the issue is. – Reto Koradi Oct 07 '15 at 07:56
  • The issue that I have is that if I use glRasterPos2f( 4.0, 4.0 ); as an example, the subsequent call to glutBitmapCharacter does not show up on screen. At this point, I am seeing different behavior vs. some examples I have seen. But, if I use glRasterPos2f(x/windowWidth*2-1,y/windowHeight*2-1), I get the text at the x,y location I expect on the screen. – M F Oct 07 '15 at 13:02

2 Answers2

1

The glRasterPos famlili of functions will transform the coordinates using the current ModelView and Projection matrices, similiar to the coordinate transformations which are applied to vertex data.

You can use any matrices you like at this stage. If you use the same matrices as for drawing your object, you can simply use the glRasterPos3f variant to directly specify the point in 3D object coordinates.

derhass
  • 43,833
  • 2
  • 57
  • 78
1

glRasterPos takes coordinates in the same coordinate system as drawing primitives. So if you have set up the transformations for a 3D object, and made the call(s) for drawing it, you can simply pass the same coordinates that you were using for the object vertices to glRasterPos.

For example, if you want to place a text next to a vertex, you call glRasterPos with the same coordinates that you also used for the vertex, while the same transformation is still set. glRasterPos will then transform these coordinates with the modelview and projection transforms that were also applied to the vertex, and the bottom-left corner of the text you draw with glutBitmapCharacter() will be aligned with the vertex.

If you want to position the text at a given pixel position, you can use glWindowPos instead of glRasterPos.

As the name suggests, it takes its arguments in window coordinates, meaning that none of the model/view/projections are applied. Window coordinates are in pixel units, with the origin at the bottom-left corner of the window.

For example, the following call will set the raster position 100 pixels to the right and 30 pixels above the bottom-left corner of the window:

glWindowPos2i(100, 30);

Using this call, you don't have to worry about changing your transformations for rendering text.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • But OP wants to "write some text near the object", which does need to take transformations into account. – Jongware Oct 07 '15 at 06:49
  • 1
    @Jongware Hmm, yes. `glRasterPos` should actually work just fine for that case. They talk about "my issue", but don't really say what the issue is. – Reto Koradi Oct 07 '15 at 07:55