OpenGL/GSL 不将 Transpose 设置为 GL_TRUE 就无法工作

OpenGL/GSL not working without setting Transpose to GL_TRUE

如果没有变换,GLM 矩阵似乎无法工作

glm::mat4 proj = glm::ortho(0.0f,960.0f,0.0f,540.0f,-1.0f, 1.0f);

GL_TRUE 必须设置:

glUniformMatrix4fv(GetUniformLocation(name),1 ,GL_TRUE,&matrix[0][0])

GLM 不是应该采用列主要形式吗?

如果您不想转置矩阵,则必须将向量乘以着色器代码中 右边 的矩阵:

mat4 transformation;
vec4 vertexPosition;

gl_Position = transformation * vertexPosition;

说明:

GLSL Programming/Vector and Matrix Operations:

Furthermore, the *-operator can be used for matrix-vector products of the corresponding dimension, e.g.:

vec2 v = vec2(10., 20.);
mat2 m = mat2(1., 2.,  3., 4.);
vec2 w = m * v; // = vec2(1. * 10. + 3. * 20., 2. * 10. + 4. * 20.)

Note that the vector has to be multiplied to the matrix from the right.

If a vector is multiplied to a matrix from the left, the result corresponds to to multiplying a column vector to the transposed matrix from the right. This corresponds to multiplying a column vector to the transposed matrix from the right:

Thus, multiplying a vector from the left to a matrix corresponds to multiplying it from the right to the transposed matrix:

vec2 v = vec2(10., 20.);
mat2 m = mat2(1., 2.,  3., 4.);
vec2 w = v * m; // = vec2(1. * 10. + 2. * 20., 3. * 10. + 4. * 20.)


这意味着:

如果矩阵定义如下:

mat4 m44 = mat4(
    vec4( Xx, Xy, Xz, 0.0),
    vec4( Yx, Xy, Yz, 0.0),
    vec4( Zx  Zy  Zz, 0.0),
    vec4( Tx, Ty, Tz, 1.0) );

而矩阵uniform mat4 transformation是这样设置的(见glUniformMatrix4fv:

glUniformMatrix4fv( .... , 1, GL_FALSE, &(m44[0][0] ); 

然后向量必须乘以右边的矩阵:

gl_Position = transformation * vertexPosition;


但是当然可以设置矩阵转置:

mat4 m44 = mat4(
    vec4(  Xx,  Yx,  Zx,  Tx),
    vec4(  Xy,  Yy,  Zy,  Ty),
    vec4(  Xz   Yz   Zz,  Tz),
    vec4( 0.0, 0.0, 0.0, 1.0) );

设置为uniform变量时可以转置:

glUniformMatrix4fv( .... , 1, GL_TRUE, &(m44[0][0] );

然后向量必须乘以的矩阵:

gl_Position = vertexPosition * transformation;

注意,glm API documentation refers to The OpenGL Shading Language specification 4.20