Jump to content

  • Log In with Google      Sign In   
  • Create Account


Trying to use gluUnProject/gluProject to check if hitting objects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 sothro   Members   -  Reputation: 145

Like
0Likes
Like

Posted 05 May 2014 - 02:34 PM

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.

 



Sponsor:

#2 SeanMiddleditch   Members   -  Reputation: 3868

Like
2Likes
Like

Posted 05 May 2014 - 11:00 PM

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.

#3 sothro   Members   -  Reputation: 145

Like
0Likes
Like

Posted 06 May 2014 - 02:36 PM

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

If i have questions i will let you know :P






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS