Getting Mouse Coordinates

What’s the easiest way to get mouse coordinates (and change them as nessecary for my 3d game) without using GLUT? I’m using windows.h and I can’t find anything about it. Thanks

Hi !

I am not sure what this has to do with OpenGL, but normally you get the mouse coordinates from the WM_MOUSEMOVE message (and the WM_L/RBUTTONUP/DOWN).

Mikael

Getting mouse coordinates can be done using the WM_ messages, but changing them is not as easy. You can use SetCursorPos(), but it takes screen coordinates (not window coordinates), so you need to figure out the window position too if you want to move the mouse cursor in window coordinates. You can use the WM_MOVE message to figure out where the window is.

…better yet: use GLFW , which does this for you. Or, you can just check out the source for GLFW to see how it’s done.

Hi !

You can use ScreenToClient() and ClientToScreen() to convert mouse coordinates to client area coordinates.

Mikael

I’m going to look into all of these. My problem is I need it so that the mouse can move in any direction indefinatly. How do I access “WM_messages” or whatever? I’m gonna look into GLFW right now

glfw works great! It’s my new favorite library!

Originally posted by mikael_aronsson:
You can use ScreenToClient() and ClientToScreen() to convert mouse coordinates to client area coordinates.

Thanks Mikael! I will use that from now on

glfw doesn’t seem to be working. For example after I have initialized it I run the command glfwGetMousePos(&mouseX, &mouseY); and through debugging I have found that mouseX and mouseY always get assigned to 0 for some reason. It seems as though glfw is just ignoring me or something lol. Any ideas?

Originally posted by 31337:
glfw doesn’t seem to be working. For example after I have initialized it I run the command glfwGetMousePos(&mouseX, &mouseY); and through debugging I have found that mouseX and mouseY always get assigned to 0 for some reason. It seems as though glfw is just ignoring me or something lol. Any ideas?

You need to:

  1. Open a window
  2. Call glfwPollEvents or glfwSwapBuffers every time you want fresh mouse coords

Hope this helps. If not, you’ve found a bug.

/Marcus

does it need to be a glfw window or can it be a windows.h window?

Originally posted by 31337:
does it need to be a glfw window or can it be a windows.h window?

It must be a GLFW window. Check the manuals - they should hopefully make things more clear. In the source distribution there are also program examples that you can have a look at.

well looks like I’m going to do some experimentation and then rewrite the base code with glfw then. Thanks for your help

Don’t forget to download the newly released GLFW v2.2. You get joystick support, among other things