c++ - glm 设置对象位置

标签 c++ opengl matrix glm-math coordinate-transformation

我想使用 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 坐标,无需转置矩阵本身?

See this picture

最佳答案

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

关于c++ - glm 设置对象位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49310067/

相关文章:

c++ - 在 C++ 中更改现有 protobuf 消息的元素

c++ - 如何获得正在监听的端口列表及其各自的应用程序名称

r - 字符串分解

sorting - 如何按幂 bi 矩阵的降序对列日期进行排序

c++ - 比较位(一次一个位置)

c++ - C++ 中两个以上集合的 Set_union/Set_intersect

opencv - 为什么模型的 opengl 投影没有给出与 opencv 投影完全相同的结果?

opengl - 使用缓冲区对象来传输顶点数据有什么优点?

c++ - glDrawElements 错误 : nvoglv32. dll OpenGL

android - android canvas 中位图的同步旋转、平移和缩放