Jump to content
  • Advertisement
Sign in to follow this  
Silex

3D point to 2D screen coordinates?

This topic is 4609 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

I'm drawing 2D labels (text) that follow my 3D objects around, but as the 3D objects move away from origin the labels move further and further away from the actual 3D object.
ASCII art:

           /\               /\                    /
.origin    \/   object0     \/        object1     \/              object2







The diamonds are the 3D meshes. "object*" are the 2D text labels. Here are the functions I call to get the 2D screen location of a 3D object:
    double x,y,z;
    double view[16];
    double proj[16];
    int viewport[4];
    viewport[0]=0;
    viewport[1]=0;
    viewport[2]=width;
    viewport[3]=height;
    glGetDoublev(GL_MODELVIEW_MATRIX,view);
    glGetDoublev(GL_PROJECTION_MATRIX,proj);
    //glGetIntegerv(GL_VIEWPORT,viewport);
    gluProject((double)ox,
                (double)oy,
                (double)oz,
                view,
                proj,
                viewport,
                &x,&y,&z);

    print(x,y,z,text);







The reason glGetIntergerv(GL_VIEWPORT,viewport) is commented out is that I use four seperate viewports, and with it the labels are completely in the wrong place. By using the total size of the window size each time they match up a lot better, with the exception of the problem I describe above.

Share this post


Link to post
Share on other sites
Advertisement
I dont quite understand:
-----------------------

But firstly the reason ur GL_GETVIEWPORT doesnt work is you dont seem to understand the order that the data is returned.
Ur expecting:
viewport[0]=0;
viewport[1]=0;
viewport[2]=width;
viewport[3]=height;

But in reality OpenGL Returns something like

Left , Rigth , Bottom , Top Which translates into 0 , Width , Height , 0

And then secondly why are u using gluunproject? That is if you want to go from screen coords (2d coords) to 3d coords (using an arbitary Depth value).

In constrast you want something like glProject().
Why dont you just render the Text with the 3d objects in a second pass using the same translations and the same matrices (perspective view)?


Share this post


Link to post
Share on other sites
Well, first of all I am using glProject. I'm using it instead of drawing the text as 3D objects because in theory it should be easy enough to just draw text at a 2D location projected from a 3D point, and that's what I'd like to get working here. Second, I'm pretty sure viewport takes the values in the format I'm using.

[Edited by - Silex on January 2, 2006 1:31:13 PM]

Share this post


Link to post
Share on other sites
From what I see you are calling gluProject correctly but the problem might be in your print function or how you are handling the 2d mode and it coordinates. Also the z you get back from gluProject is not a true world z but more so a number between 0 and 1 the represents a depth like the depth buffer.

Share this post


Link to post
Share on other sites
That narrows it down...let's see, this is how I print:


glMatrixMode(GL_PROJECTION);
glPushMatrix(); //push temporary projection matrix
glLoadIdentity();
gluOrtho2D(0,width,0,height);
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); //push temporary modelview matrix
glLoadIdentity();
glTranslated(x,y,z);
glScalef(15,15,15);
glfStringCentering(GL_FALSE);
glfDrawSolidString(text);
glMatrixMode(GL_PROJECTION);
glPopMatrix(); //reset old projection matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix();



The glfDrawSolidString function just calls glTranslate a few times to place the characters in the string, so I don't think the problem is in there. Am I doing anything wrong in the code above?

Share this post


Link to post
Share on other sites
Quote:
I'm drawing 2D labels (text) that follow my 3D objects around

if u want to use it like a players name over their model in quake3 etc, then search for billboarding

Share this post


Link to post
Share on other sites
That code looks right, it might be in the glf functions that the problem hides now from what I see.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!