Mouse coordinates in a 2D-ish system [SOLVED]
I'm creating a visual designer in OpenGL in C# and the Windows environment. The designer hosts 2-D rectangular sprites. I need to do hit testing on the sprites with the client mouse coordinates. I've found plenty of examples on how to do picking in 3-D space, but this doesn't seem to quite fit that.
There are two things that make it difficult: the canvas can be zoomed in or out and the sprites can be rotated about their centers. I know I'll need to take the projection and modelview matrices into account for this. All the examples use gluUnProject to accomplish this. I don't need to check which object is directly below my mouse cursor, though; I can hit-test them in reverse draw order to determine which one was in front.
Am I making any sense? If you have any advice, please let me know.
[Edited by - DaWanderer on March 3, 2006 12:57:41 AM]
gluUnproject doesn't tell you what object is directly below your mouse cursor, it only gives you a point along a ray extending from the near Z plane to the far Z plane.
You should be able to use gluUnproject with the near Z set to 0.0f and the far Z set to 1.0f (the Z coordinate is normalized) to create this ray. Then you can iterate through your list in reverse order, as you mentioned.
You should be able to use gluUnproject with the near Z set to 0.0f and the far Z set to 1.0f (the Z coordinate is normalized) to create this ray. Then you can iterate through your list in reverse order, as you mentioned.
Here's the way I use it:
If you use glOrtho view mode, you should go:
That will ensure you have your rectangles aligned with Windows coordinate system, where (0,0) is Top Left.
1. Acquire mouse position either via message loop or ::GetCursorPos(LPPOINT Point)
2. Adjust mouse coordinates by using ::ScreenToClient(GetActiveWindow(), &Point);
Voila! You get your proper transition from Windows to OpenGL. Now you can do the testing such as PointInRect supplying rectangle of your sprite.
If you use glOrtho view mode, you should go:
glOrtho(0, uiWindowWidth, uiWindowHeight, 0, 0, 1);
That will ensure you have your rectangles aligned with Windows coordinate system, where (0,0) is Top Left.
1. Acquire mouse position either via message loop or ::GetCursorPos(LPPOINT Point)
2. Adjust mouse coordinates by using ::ScreenToClient(GetActiveWindow(), &Point);
Voila! You get your proper transition from Windows to OpenGL. Now you can do the testing such as PointInRect supplying rectangle of your sprite.
Well, I feel pretty dumb. I was calling glLoadIdentity in my rendering function (per NeHe's tutorial) and wiping out my glOrtho call. Thanks for all your help!
[Edited by - DaWanderer on March 2, 2006 11:46:27 PM]
[Edited by - DaWanderer on March 2, 2006 11:46:27 PM]
Quote:Original post by DaWandererglOrtho creates a projection matrix, and should therefore only be called when the current matrix is set to GL_PROJECTION. This way when you call glLoadIdentity on the GL_MODELVIEW matrix, your projection won't be affected.
...
I was calling glLoadIdentity in my rendering function (per NeHe's tutorial) and wiping out my glOrtho call.
...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement