glm 设置对象位置

glm set object position

我想在 OpenGL 中使用 glm 库设置对象(不是相机)的 x、y、z 坐标。 我希望 glm::translate 方法可以解决这个问题,但它生成的矩阵实际上改变了我看待对象的方式。

也就是调用方法的方式:

glm::translate(glm::vec3(x, y, z));

它returns矩阵:

| 1  0  0  0 |
| 0  1  0  0 |
| 0  0  1  0 |
| x  y  z  1 |

但我预计:

| 1  0  0  x |
| 0  1  0  y |
| 0  0  1  z |
| 0  0  0  1 |

我做了一个像 glm::transpose(glm::translate(glm::vec3(x, y, z))) 这样的快速修复,但它似乎是一个错误的代码。

有没有办法生成一个矩阵来创建平行平移,设置对象的 x、y、z 坐标,而不转置矩阵本身?

GLM 创建列主阶矩阵,因为 GLSL 创建列主阶矩阵。

如果你想得到一个行主阶矩阵,那么你必须 glm::transpose 矩阵或者你必须使用矩阵初始化器:

glm::mat4 m = glm::mat4(
    1.0f, 0.0f, 0.0f, x,
    0.0f, 1.0f, 0.0f, y,
    0.0f, 0.0f, 1.0f, z,
    0.0f, 0.0f, 0.0f, 1.0 );


OpenGL Mathematics (GLM):

OpenGL Mathematics (GLM) is a header only C++ mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications.


The OpenGL Shading Language 4.6, 5.4.2 Vector and Matrix Constructors, page 101:

To initialize a matrix by specifying vectors or scalars, the components are assigned to the matrix elements in column-major order.

mat4(float, float, float, float,  // first column
     float, float, float, float,  // second column
     float, float, float, float,  // third column
     float, float, float, float); // fourth column


另见 OpenGL Mathematics (GLM); 2. Vector and Matrix Constructors