OpenGL Unproject Mouse Coordinates SDL using OpenGL

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Alright, up front I'll admit I'm a noob, and just began using OpenGl as well as SDL. Right now I'm trying to Unproject the SDL mouse coordinates from screen space to view space in OpenGL for a first person camera based off mouse input. I was wondering if there are any tutorials or any pieces of info anyone can give me to help me with this. Thanks, sorry for being so noobish.

Share on other sites
hey man
basically, if you're just wanting a ray in the direction of the mouse click, then its pretty simple using gluUnProject.
below is a simple usage to get the ray
GLdouble ray_x, ray_y, ray_z;GLint viewport[4];GLdouble proj[16];GLdouble modelview[16];// we query OpenGL for the necessary matrices etc.glGetIntegerv(GL_VIEWPORT, viewport);glGetDoublev(GL_PROJECTION_MATRIX, proj);glGetDoublev(GL_MODELVIEW_MATRIX, modelview);// assuming you have mouse coordinates as   mouseX and mouseY// gluUnproject assumes coordinates are measured from bottom of screen// so we invert the mouseY you got from glut or SDL or wateverGLdouble _mouseY = viewport[3] - mouseY;// using 1.0 for winZ gives u a raygluUnProject(mouseX, _mouseY, 1.0f, modelview, proj, viewport, &ray_x, &ray_y, &ray_z);

and there you have the ray direction coords... it isn't normalized tho fyi.

i think you can actually get the z coordinate of the object you clicked.. but i haven't needed that yet... then you'd pass in something else as winZ, getting it from the Z-buffer... but not too sure.

hope that helped.

Share on other sites
If you just want the 3d coordinates of the point in space the mouse is over, the following code will give you just that:

GLdouble model_view[16];glGetDoublev(GL_MODELVIEW_MATRIX, model_view);GLdouble projection[16];glGetDoublev(GL_PROJECTION_MATRIX, projection);GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport);double dx; double dy; double dz;GLfloat depth[2];	 glReadPixels (x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth); gluUnProject(x, y, depth[0], model_view, projection, viewport, &dx, &dy, &dz);x3d = float(dx);y3d = float(dy);z3d = float(dz);

Rgds,
Mitch

Share on other sites
Hey guys, I just wanted to thank you for the help and examples. They worked really well. Thanks.

Share on other sites
does that not also require that the data is still in the depth buffer? ie, u haven't just cleared everything? coz I tried that, and it didn't work, so I assumed its because i was doing this in my logic function which happens after I clear the video and depth buffers.

Share on other sites
Quote:
 Original post by rewolferdoes that not also require that the data is still in the depth buffer? ie, u haven't just cleared everything? coz I tried that, and it didn't work, so I assumed its because i was doing this in my logic function which happens after I clear the video and depth buffers.

Yes, it needs the data in the depth buffer to work properly.
Why not perform your game logic while the last frame is still in the buffer, and then clear it? From the sounds of things, if you had a delay in your logic section, you could end up with a blank screen for a while. :)

Share on other sites
haha...
that may be a good point actually lol!! ill shift the clear between logic and render... no idea why it was at the top..
thanks man.
my unproject works fine as it is anyways tho :)

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×