# Thread: Mouse Cursor Position in OpenGL coordinates

1. ## Mouse Cursor Position in OpenGL coordinates

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.

2. ## Re: Mouse Cursor Position in OpenGL coordinates

"without using GLUT" -> and using what instead ? GL alone does not handle a mouse.

3. ## Re: Mouse Cursor Position in OpenGL coordinates

You can do this by math calc or by using the glReadPixels

Code :
```Vector2 ScreenToGlobal(Integer X; Integer Y);
{
var
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

4. ## Re: Mouse Cursor Position in OpenGL coordinates

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.

5. ## Re: Mouse Cursor Position in OpenGL coordinates

Actually, I figured it out...thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•