OpenGL 中的 glm 旋转用法

2021-12-19 00:00:00 opengl c++ glm-math

我正在渲染一个圆锥体,我想将它逆时针旋转 90 度,使尖端朝西!我使用的是 OpenGL 3+.

到目前为止,这是我在 Cone.cpp 中的代码:

//投影glm::mat4 投影 = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);//看法glm::mat4 视图 = glm::mat4(1.);View = glm::translate(View, glm::vec3(2.0f,4.0f, -25.0f));//模型glm::mat4 模型 = glm::mat4(1.0);//按因子 0.5 缩放模型 = glm::scale(glm::mat4(1.0f),glm::vec3(0.5f));glm::mat4 MVP = 投影 * 视图 * 模型;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 );

未显示所有代码.

有人可以指导我完成轮换吗?我必须乘以视图矩阵吗?带有glm 旋转"功能?

解决方案

您需要乘以您的模型矩阵.因为那是模型位置、缩放和旋转的位置(这就是为什么它被称为模型矩阵).

您需要做的就是(参见此处)

Model = glm::rotate(Model, angle_in_radians, glm::vec3(x, y, z));//其中 x, y, z 是旋转轴(例如 0 1 0)

<块引用>

注意要从度数转换为弧度,使用 glm::radians(degrees)

这需要模型矩阵并在其中已有的所有操作之上应用旋转.其他函数 translate 和 scale 做同样的事情.这样就可以在一个矩阵中组合许多变换.

注意:早期版本接受以度为单位的角度.这已被弃用,因为 0.9.6

Model = glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z));//其中 x, y, z 是旋转轴(例如 0 1 0)

I am rendering a cone, and I would like to rotate it, 90 degrees anti-clockwise, so that the pointy end faces west! I am using OpenGL 3+.

Here is my code in my Cone.cpp so far:

//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 );

Not all of the code is shown.

Can somebody guide me through the rotation? I do have to multiply the View matrix right ? with "glm rotate" function ?

解决方案

You need to multiply your Model matrix. Because that is where model position, scaling and rotation should be (that's why it's called the model matrix).

All you need to do is (see 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)

That takes the Model matrix and applies rotation on top of all the operations that are already in there. The other functions translate and scale do the same. That way it's possible to combine many transformations in a single matrix.

note: earlier versions accepted angles in degrees. This is deprecated since 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)

相关文章