我正在渲染一个圆锥体,我想将它逆时针旋转 90 度,使尖端朝西!我正在使用 OpenGL 3+。
到目前为止,这是我在 Cone.cpp 中的代码:
//PROJECTION
glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);
//VIEW
glm::mat4 View = glm::mat4(1.);
View = glm::translate(View, glm::vec3(2.0f,4.0f, -25.0f));
//MODEL
glm::mat4 Model = glm::mat4(1.0);
//Scale by factor 0.5
Model = glm::scale(glm::mat4(1.0f),glm::vec3(0.5f));
glm::mat4 MVP = Projection * View * Model;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram_spaceship, "MVP_matrix"), 1, GL_FALSE, glm::value_ptr(MVP));
glClearColor(0.0, 0.0, 0.0, 1.0);
glDrawArrays(GL_LINE_STRIP, start_cone, end_cone );
并未显示所有代码。
有人可以指导我完成轮换吗?我必须乘以 View 矩阵吗?带有“glm 旋转”功能?
最佳答案
您需要将模型矩阵相乘。因为那是模型位置、缩放和旋转应该在的地方(这就是它被称为模型矩阵的原因)。
您需要做的就是(参见 here)
Model = glm::rotate(Model, angle_in_radians, glm::vec3(x, y, z)); // where x, y, z is axis of rotation (e.g. 0 1 0)
Note that to convert from degrees to radians, use
glm::radians(degrees)
这需要模型矩阵并在所有已经存在的操作之上应用旋转。其他函数 translate 和 scale 做同样的事情。这样就可以在一个矩阵中组合许多变换。
注意: 早期版本接受以度为单位的角度。自 0.9.6 起已弃用此功能
Model = glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z)); // where x, y, z is axis of rotation (e.g. 0 1 0)
关于c++ - 在 Opengl 中使用 glm 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844585/