Thursday, December 2, 2010

Picking Objects in an OpenGL scene

Ok this one's a bit technical.

Picking is the ability to select an object with a mouse or on the screen. Up to now, the picking system worked flawlessly but picking now has to be upgraded from OpenGL to OpenGL ES2 if I'm to run on the Internet using Native Client. In OpenGL, picking was handled automatically with a simple function that OpenGL use to show me index number of the object but now it's not there anymore. So here is how I'm going to do it. Instead of figuring out the an imaginary ray in 3d space that traverses all the objects under the cursor, a complicated scheme, I'll do with coloring the objects. It's a lot easier to program. Every second of time, I recreate every object in the scene more than 30 times a second, that's a lot of stuff happening really fast. Since I tell the system what color each object is going to be anyway, then on the frame that is sent when the user clicks, I will color each object with a unique color. Then after the scene is drawn, I'll get a picture from the graphics card of the scene, then look at the X and Y position of the dot under the click of the mouse and using a pre-created list matching color to object, I'll know what object has been selected by using the color as the index into the list. Voila object picking. Well, that's the theoretical side anyway, now I have to figure out the code that makes it work.

You know, it's almost like I know what I'm doing.

No comments:

Post a Comment