Depth problem with translating screen coords to world coords

Hello,

I’m pretty new to all of this stuff, so please excuse the inexperience of this question.

I am translating mouse coordinates to world coordinates using glReadPixels then gluUnProject. And it converts the coordniates nicely.

The issue is that when I’m capturing them, the mouse is dragging an object and when I move the mouse back and forth while dragging the object, the object is actually getting closer and closer to the camera (but it doesnt actually look like it, very strange). Something is happening with the depth that is far beyond my understanding. I would think that what should happen is that if I dragged the mouse a certain distance to the right and that same distance to the left, the object should be in the same place as when I started. It appears to be that way, but if I rotate the scene, it shows that the object has moved closer to the camera.

I know its probably fairly difficult to get an understanding of what exactly I’m talking about. If you want I can post the code, its relatively short.

Thanks for your help,
Bryan

Originally posted by Bryan:
[b]Hello,

I’m pretty new to all of this stuff, so please excuse the inexperience of this question.

I am translating mouse coordinates to world coordinates using glReadPixels then gluUnProject. And it converts the coordniates nicely.

The issue is that when I’m capturing them, the mouse is dragging an object and when I move the mouse back and forth while dragging the object, the object is actually getting closer and closer to the camera (but it doesnt actually look like it, very strange). Something is happening with the depth that is far beyond my understanding. I would think that what should happen is that if I dragged the mouse a certain distance to the right and that same distance to the left, the object should be in the same place as when I started. It appears to be that way, but if I rotate the scene, it shows that the object has moved closer to the camera.

I know its probably fairly difficult to get an understanding of what exactly I’m talking about. If you want I can post the code, its relatively short.

Thanks for your help,
Bryan[/b]
Hello there,
How are are making the object move close to camera and rotating it. Some of this stuff is unclear post the code.
Thanx
MMM

Well two things are happening. If you click on the background (not an object) and drag, it rotates the scene using this code:

// Effects modelview
ax = dy; // x coordinate
ay = dx; // y coordinate
az = 0.0;
angle = vlen(ax,ay,az)/(double)(viewport[2]+1)*180.0;

/* Use inverse matrix to determine local axis of rotation */

bx = _matrixInverse[0]*ax + _matrixInverse[4]*ay + _matrixInverse[8] *az;
by = _matrixInverse[1]*ax + _matrixInverse[5]*ay + _matrixInverse[9] *az;
bz = _matrixInverse[2]*ax + _matrixInverse[6]*ay + _matrixInverse[10]*az;

glRotatef(angle,bx,by,bz);

Then if you click on object and begin to drag it, this code is used:

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

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
 

Where posX, posY, posZ are the x,y,z world coords. What I want to accomplish is that when you click on the object to move it, it moves it in the x,y plane that it’s currently in. Meaning that it doesnt move further or closer to the camera. I hope this clarifies whats happening.

Thanks,
Bryan

I’m not to sure how to solve your problem but here is my problem.

Can you please help me with a problem i am experiencing with OpenGL.

I have a scaled 3d Model in OpenGL which i can rotate and translate. The problem is when i try to get the world coordinates back from this 3d world especialy when i did a rotation before hand it doesn’t seem to be correct.

I will try to explain the process with a simple flow layout

  1. Initialise OpenGL

  2. RenderView
    {
    pushmatrix
    scale model (glScale function)
    translate model (glTranslate function)
    rotate model (glRotate function)

    call displayList (Example a cube)
    popmatrix
    }

  3. Projection View ( Will happen every time the display window resize )
    {
    Setup Viewport (glViewport)
    Setup Projection Matrix (glMatrixMode(GL_Projection))
    Setup Perspective View (glPerspective)
    }

4.MouseClick Event (To get the world coordinates back)
{
get ViewPort (glGetIntegerv(GL_Viewport,Viewport))
get ProjectionMatrix (glGetDoublev (GL_PROJECTION_MATRIX, projmatrix)
get ModelMatrix (glGetDoublev( GL_MODELVIEW_MATRIX, mvmatrix))

Read pixels to get correct z position (glReadPixels)
unProject function to get world coordinates (gluUnProject) 

Translate Value got from the gluUnProject

inverse scaling

}

The mouse click gets the correct values if the model isn’t rotated. (xrotation = 0 and yrotation = 0 degrees)

As soon as you rotate the model the position values doesn’t seem to be correct.