Skip to main content
GameDev.net gamedev.net
🔒 Locked

Unproject Mouse Coordinates SDL using OpenGL

Started by DigiMan Shart Sep 18, 2008 at 10:19 PM 5 replies 5.6k views
Original Post
DigiMan Shart
DigiMan Shart
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.
Alabama Man!!!
rewolfer
rewolfer
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.
bobmitch
bobmitch
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
DigiMan Shart
DigiMan Shart
Hey guys, I just wanted to thank you for the help and examples. They worked really well. Thanks.
Alabama Man!!!
rewolfer
rewolfer
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.
bobmitch
bobmitch
Quote:
Original post by rewolfer
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.


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. :)
rewolfer
rewolfer
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 :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.