Perspective Projection:detect if object is selected

Started by
0 comments, last by Zakwayda 13 years, 11 months ago
Hi everyone. I have a cube drawn in 3D using perspective projection. I wold like to detect if user's cursor is upon that object when user press a mouse button. Camera is moving freely and can be pointed at the object in any way. My idea is as follows: When a user press a button we have a coordinates x,y on the window rectangle relatively to any point of the window,let it be upper left.Then knowing the point C,V coordinated,view angle, distances to near and far planes we can calculate coordinates of point A. From cursor coordinates and window height and width we can get the ratio in which point A divides the screen,the same proportion will we for the near plane. When we know coordinates of A point we can define a line equation using point C and A. So now we can check if the line goes through the cube. I draw a picture to make myself clear. click me I started solving mathematical equations and though maybe OpenGL has any built in features that can help me. For example gluPerspective function is a wrapper round glFrustum so it somehow calculates the coordinates of the near plane.Maybe that can help me? Thanks in advance.
Advertisement
This is called 'picking'. There are quite a few previous threads on the subject here on GDNet, and you can find info elsewhere online about it as well. (Try searching the forum archives and/or the internet in general for 'picking' or 'opengl picking' and see what you find.)

Sometimes picking in OpenGL is done using the 'feedback' mechanism, but I generally recommend doing it geometrically, as you're trying to do. The usual method for generating a picking segment is to unproject the mouse position twice, once at the near plane and once at the far plane, to yield the endpoints of the segment. (gluUnproject() can be used for this.) Again, if you look around a bit you should be able to find some examples of how to do this.

This topic is closed to new replies.

Advertisement