# Positioning 2D text correctly

This topic is 4548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My OpenGL scene is just a few cubes moving around. I'd like to have text labels next to each cube. I've worked out how to use the bitmap fonts, but I can't position them how I'd like. I want the labels to always be about 20 pixels to the right of each cube, no matter which angle I'm looking at them from. At the moment, as I move around, the position of the text shifts relative to the cube. Here's the code:
glBegin(GL_QUADS);
cube(0.0f,0.0f,0.0f,0.1f);
glEnd();

glRasterPos2f(0.5f,0.0f);
glPrint("hello");

What options are available to me?

##### Share on other sites
I hope this is usefull.
Try to project the cube onto the near camera plane. With the projected cube get the bounds in screen coordinates then calculate the offset from the bounds (right edge, left edge, ...)where you want to position the text. You'll have to do the projection calculations yourself, and you're doing this to get where it would end up on the near plane. You will glPrint(...) the text in Ortho view.

##### Share on other sites
Would I have to take into account my current projection matrix?

EDIT: Additionally, how do I work out my near camera plane? Is it simply the plane perpendicular to my camera vector?

EDIT: I think I understand now. I build my modelview matrix, then do glOrtho to get the projection onto the near camera plane. Then I manually get the projection of the vertices of the cube, and when I put it together I'll have the screen boundary of the cube. Is that correct? If it is, what do I pass as the fourth coordinate? I assume it would be 1, as in (x,y,z,1).

##### Share on other sites
Hi,

Have a look at the gluProject method:

gluProject -- map object coordinates to window coordinates

int gluProject(GLdouble objx,               GLdouble objy,               GLdouble objz,               const GLdouble modelMatrix[16],               const GLdouble projMatrix[16],               const GLint viewport[4],               GLdouble *winx,               GLdouble *winy,               GLdouble *winz)

You can then use winx, winy to help position your text.

I'm sure a NeHe tutorial covers its use - but cannot remember. If not Google will help.

Cheers,
Dave

##### Share on other sites
Thanks, gluProject works well. Thanks to you too miminawewe, but I think your method involves too much work. Or I was doing it all wrong.

I think I should be fine now. Using gluUnProject I can take the screen coordinates I want and convert them to coordinates for glRasterPos3d. All I have to do is gluProject every vertex of the cube, see which one is rightmost, and specify screen coordinates 20 pixels to the right of it. Thanks for all the help.

##### Share on other sites
The above post was by me.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013630
×