Jump to content
  • Advertisement
Sign in to follow this  
DigiMan Shart

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 watever
GLdouble _mouseY = viewport[3] - mouseY;

// using 1.0 for winZ gives u a ray
gluUnProject(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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!