c++ - 在 Opengl 中使用 glm 旋转

标签 c++ opengl glm-math

我正在渲染一个圆锥体,我想将它逆时针旋转 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/

相关文章:

c++ - 使用CPPCHECK的Red Hat Enterprise Linux(RHEL)上的C++宏问题

python - 现代 OPENGL 无法更改 glDrawArrays 调用之间的统一值

c++ - OpenGL 4 : Draw with glVertexAttrib

c++ - 无需访问类的自动转换

c++ - 使用lookAt旋转相机

c++ - WhiteThresholdImage 魔法

c++ - 如何使用 premake 从 C++ 项目中复制所有头文件

c++ - 继承和派生类

c++ - 如何使用 GLSL 在体积渲染中进行混合?

opengl-3 - 获取 Opengl 的子弹物理变换矩阵