Mouse Cursor Position

Started by
9 comments, last by fmwyso 15 years, 3 months ago
Hey, I need to find the placement of my mouse in X and Y coordinates. I have a 2-d "map" if you will and am in need of just getting those x, y coords... It should be known that I'd actually rather X, Y coords in integer rather than float / double... I tried google searching but from what I've seen... A. The codes don't work. B. I don't understand them. C. I'm not using them correctly. Any help is appreciated, thanks. P.S. I am using CodeBlocks and using the default setup with NeHe's new lesson tutorial. (I'm new, so please explain things that you may assume I already know... Worst case scenario is I have to a read a few more lines which I don't mind one bit.)
Advertisement
Which mouse coords do you need. On screen (i.e. like x pixels right of the windows left border) or within the world (e.g. on the map).

If on screen, then you should have a look into your language's/systems libraries (or tell us ;) ).

If within the world, here's a short explanation.

I'm not sure how much you know OpenGL already, so here's a short summary of the transformation stages of a point from local (object) space to screen.

1a. transform by model matrix to world space
1b. transform by view matrix to eye space
2. transform by projection matrix to screen space (values in range [-1,1] for x,y and [0,1] for z)
3. use viewport information to transform into pixel coordinates.

In OpenGL model and view matrix are merged together to form the modelview matrix, so I named those steps 1a and 1b.

In order to transform from pixel coordinates to world space you have to apply steps 1b to 3 in reverse order and using the inverse matrices (i.e. instead of multiplying with the projection matrix, you multiply with the inverse projection matrix).

In a 2d game you often have orthogonal projection matrices, so unprojection is quite easy. The frustum is basically a box so just remap your values from [-1,1] to [left,right] for x, doing the same for y and z (note that z is [0,1] to [near,far]) for step 2.

Hope that helps.

Edit: If you don't understand what I wrote, you maybe should read a bit on matrix algebra in general and its use in 3D application. Among the most important keywords are projection matrix, unprojection, modelview matrix, screen space, eye space, world space and viewport.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Thanks for your detailed post and I could understand what you said (not extensively but still did understand what you meant :P).

My program is 2-d, and I have set up the scale / projection to be in the exact pixels the frame size is. I'm assuming that would mean that the mouse coords on the screen (or I should say window) would be exactly what I want...

Just so you know... My map doesn't really exist, I don't have any textures... The only thing this project will become is just basically a GUI in OpenGL. The reason for this project is to better understand some functions of OpenGL, and it has definitely gotten me more familiar with this very confusing thing :P.

Anyway, I do just need the mouse coordinates on the screen... I checked out some of the libraries but noticed they only work for the frame that calls it or something? All I know is that it wasn't working for me... Then, I tried to use ScreenToClient but I couldn't find out how to get the handle of my OpenGL window ><.

Currently, the only thing I know about Mouse Coords is that you can do the C++ function GetCursorPos(&var), but I need to find out how to get the coords in the window ><. So, could you explain how I could do that? By the way, the way my X/Y is setup is so that the origin is in the lower-left corner of my window.
So if I understand you correctly you need to get the mouse coordinates relative to your OpenGL window?

To answer this, we'd need to know your OS and programming language.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
I'm using C++ and it is for Windows.
You are half right.

This code should work:

POINT ptMouse;GetCursorPos(&ptMouse);ScreenToClient(hWnd, &ptMouse);

where hWnd is a handle to your window.

GetCursorPos
ScreenToClient
This probably sounds really stupid... but how do I get the handle to my OpenGL window? ><

Btw, I think I forgot to mention that I'm using the NeHe's new tutorial code for everything... (I'm just re-writing the draw function and a few other things).
Quote:Original post by fmwyso
This probably sounds really stupid... but how do I get the handle to my OpenGL window? ><


Are you not using Windows API to create the opengl window? If so, what are you using?
I don't THINK I'm using Windows API >,<...

Here's the code I'm using to open the window, etc.

http://nehe.gamedev.net/wiki/NewLesson1.ashx

I honestly have no clue how most of it works to be honest :P ><.
Ah, I see you're using SDL. In that case it's also quite simple to retrieve the mouse position.

Just add an SDL_MOUSEMOTION case in the event switch and use event.motion.x and .y.

int mouseX = 0, mouseY = 0;...switch(event.type)...case SDL_MOUSEMOTION:{  mouseX = event.motion.x;  mouseY = event.motion.y;  break;}...

This topic is closed to new replies.

Advertisement