Jump to content
  • Advertisement
Sign in to follow this  
prototypev

OpenGL Having problems with gluUnproject

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

Greetings to everyone, I'm having some problems with gluUnproject. First off, I have a model of a building, in which I draw, then translate, rotate, and scale to fit my requirements. (for simplicity sake, we'll just take it that the yMin = 0 and yMax = 80, scale factor = 0.2) Now I'm trying to determine the actual co-ordinate when the user clicks on a specific region within the building by using gluUnproject, however the returned co-ordinates (I'm not too concerned with the x and z for the moment) don't seem to be correct at all. Just to give an illustration, when I click at the bottom of the building, the returned y value is 5.2079742665861311, when I click at the top of the building, the returned y value is 5.2433680417046098. This is clearly wrong :( I've read that this could be due to having a wrong projection or model matrix, but I'm kind of new to OpenGL, is there a way to verify this? Help would be greatly appreciated. BTW, here is my code snippet for the gluUnproject (it's exactly the same one found at http://nehe.gamedev.net/data/articles/article.asp?article=13: CVector3 GetOGLPos(int x, int y) { 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); return CVector3(posX, posY, posZ); } [Edited by - prototypev on December 6, 2005 2:52:10 AM]

Share this post


Link to post
Share on other sites
Advertisement
it should work
to debug try printing out all the values onscreen
eg mouse x,y + zdepth (under cursor)
and print out the x,y,z result from the unproject command
move the mouse around and see whats happening

Share this post


Link to post
Share on other sites
Most likely your unprojected point is in world coordinates and you are expecting object relative coordinates. Rather than comparing the unprojected point to the vertices of the object compare it to the position of the object. Pretty much to get object relative coordinates you have to know which object it is so you can load the right modelview matrix. Depending upon what you are doing determining the right object may be all you need to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by zedzeek
it should work
to debug try printing out all the values onscreen
eg mouse x,y + zdepth (under cursor)
and print out the x,y,z result from the unproject command
move the mouse around and see whats happening


As currently theres only one object in the scene, I only tested mousing on this object. The depth value always remains the same no matter where I click, that's one (seems wierd, as the different points I clicked should have different depth values I think). Secondly, the x and y variation is very very small, as what I highlighted above.

Share this post


Link to post
Share on other sites
Quote:
Original post by LilBudyWizer
Most likely your unprojected point is in world coordinates and you are expecting object relative coordinates. Rather than comparing the unprojected point to the vertices of the object compare it to the position of the object. Pretty much to get object relative coordinates you have to know which object it is so you can load the right modelview matrix. Depending upon what you are doing determining the right object may be all you need to do.


Please pardon my newbness but does that mean I should do my translation, rotation, scaling on the modelview matrix depending on which object is being chosen, BEFORE I attempt to do gluUnProject?

Share this post


Link to post
Share on other sites
No, you would just want to apply the inverse of the translation of the object then the inverse of its orientation/rotation on the value you get when you click on it. That would give you the coordinate in object space.

Share this post


Link to post
Share on other sites
Quote:
As currently theres only one object in the scene, I only tested mousing on this object. The depth value always remains the same no matter where I click, that's one (seems wierd, as the different points I clicked should have different depth values I think). Secondly, the x and y variation is very very small, as what I highlighted above

never assume it leads to problems
do what i said + print out the 6 values every frame preferably onscreen

Share this post


Link to post
Share on other sites
I'm getting confused :(

Ok here's what I did when I rendered the object:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(obj->x, obj->y, obj->z);
glRotatef(obj->orientation, 0, 1, 0);
glScalef(obj->scale, obj->scale, obj->scale);
glPopMatrix();

So, I modified the existing function containing gluUnProject to:

GLdouble MyFunc(int mousex, int mousey) {

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

GLdouble modelview[16], projection[16];
GLfloat winZ;
GLdouble posX, posY, posZ;

glGetDoublev(GL_PROJECTION_MATRIX, projection);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef(1/obj->scale, 1/obj->scale, 1/obj->scale);
glRotatef(-obj->orientation, 0, 1, 0);
glTranslatef(-obj->x, -obj->y, -obj->z);

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

GLint winY = viewport[3] - mousey - 1;
glReadPixels(mousex, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

gluUnProject((double)mousex, (double)winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
glPopMatrix();

return posY;
}

The co-ordinates I get at the bottom and top of the object are still incorrect :(

Once again, please pardon my newbness.

Share this post


Link to post
Share on other sites
Quote:
Original post by prototypev
Quote:
Original post by LilBudyWizer
Most likely your unprojected point is in world coordinates and you are expecting object relative coordinates. Rather than comparing the unprojected point to the vertices of the object compare it to the position of the object. Pretty much to get object relative coordinates you have to know which object it is so you can load the right modelview matrix. Depending upon what you are doing determining the right object may be all you need to do.


Please pardon my newbness but does that mean I should do my translation, rotation, scaling on the modelview matrix depending on which object is being chosen, BEFORE I attempt to do gluUnProject?


Actually, I think the answer to this is yes. I was messing around with gluUnproject a while back, and I was getting wrong coordinate values returned. It turned out that I needed to be using the projection/modelview matrix that resulted from my call to gluLookAt (or in your case, the series of translates, rotates, and scales).

Share this post


Link to post
Share on other sites
No, you don't want to invert the modelview matrix. The stuff about inversion is an optimization and you aren't to that point yet. You just need to use the same modelview matrix you used to draw the object. Once you get multiple objects you won't know which modelview matrix is the correct one. So you do all of them. You use the modelview matrix used to draw an object and check the unprojected point against that object. Once you have that working then you can worry about using the inverse of the object to world transform to avoid repeating the screen to world transform. Optimization comes last. Once you are doing everything you have to do then you worry about doing it faster.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!