Sign in to follow this  
prototypev

OpenGL Having problems with gluUnproject

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
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
Hi prototypev, I had the same problem before and solved now. Please try to put your MyFunc(int mousex, int mousey) after rendering, ie. after transformation and glVetex3f() etc. It shold work.

Share this post


Link to post
Share on other sites
Quote:
Original post by crslimin
Hi prototypev, I had the same problem before and solved now. Please try to put your MyFunc(int mousex, int mousey) after rendering, ie. after transformation and glVetex3f() etc. It shold work.


the MyFunc is called after rendering :(

Share this post


Link to post
Share on other sites
Quote:
Original post by LilBudyWizer
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.


Yes I am not worried about optimization yet, however, I can't seem to get it right even though I understand what needs to be done. What I did was instead of:

...
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();
...

I modified it to:

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

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();
...

but the values are still totally wrong. 0.1612xxx at the bottom of the object, and also 0.1647xxx at the top, definitely not right :(

Share this post


Link to post
Share on other sites
You're still inverting the individual transforms. It should be exactly the same as was used to draw the object.


// should be

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

// and not

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






Share this post


Link to post
Share on other sites
Still does not work (sigh~). Values I get now are: 110.76435311511121 at the bottom (when it should be close to 0), and 112.27078247647036 at the top (when it should be close to 80).

(On the verge of a breakdown ><)

Share this post


Link to post
Share on other sites
Quote:
Original post by zedzeek
why havent u done what i said?


I am doing that, I have a debugging text to show the x,y,z in window co-ords as well as x,y,z in object co-ords (after gluUnProject). But your suggestion isn't really helping because like I mentioned, the values I'm getting are wrong :)

Share this post


Link to post
Share on other sites
Quote:
Original post by crslimin
Put your GetOGLPos(int x, int y), just after rendering in OnPaint() to check the result.


MyFunc is already called after all rendering is done, as mentioned above. :/

Share this post


Link to post
Share on other sites
Quote:
Original post by prototypev
Quote:
Original post by zedzeek
why havent u done what i said?


I am doing that, I have a debugging text to show the x,y,z in window co-ords as well as x,y,z in object co-ords (after gluUnProject). But your suggestion isn't really helping because like I mentioned, the values I'm getting are wrong :)


In defense of zedzeek: He wants you to print the mouse x, y and associated z (depth). You never told us that you printed those values or that they are wrong. The only values you give us are the return values of the function.

Since the consensus is that function looks fine, your first assumption should be that at least one of your input values (mouse x,y, projection, modelview and viewport) is incorrect. Therfore, make absolutely sure that the modelview matrix you use is identical to the modelview matrix of your object. This can be done by calling glGetFloatv(GL_MODELVIEW_MATRIX, matrix), both in the render function and in the GetOGLPos function and comparing the contents of the two 16 float arrays. If they differ this might be caused by the fact that you don't load the view matrix before the 3 object transforms (it might still be on the stack, but there is no way for us to tell). A less redundant and therefore safer option would be to just store the array you get with glGetFloatv while rendering the object and pass that array directly in the gluUnProject function. Similar tricks should be done for the viewport and projection if those ever change in your application.
If your mouse x, y values are wrong check the parameters of the function. If depth is wrong, you should check for errors with
glGetError
. In fact, you should probably call glGetError at the end of the function anyhow. Just to make sure nothing went wrong.

Tom

Share this post


Link to post
Share on other sites
right, here's how to do it.

1. setup your camera.
2. push matrix.
3. rotate, scale, translate.
4. render.
5. pop matrix.

6. do your gluUnproject() before glClear().

The best way to debug gluUnprojcet, is to render somethin at the coordinates returned coordinates, eg. a sphere.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this