Question

I would like to display a 2D image at a 2D point calculated from a 3D point using gluProject(). So I have my 3D point, I use gluProject to get its 2D coordinates, then I display my image at this point. It works well but I have a problem with Z coordinate which makes my image appear two times on the screen : where it should really appear and at "the opposite".

Let's take an example : the camera is at (0,0,0) and I look at (0,0,-1) so in direction of negative Z.

I use 3D point (0,0,-1) for my object, gluProject gives me as 2D point the center of my window which is the good point.

So when I look in direction of (0,0,-1) my 2D image appears, when I rotate, it moves well until the point (0,0,-1) is not visible, which makes the 2D image go out of screen so not displayed.

But when I look at (0,0,1), it also appears. Consequently, I get the same result (for the display of my 2D image) if I use 3D point (0,0,-1) and (0,0,1) for example. I assume there is something to do with the Z coordinate that gluProject returns but I don't know what.

Here is my code : my zNear=0.1 and zFar=1000

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble viewVector[3];
    GLdouble projection[16];

    GLdouble winX, winY, winZ;//2D point

    GLdouble posX, posY, posZ;//3D point
    posX=0.0;
    posY=0.0;
    posZ=-1.0;//the display is the same if posZ=1 which should not be the case

    //get the matrices
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );

    viewVector[0]=modelview[8];
    viewVector[1]=modelview[9];
    viewVector[2]=modelview[10];

    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );

    int res=gluProject(posX,posY,posZ,modelview,projection,viewport,&winX,&winY,&winZ);

    if(viewVector[0]*posX+viewVector[1]*posY+viewVector[2]*posZ<0){
            displayMyImageAt(winX,windowHeight-winY);
    }

So, what do I need to do to get the good display of my 2D image, that's to say to take Z into account?

Was it helpful?

Solution

gluProject works correctly, you projection matrix projects points on the screen plane, you should check whether point is behind, you can achieve this by calculating dot product of your view vector and vector to point, if it is less then 0 then point is behind.

OTHER TIPS

what is your DisplayImageAt function?

is it similar to this display function given in my code? I am trying to as well get 2D coordinate of a point selected 3d coordinates.

here is my display pretty much all.. `void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glMultMatrixd(_matrix);

glColor3f(0.5,0.5,0.5);
glPushMatrix();                                                 //draw terrain
glColor3f(0.7,0.7,0.7);
glBegin(GL_QUADS);
    glVertex3f(-3,-0.85,3);
    glVertex3f(3,-0.85,3);
    glVertex3f(3,-0.85,-3);
    glVertex3f(-3,-0.85,-3);
glEnd();
glPopMatrix();

glPushMatrix();

myDefMesh.glDraw(meshModel);

glPopMatrix();

glutSwapBuffers();

}'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top