我開始使用GLM庫來完成OpenGL 3和GLSL的數學運算。 我需要一個正投影繪製2D圖形,所以我所著這個簡單的代碼:屏幕上的glm中矩陣值的順序錯誤?
glm::mat4 projection(1.0);
projection = glm::ortho(0.0f, 640.0f, 480.0f, 0.0f, 0.0f, 500.0f);
打印的GLM ::鄰創造了價值,我得到:
0.00313 0.00000 0.00000 0.00000
0.00000 -0.00417 0.00000 0.00000
0.00000 0.00000 -0.00200 0.00000
-1.00000 1.00000 -1.00000 1.00000
據我所知這不是OpenGL中值的正確順序,因爲將該矩陣乘以位置向量將忽略所有翻譯值。
我用我的着色器和一些原語測試了這個矩陣,我只得到一個空白屏幕。但是,如果我手動修改矩陣爲它工作的很好:
0.00313 0.00000 0.00000 -1.00000
0.00000 -0.00417 0.00000 1.00000
0.00000 0.00000 -0.00200 -1.00000
0.00000 0.00000 0.00000 1.00000
而且,看功能「鄰」,在「GLM/GTC/matrix_transform.inl」文件:
template <typename valType>
inline detail::tmat4x4<valType> ortho(
valType const & left,
valType const & right,
valType const & bottom,
valType const & top,
valType const & zNear,
valType const & zFar)
{
detail::tmat4x4<valType> Result(1);
Result[0][0] = valType(2)/(right - left);
Result[1][1] = valType(2)/(top - bottom);
Result[2][2] = - valType(2)/(zFar - zNear);
Result[3][0] = - (right + left)/(right - left);
Result[3][1] = - (top + bottom)/(top - bottom);
Result[3][2] = - (zFar + zNear)/(zFar - zNear);
return Result;
}
我已經取代由以下代碼的最後3條初始化線也工作確定:
Result[0][3] = - (right + left)/(right - left);
Result[1][3] = - (top + bottom)/(top - bottom);
Result[2][3] = - (zFar + zNear)/(zFar - zNear);
這是我使用的測試(注意,最小的頂點着色器在這一刻uni_MVP只是projecti在上面解釋的矩陣):
uniform mat4 uni_MVP;
in vec2 in_Position;
void main(void)
{
gl_Position = uni_MVP * vec4(in_Position.xy,0.0, 1.0);
}
我thik,這不是一個錯誤,因爲所有功能的工作方式相同。 也許是我的C++編譯器的問題,它顛倒了多維數組的順序? 如何在不修改所有GLM源代碼的情況下解決此問題?
我在Windows Vista上使用運行在Code :: Blocks和MinGW上的最新版本的GLM庫(0.9.1)。
謝謝datenwolf!通過更改glUniformMatrix上的轉置參數來修復:) – Dani 2011-02-23 23:48:13