View Full Version : Trouble using GetCursorPos(); with OpenGL

11-07-2010, 05:12 AM
I am doing a small 2D game as a school project and as the newbie I am I got stuck when trying to make the character look at the current cursor position.

Right now I am just trying to draw a quad at the cursor position to make sure that it will give the right value later.

The relevant code looks like this:


glLoadIdentity(); // Reset The Current Modelview Matrix
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-0.03f,0.03f,0.0f); // Top Left
glVertex3f(0.03f,0.03f,0.0f); // Top Right
glVertex3f(0.03f,-0.03f,0.0f); // Bottom Right
glVertex3f(-0.03f,-0.03f,0.0f); // Bottom Left

The program draws a quad that reacts to the mouse, however it is drawn in the wrong place and its reaction to mouse movement is extremely jumpy. (Moving the mouse a little wont do anything but once it comes outside a "zone" the quad jumps what looks like several units)

I have tried to correct this manually by dividing and subtracting the value x and y to make the quad fit under the cursor, however this does not make them less jumpy.

So to my question: Is there a better way to do this or am I doing something horribly wrong with the current method?

11-07-2010, 07:45 AM
Windows use top-left origin coordinates whereas opengl used bottom left as default.
You should deal with this.

11-07-2010, 07:58 AM
Windows use top-left origin coordinates whereas opengl used bottom left as default.
You should deal with this.

Thank you for a quick response.
That explains why it does not draw itself on the right spot this seems to be easily fixed, but it does not explain why it moves so jerky.

EDIT: Using ScreenToClient(); and then moving cp.x to an GLfloat solved the jerky movement.

The Quad is still being drawn in the wrong place, correcting this in windowed mode makes it worse when in full screen and the other way around. Is there any way to make sure that the position value is always relative to the size of the window?

11-07-2010, 09:51 AM
Check your viewport (glViewport) and your orthographic (glOrtho) view, they should match your window size.

11-07-2010, 10:28 AM
This function runs every time the window resizes.


From what I understand it will keep viewport updated with the latest window size.

I have gone by fine without glOrtho so far, is this the time to start using it?

11-09-2010, 12:53 AM
The simplest way to draw at cursor position is to use a projection matrix that match your window size. You can deal with it with another projection and view, but you'll have to transform from and to mouse coordinates and modelview coordinates.