我正在渲染一个圆锥体,我想将它旋转,逆时针旋转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 rotate”功能?
答案 0 :(得分:36)
您需要将模型矩阵相乘。因为这就是模型位置,缩放和旋转的位置(这就是它被称为模型矩阵的原因)。
您需要做的就是(参见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)
注意要将度数转换为弧度,使用
glm::radians(degrees)
采用Model矩阵并在已经存在的所有操作之上应用旋转。翻译和缩放的其他功能也是如此。这样就可以在单个矩阵中组合许多变换。
注意:早期版本接受度数的角度。自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)
答案 1 :(得分:9)
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));
答案 2 :(得分:6)
我注意到如果你没有正确指定角度也会出错,即使使用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))
或者只是写第二个参数,以弧度表示的角度(以度为单位),作为不需要投射的浮点数,例如:
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), 3.14f, glm::vec3(1.0));
如果要继续使用度数,可以添加glm :: radians()。并添加包含:
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"