如果不将 Transpose 设置为 GL_TRUE,OpenGL/GSL 将无法工作

标签 opengl game-engine glm-math coordinate-transformation

如果没有变换,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) );

矩阵统一mat4变换设置如下(参见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) );

Or 当设置为统一变量时可以转置:

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

然后向量必须从左侧开始乘以矩阵:

gl_Position = vertexPosition * transformation;

请注意,glm API documentationThe OpenGL Shading Language specification 4.20

关于如果不将 Transpose 设置为 GL_TRUE,OpenGL/GSL 将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51627105/

相关文章:

xna - 用于网络世界模拟/AI沙箱的3D游戏引擎

c++ - 将图像从屏幕坐标转换为世界坐标

opengl - OpenGL 三角形条中的缠绕方向是否从三角形到三角形交替?

opengl - (GL) 我们可以在一次绘制调用中绘制多条线带吗?

javascript - 三个js分组碰撞检测(THREE.Group)

c++ - 我的定义有什么问题? C++

c++ - OpenGL GLI 不支持 VS2015?

c++ - 另一个OpenGL SuperBible 5th edition的设置问题

c++ - 如何在运行时更改现有 QGLWidget 的 QGLFormat?

objective-c - 游戏实体的类型和子类型的良好模式或架构?