Archived

This topic is now archived and is closed to further replies.

2d text in 3d space

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i need to create a flying string that would appear on the screen when the player picks something up. since the game is 3d and the player can move around on all 3 axes, i see no way of calculating the correct screen coordinates fot rhe text in ortho mode. lesson 14 of the nehe tutorials uses outline fonts which can be moved around in 3d, but i don''t need to use outline fonts - what i need is a simple 2d string that would retain its size and orientation (always facing the viewport) no matter how i move the camera. well, move the world, but that''s relative... anyway - i have createad my own set of fonts like those supplied with nehe''s tutes and that is basically what i''m stuck with. could anyone point a finger at what i should look for? thanks crispy

Share this post


Link to post
Share on other sites
I recommend using the feedback buffer.

I''ve already said that thousands of times (not really, in fact it was billions of times), but you should look at The Red Book which has VERY good explanations. Go to "Chapter 13 - Selection and Feedback".

The feedback buffer retrieves what coordinates are passed to the graphics card.
You may do something like that :

...
draw_your_picked_object();
GLfloat obj_2D_coord[3];
GLint nb_vertex;
glFeedbackBuffer (3, GL_2D, obj_2D_coord);
(void)glRenderMode(GL_FEEDBACK);
glBegin(GL_POINTS);
glVertex3f(...); // draw a vertex representing the picked object, something like the center of the object, or the top of the object (if you want the text to be displaye on top of the object)
glEnd();
nbvertex = glRenderMode (GL_RENDER);
if (nbvertex == 0)
{
// the displayed vertex in not in the viewing frustum.
}
else
{
// the displayed vertex is in the viewing frustum
// and the screen coordinates are stored in
// obj_2D_coord[1] (X coordinate) and obj_2D_coord[2] (Y coordinate)
// note that obj_2D_coord[0] is useless in this case
GLint matrix_mode;
GLint viewport[4];
GLboolean depth_test;
GLboolean lighting;
glGetIntegerv(GL_MATRIX_MODE, &matrix_mode);
glGetIntegerv(GL_VIEWPORT, viewport);
glGetBooleanv(GL_DEPTH_TEST, &depth_test);
glGetBooleanv(GL_LIGHTING, &lighting);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, (GLdouble)viewport[2], 0.0, (GLdouble)viewport[3], -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glColor3f(...); // set the color of the text.
glRasterPos2f(obj_2D_coord[1], obj_2D_coord[2]);
glPrint(...); // set the text string.
if (lighting == GL_TRUE) glEnable(GL_LIGHTING);
if (depth_test == GL_TRUE) glEnable(GL_DEPTH_TEST);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(matrix_mode);
}

Share this post


Link to post
Share on other sites
OR you could simply draw it last like so.

      
//Draw World

DoMyRotations();
DrawMyWorld();
//Save Matrix State

glPushMatrix();
//Reset Coordinate System

glLoadIdentity();
//Make Sure Text Is On Top

glDisable(GL_DEPTH_TEST);
//Draw Text

DrawMyText();
//Put Everything Back

glPopMatrix();

It's simpler than it looks.

EDIT--> Heh, seems that's what the last poster did. I just simplified it by not puting the real draw code in. Anyway, good luck to you. (re-edit: no wait, his is more complicated)<--EDIT

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I'm using Windows!"

Edited by - ATronic on February 20, 2002 9:16:49 AM

Share this post


Link to post
Share on other sites