2D Cursor Interacting with 3D Objects

Started by
1 comment, last by McCoder 15 years, 7 months ago
Let's say I have a spinning cube on screen. I would want to build a simple move routine with the mouse, so whenever I click the cube, I would select it. But how would my mouse cursor know it touched a cube?
Advertisement
There are multiple ways to do this, for example:
Mathematical: use glUnProject to project the mouse cursor from window coordinates into worldspace - since we're mapping 2D to 3D you will get a line (ray), then define each object as known geometrical shape (typically done with bouding boxes or bounding spheres), and it then becomes simple function of ray-box or ray-sphere intersection.

Color-based: when mouse button is clicked, render offscreen all the selectable objects in different colors, then use glReadPixels to read the color at the specific cordinates.
I really can't "think" of that issue what you said. I'll search some code to cover the dryness. Any idea if I could use framebuffers? I was thinking when I create an object, store it's data into a hash of vertex - screen coord. And if the mouse hits that coordinate, it would select the cube or sphere or whatever.

This topic is closed to new replies.

Advertisement