Drawing on an object.

I’m importing an object file, let’s say it’s a plane for example. I want to be able to draw on the plane and I just can’t seem to figure out how it’s done. It’s fairly trivial to know the x and y coordinates of the mouse click but what about the z coordinate? I need to be able to do some sort of ray tracing that shoots out a ray and tells me as soon as it reaches the object. What I’m trying to know is the z coordinate of the click which is on the object. That there are no other objects in my space is a good assumption for now.

Transform the ray to object space coordinates for each object and test all triangles of the object for a intersection with the ray.
There are many possibilities to make this more efficient, some are:

  • test the ray (in world coordinates) against the world space bounding volumes of the object
  • use a hierarchical data structure on top of the object’s bounding volumes to discard whole groups of objects
  • if each object has a large number of triangles, use a hierarchical data structure for each object

A different approach is to read the depth buffer at the mouse position and transform the value from there into whatever coordinate system you need (probably world coordinates?). If you have multiple objects you won’t know which one was hit though. To get that information you can assign each object a unique color and render all objects in their assigned color (lighting off) into the back buffer (or an FBO). In addition to the depth buffer you also read back the color at the mouse position which then identifies the object as well.