• Advertisement
Sign in to follow this  

Trying to use gluUnProject/gluProject to check if hitting objects

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

Hi,

 

I'm trying to select a object in my 3D world. I looked online a bit and u found that you can use gluUnProject, but i don't understand how it works.

I Currently have this code:

double modelMatrix[16];
double projMatrix[16];
GLint viewport[4], winZ;
    
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
glGetIntegerv(GL_VIEWPORT,viewport);

 GLdouble posX, posY, posZ;
    

         glReadPixels( x, int(y), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

        gluProject(x,y,winZ,modelMatrix,projMatrix,viewport,&posX,&posY,&posZ);
        gluUnProject(x,y,winZ,modelMatrix,projMatrix,viewport,&posX,&posY,&posZ);

X, y and Z are my mouse position

And when i check after gluProject the numbers are huge, and after gluUnproject they are between -1 and 1(screen i gues).

But the x, y and z coordinates of my objects aren't near the numbers i'm getting back. Do i need to do a nother calculation?

 

Thanks for the help in forhand.

 

Share this post


Link to post
Share on other sites
Advertisement
The use of glReadPixels is not a particularly good way of doing picking. Reading back the depth at a particular position is particularly unhelpful, even with gluUnProject; it doesn't tell you which object has been picked. Even if you had the math working, reading back textures (glRead and other means) is very inefficient. You want to avoid as much serialization and round-trips between the CPU and GPU as possible.

Graphics and picking seem like they're closely related but they're really not, unless you particularly enjoy issues like needing to be pixel-perfect with your mouse while trying to click on some tiny object on the screen.

gluProject/gluUnProject simply take a world-space coordinate and put into screen-space or vice versa. This by itself won't solve the problem, even if you give a depth as further input. At best it could give you the world-space coordinate of the pixel (surface) selected and then you have to find out which object includes that surface. That is neither easy nor efficient. An alternative approach that doesn't even need glUnProject is to render each object with a different/unique solid color (no shading or texturing), read back the pixel under the cursor, and check the color. This is still potentially very inefficient due to the required synchronization between the CPU and GPU.

A (usually) better approach is to do your own ray casting or to use an existing off-the-shelf collision/physics library to do it for you (e.g. Bullet).

Using gluUnProject to construct a ray.

Share this post


Link to post
Share on other sites

Thanks for the tip, i'm going to try it.

If i have questions i will let you know :p

Share this post


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

  • Advertisement