Picking and selection... - Printable Version +- iDevGames Forums ( http://www.idevgames.com/forums)+-- Forum: Development Zone ( /forum-3.html)+--- Forum: Graphics & Audio Programming ( /forum-9.html)+--- Thread: Picking and selection... ( /thread-2695.html) |

Picking and selection... - chalamov33 - Mar 30, 2008 08:03 PM
http://www.opengl.org/resources/faq/technical/selection.htm What I try to do it's to generate an event when I click on object in my 3D univers Someone can give me an example using this approach with opengl and win 32 api? I will really appreciate! ///////////////////////////////////////////////////////////////////// Another method is to generate the ray in eye coordinates, and transform it by the inverse of the ModelView matrix. In eye coordinates, the pick ray origin is simply (0, 0, 0). You can build the pick ray vector from the perspective projection parameters, for example, by setting up your perspective projection this way aspect = double(window_width)/double(window_height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glFrustum(-near_height * aspect, near_height * aspect, -near_height, near_height, zNear, zFar ); you can build your pick ray vector like this: int window_y = (window_height - mouse_y) - window_height/2; double norm_y = double(window_y)/double(window_height/2); int window_x = mouse_x - window_width/2; double norm_x = double(window_x)/double(window_width/2); (Note that most window systems place the mouse coordinate origin in the upper left of the window instead of the lower left. That's why window_y is calculated the way it is in the above code. When using a glViewport() that doesn't match the window height, the viewport height and viewport Y are used to determine the values for window_y and norm_y.) The variables norm_x and norm_y are scaled between -1.0 and 1.0. Use them to find the mouse location on your zNear clipping plane like so: float y = near_height * norm_y; float x = near_height * aspect * norm_x; Now your pick ray vector is (x, y, -zNear). To transform this eye coordinate pick ray into object coordinates, multiply it by the inverse of the ModelView matrix in use when the scene was rendered. When performing this multiplication, remember that the pick ray is made up of a vector and a point, and that vectors and points transform differently. You can translate and rotate points, but vectors only rotate. The way to guarantee that this is working correctly is to define your point and vector as four-element arrays, as the following pseudo-code shows: float ray_pnt[4] = {0.f, 0.f, 0.f, 1.f}; float ray_vec[4] = {x, y, -near_distance, 0.f}; The one and zero in the last element determines whether an array transforms as a point or a vector when multiplied by the inverse of the ModelView matrix. //////////////////////////////////////////// Picking and selection... - TomorrowPlusX - Mar 31, 2008 07:07 AM
You won't find much help here since this is a Macintosh forum, but we do OpenGL here so there's a bridge. In my work I do picking by means colliding pick-rays with geometry. I'm using ODE for my physics and collision detection so it's really easy. I've also done picking -- really crudely I might add -- by rendering the scene into the backbuffer with each "pickable" object rendering a unique color. Think of the color as a unique ID for the object. Then I use glReadPixels to grab the single pixel corresponding to the mouse point. I get the color out, convert it back to an ID, and check against a table which maps the IDs to objects in my graph. It works really well, and is easy to implement. That being said, I never grokked OpenGL's built-in picking. Picking and selection... - wyrmmage - Mar 31, 2008 02:08 PM
sorry about my post last night; I was tired Here's some code that uses glutPicking that I obtained from somewhere back when I was writing my first game: Code: `int clientDLL::pickClosest(anim* theAnims[], int numOfAnims)` where theAnims is an array of objects that contains a method to dray polygons in OpenGL. Again, sorry about my previous post; hope this helps -wyrmmage |