Sign in to follow this  
McCoder

2D Cursor Interacting with 3D Objects

Recommended Posts

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?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this