Jump to content
  • Advertisement
SR D

raycasting to select a triangle on a mesh

Recommended Posts

Posted (edited)

Hi,

I am trying to do ray casting with Ogre for the purpose of selecting a triangle on a mesh. I have this question.

As I understand it, ray casting is drawing a line from the mouse on the screen into the center of the model. The ray is tested first against a bounding box to find the mesh. However, I have 3 mesh objects, one inside the other and each would have it's own bounding box.

So how would I know which mesh to look at for where the ray intersects with which triangle?

 

Edited by SR D
accuracy

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)
Quote

As I understand it, ray casting is drawing a line from the mouse on the screen into the center of the model.

You cast a ray from the camera's eye position into the direction of the ray in the view frustum based on the mouse's location. (An easy way to do this is to project the point onto the near plane and using inverse World * View * Projection. Note not the World per object... only if you have global World matrix, otherwise just View * Projection) 

The ray is then intersected with bounding volumes to quickly cull a lot of geometry not in the bounding volume. If you have overlapping geometry, then their boxes overlap. You will then need to do a ray-triangle cast on the geometry in the bounding volume and determine the hit point you want (usually the closest hit point .e.g. origin + t * direction for minimum t (but not negative t))... but in any case the ray may intersect multiple triangles.

If you have a lot of triangles in your single bounding volume, it may be beneficial to create a bounding volume hierarchy if you're going to do a lot of ray casts (in order to speed it up.)

Alternatively if you don't really care about the triangle itself, but you care only about the object and you have trouble with the math... an easy 'solution' is to render a 1x1 viewport at the mouse location and render each object with a unique color. Then you can read back that color and you know which object was closest to the viewer. (The downside to this is that you need to use the GPU a little and you need to read back which causes a sync point. But in practice this is a fairly easy method for things like editors and the like.)

Edited by deadc0deh

Share this post


Link to post
Share on other sites
On 6/2/2019 at 10:12 PM, SR D said:

However, I have 3 mesh objects, one inside the other and each would have it's own bounding box.

You generaly omit only the uninterstected bounding volumes, and all intersected bounding volumes must be tested, you cannot even decide upon order of hit if they overlapp, only  unoverlapping volumes can be omitted if you are after the closest triangle.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!