我想使用 OpenGL 中的 glm 库设置对象(不是相机)的 x、y、z 坐标。 我希望 glm::translate 方法能够解决这个问题,但它会生成矩阵,这实际上会修改我查看对象的方式。
这就是调用方法的方式:
glm::translate(glm::vec3(x, y, z));
它返回矩阵:
| 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) 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
关于c++ - glm 设置对象位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49310067/