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 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 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 on other sites
Quote:
 Original post by zedzeekit should workto debug try printing out all the values onscreeneg mouse x,y + zdepth (under cursor)and print out the x,y,z result from the unproject commandmove 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 on other sites
Quote:
 Original post by LilBudyWizerMost 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 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 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 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 on other sites
Quote:
Original post by prototypev
Quote:
 Original post by LilBudyWizerMost 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 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 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 on other sites
Quote:
 Original post by crsliminHi 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 on other sites
Quote:
 Original post by LilBudyWizerNo, 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 on other sites
You're still inverting the individual transforms. It should be exactly the same as was used to draw the object.

// should beglTranslatef(obj->x, obj->y, obj->z);glRotatef(obj->orientation, 0, 1, 0);glScalef(obj->scale, obj->scale, obj->scale);// and notglTranslatef(-obj->x, -obj->y, -obj->z);glRotatef(-obj->orientation, 0, 1, 0);glScalef(1/obj->scale, 1/obj->scale, 1/obj->scale);

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 on other sites
why havent u done what i said?

Share on other sites
Put your GetOGLPos(int x, int y), just after rendering in OnPaint() to check the result.

Share on other sites
Quote:
 Original post by zedzeekwhy 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 on other sites
Quote:
 Original post by crsliminPut 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 on other sites
Quote:
Original post by prototypev
Quote:
 Original post by zedzeekwhy 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 on other sites
right, here's how to do it.

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.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627676
• Total Posts
2978582
• Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 12
• 10
• 12
• 22