Sign in to follow this  
prototypev

OpenGL Having problems with gluUnproject

Recommended Posts

prototypev    122
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
zedzeek    528
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
LilBudyWizer    491
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
prototypev    122
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
prototypev    122
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
smitty1276    560
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
zedzeek    528
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
prototypev    122
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
Nit    533
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
LilBudyWizer    491
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
crslimin    122
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
prototypev    122
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
prototypev    122
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
LilBudyWizer    491
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
prototypev    122
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
prototypev    122
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
prototypev    122
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
dimebolt    440
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
Lopez    122
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  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now