View Full Version : Mouse Cursor Position in OpenGL coordinates

11-30-2011, 11:00 AM
Is there an easy way to get the current mouse position in OpenGL coordinates without using GLUT?

I'm using an ortho projection and everyting is drawn at -1 so the Z coordinate doesn't really matter to me, but if I could somehow read the mouse position in opengl coordinates that would be super helpful in moving forward.

11-30-2011, 12:35 PM
"without using GLUT" -> and using what instead ? GL alone does not handle a mouse.

Manel Goucha
11-30-2011, 12:35 PM
You can do this by math calc or by using the glReadPixels

Vector2 ScreenToGlobal(Integer X; Integer Y);
viewport: array [1..4] of Integer;
modelview: array [1..16] of Double;
projection: array [1..16] of Double;
Point: array [0..2] of Double;
winZ: Double;
glGetDoublev(GL_MODELVIEW_MATRIX, @modelview);
glGetDoublev(GL_PROJECTION_MATRIX, @projection);
glGetIntegerv(GL_VIEWPORT, @viewport);

if (Y == 0) {Y := 1};

glReadPixels(X, -Y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, @winZ );
gluUnProject(X, viewport[4]-Y, winZ, @modelview, @projection, @viewport, Point[0], Point[1], Point[2]);

Result[0] = Point[0];
Result[1] = Point[1];

using math, you need to know what's your zoom value, your current camera position and your mouse position

11-30-2011, 01:05 PM
hmmm...I for some reason thought gluUnProject was GLUT...apparently its not...I seem to have gluUnProject. I've found plenty of examples of gluUnproject...I guess I just need to know now how to get my mouse cursor position in screen coordinates.

11-30-2011, 01:19 PM
Actually, I figured it out...thanks!