Get OpenGL X Y Z from any 2D Points.

Hello,

I am using below code to get 3D points from 2d points but I found that there is a dependency of mouse while using this code. if I pass 2d points manual like (10,10) then it is giving me wrong result but when i use mouse it works fine.

Does it require any other way?


    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;
 
    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);

In the case which doesn’t work:

[ul]
[li] Is the OpenGL context bound?
[/li][li] Do the model-view matrix and projection matrix have the correct values?
[/li][li] Is the viewport set correctly?
[/li][li] Does the depth buffer contain the correct data?
[/li][/ul]
It’s possible that the code path for handling a mouse click sets up the above but the other code path misses one or more of them.

Use a debugger to observe the values which are being passed to gluUnProject().

I also debug it by adding manual input values same as mouse click values.

other View-Project matrix and view ports are correct.

[QUOTE=GClements;1293294]In the case which doesn’t work:

[ul]
[li] Is the OpenGL context bound? -Yes
[/li][li] Do the model-view matrix and projection matrix have the correct values? -Yes
[/li][li] Is the viewport set correctly? -Yes
[/li][li] Does the depth buffer contain the correct data? -Yes
[/li][/ul]
It’s possible that the code path for handling a mouse click sets up the above but the other code path misses one or more of them.

Use a debugger to observe the values which are being passed to gluUnProject().[/QUOTE]

View this thread, maybe it will work for you :confused:

Select points with mouse in 2D space?