[英]Understanding the GLM 4x4 Matrix functions
我在嘗試復制其某些數學運算以完成我的Matrix4類以進行分配時,無法理解GLM Matrix函數的某些元素。 采取此旋轉功能。
tmat4x4<T, P> const & m,
T angle,
tvec3<T, P> const & v
T const a = angle;
T const c = cos(a);
T const s = sin(a);
tvec3<T, P> axis(normalize(v));
tvec3<T, P> temp((T(1) - c) * axis);
tmat4x4<T, P> Rotate(uninitialize);
Rotate[0][0] = c + temp[0] * axis[0];
Rotate[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
Rotate[0][2] = 0 + temp[0] * axis[2] - s * axis[1];
Rotate[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
Rotate[1][1] = c + temp[1] * axis[1];
Rotate[1][2] = 0 + temp[1] * axis[2] + s * axis[0];
Rotate[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
Rotate[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
Rotate[2][2] = c + temp[2] * axis[2];
tmat4x4<T, P> Result(uninitialize);
Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
Result[3] = m[3];
或翻譯功能(v是向量)
tmat4x4<T, P> const & m,
tvec3<T, P> const & v
Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
我難以理解的部分是對矩陣Result [1]或m [0]訪問的部分。 結果[1] =結果[1] [1]嗎? 它已在許多功能中使用,這是我遇到的這些功能的最后一部分。
他們如何使用單個數字來訪問2D數組中的元素,該單個數字是訪問哪個元素?
定義模板tmat4x4<T,P>
的代碼(類型T
和精度P
在此處可用 ,可以回答您的問題。
如您所見,第60 tmat4x4
的實際數據內容定義為4個col_type
元素的數組,訪問m[i]
(將第96行定義為返回col_type &
) 返回第i個完整列 。
col_type
是tvec4<T,P>
類型定義 ,其代碼在此處可用 ,並且還定義了一個返回類型T &
的[]
訪問運算符 ,因此當您編寫m[a][b]
您說“請給我列a,元素b”。
tvec4<T,P>
還定義了一個二進制*
運算符 ,因此取整個向量並將其乘以類型為U
的某個標量是有意義的,該類型標量將向量的每個元素乘以該標量 。
因此,為了回答您的問題, Result[1]
不是Result[1][1]
,而是Result[1][1..4]
(即使這不是正確的C ++)。
有兩個名為tmat4x4<T, P>::operator[]
函數。 一個具有返回類型tmat4x4<T>::col_type&
,另一個具有返回類型tmat4x4<T>::col_type const&
。 因此m[1]
並不表示矩陣的單個元素; 相反,它表示矩陣的整個列(四個元素),您可以對其執行數學列矢量運算。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.