動態三角射線相交


2

我正在研究一款小型仿真軟件,該軟件具有使用OpenGL的3D渲染器。我將對象渲染為屏幕上的三角形網格。為了能夠在屏幕上選擇三角形,我已經實現了Möller-Trumbore射線三角形相交算法。該算法工作正常。但是,我還有另一個問題。我的三角形在世界上不是靜態的。當我更改三角形在屏幕上的位置時,我必須在算法中更改三角形的輸入頂點,但是我不知道該如何進行數學計算。有沒有辦法將這種算法用於動態對象?

3

There are 2 ways to go about intersecting the triangle. Let the vertices of the triangle have positions $v_1, v_2, v_3$. Let the ray have origin $o$ and direction $d$. Let the model (4x4) matrix be $M$.

To find the new vertex coordinates one extends the positions with a 1 (to allow for translations) and multiplies by the model matrix. Let $u_i = (v_{i,x}, v_{i,y}, v_{i,z}, 1)$ then $w_i = Mu_i$. The resulting vertex positions are: $v_i' = (w_{i,x}, w_{i,y}, w_{i,z})$.

The other option is to transform the ray with the inverse matrix $M^{-1}$ and intersect with the non-transformed triangle. To achieve this extend $o$ with a 4th coord of 1 (to account for translation) and extend $d$ with a 4th coord of 0 (to ignore translation) then multiply both with $M^{-1}$: $$o' = M^{-1}(o_x, o_y, o_z, 1)$$ $$d' = M^{-1}(d_x, d_y, d_z, 0)$$ Drop the 4th coordinate of $o'$ and $d'$ then intersect with the triangle formed by $v_1, v_2, v_3$.