Jump to content
  • Advertisement
Sign in to follow this  
DaWanderer

OpenGL Mouse coordinates in a 2D-ish system [SOLVED]

This topic is 4523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Here's the way I use it:

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by DaWanderer
...
I was calling glLoadIdentity in my rendering function (per NeHe's tutorial) and wiping out my glOrtho call.
...
glOrtho 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!