Overview In order to figure out whether your cursor is hitting any of the 3D objects in your scene, you need to do the following:Transforming to the perspective vertex view The trick to making all the following steps easier is to transform the entire scene using the same perspective transformation that is used by the vertex shader. For any given vertex, first we transform by the object's global transformation, and then we apply the perspective transform, remembering to then divide through by the homogeneous coordinate w:Looping through the triangles of a triangle meshPosition is then given by: [ px/pw, py/pw, pz/pw ]. We loop through the triangles of a mesh that contains n vertices simply by looping through every set of three consecutive vertices:Seeing whether a triangle is degenerate (has zero area)V0, V1, V2 V1, V2, V3 V2, V3, V4 ... Vn-3, Vn-2, Vn-1 We don't want to hit test any triangle that has zero area. A simple test to see whether a triangle has zero area is just to do an area calculation, and see whether the result is zero.Inside/outside testing of a point A point P is inside triangle (A,B,C) if it is on the same side of all three of the triangle's edges A→B, B→C and C→A.The code we implemented For your reference, the code we implemented in class is at code9.zip. |