🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

mouse coordinate -> triangle

Started by
0 comments, last by jadawin72 22 years, 9 months ago
Hallo, maybe it''s a stupid question, but my mathematical knowledge is kind of poor: How do I calculate the triangle corresponding to the cursor-position, or even better - the correct coordinate in 3d space on the surface of the triangle? thnx a lot JF
Advertisement
It sounds like you''re trying to "pick" the 3D triangle that is drawn under the screen space cursor.

Its not a stupid question. Actually, it can be tricky. There are a few ways to do it, and I describe two here. (Actually, both ways are the same, but in the first way you are taking advantage of your graphics API and in the other way you are not.)

One way is to use the picking mechanism of your 3D graphics API. For example, OpenGL has a selection mode that can be used to give you a list of all objects drawn underneath the cursor point. Your code has to be written in a special way, but this can be used to find the correct triangle without doing any special math at all. It won''t directly give you the point that you picked, though. This method is quite fast.

The brute force way is to recognize that you have a series of transformations that map the 3D space of the world into your screen pixels. The model/view matrix (OpenGL terminology) merely places objects (triangles, points, lines) in the world at their correct location relative to the camera. The projection matrix adjusts the vertices of the objects to account for parallel or perspective projection (so that if there are two objects the same exact size, the one further away looks smaller than the one closer to the camera). The viewport transformation matrix takes the projection corrected vertices and turns them into pixel coordinates. So, to find out if you touched a triangle, you convert its vertices into screen pixels by doing the 3 transformations. Then see if the pixel touched by the cursor lies within the interior of the triangle (there are a couple of ways to do this---see below). If the cursor pixel does touch the triangle, you can find the barycentric coordinates of the cursor pixel within the triangle (do a search, I won''t talk about the barycentric coordinates here) in screen space. The barycentric coordinates are like parametric coordinates that can be used to represent any 3D point on the triangle. Once you have the barycentric coords, you can apply them to the original 3D space coordinates of the triangle to find the picked 3D point on the triangle. (caveat, the screen space barycentric coords will work for parallel and traditional perspective projection in computer graphics, but if you were doing a spherical projection such as those used for visiondome displays----see www.elumens.com----then the screen space barycentric coords might not be the same as world space barycentric coords. I don''t mean to confuse you, so just ignore this!)

The problem with the brute force approach is twofold. First, you have to worry about sorting triangles and finding the closest one, since the algorithm may tell you that *many* triangles are touched (some hidden behind others). Second, if you''re running on a video board that does hardware T&L, you''ll actually be duplicating calculations that were already done by the graphics board. I would say that a hybrid approach may be better. Use the graphics API selection mode to find the triangle, then once you have it use the barycentric coords part of the brute force approach.

Two ways to see if a point in screen space is within a triangle in screen space. First way, count how many times a horizontal line drawn from the cursor to infinite towards the right side (or left, but not both---looking for a semi-infinite line) of the screen crosses the edge of the triangle. It if crosses an even number of times, the point is outside the triangle. If it crosses an odd number of times, the point is inside the triangle. Works for convex and concave triangles. The second way involves calculating the "winding number" of the point. Do a web search for this approach.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement