C++ OpenGL 中的 glm 旋转用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8844585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:12:44  来源:igfitidea点击:

glm rotate usage in Opengl

c++openglglm-math

提问by Test Test

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+.

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

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

到目前为止,这是我在 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 );

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 ?

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

回答by the swine

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)

Notethat to convert from degrees to radians, useglm::radians(degrees)

请注意,要将度数转换为弧度,请使用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.

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

note:earlier versions accepted angles in degrees. This is deprecated since 0.9.6

注意:早期版本接受以度为单位的角度。自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)

回答by Anonymous

GLM has good example of rotation : http://glm.g-truc.net/code.html

GLM 有很好的旋转示例:http: //glm.g-truc.net/code.html

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);
glm::mat4 ViewTranslate = glm::translate(
    glm::mat4(1.0f),
    glm::vec3(0.0f, 0.0f, -Translate)
);
glm::mat4 ViewRotateX = glm::rotate(
    ViewTranslate,
    Rotate.y,
    glm::vec3(-1.0f, 0.0f, 0.0f)
);
glm::mat4 View = glm::rotate(
    ViewRotateX,
    Rotate.x,
    glm::vec3(0.0f, 1.0f, 0.0f)
);
glm::mat4 Model = glm::scale(
    glm::mat4(1.0f),
    glm::vec3(0.5f)
);
glm::mat4 MVP = Projection * View * Model;
glUniformMatrix4fv(LocationMVP, 1, GL_FALSE, glm::value_ptr(MVP));

回答by abe732

I noticed that you can also get errors if you don't specify the angles correctly, even when using glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z))you still might run into problems. The fix I found for this was specifying the type as glm::rotate(Model, (glm::mediump_float)90, glm::vec3(x, y, z))instead of just saying glm::rotate(Model, 90, glm::vec3(x, y, z))

我注意到如果你没有正确指定角度,你也会得到错误,即使在使用时glm::rotate(Model, angle_in_degrees, glm::vec3(x, y, z))你仍然可能会遇到问题。我为此找到的解决方法是指定类型glm::rotate(Model, (glm::mediump_float)90, glm::vec3(x, y, z))而不是仅仅说glm::rotate(Model, 90, glm::vec3(x, y, z))

Or just write the second argument, the angle in radians (previously in degrees), as a float with no cast needed such as in:

或者只写第二个参数,以弧度为单位的角度(以前以度为单位),作为不需要强制转换的浮点数,例如:

glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), 3.14f, glm::vec3(1.0));

You can add glm::radians() if you want to keep using degrees. And add the includes:

如果您想继续使用度数,可以添加 glm::radians() 。并添加包括:

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"