OpenGL 中的 glm 旋转用法

glm rotate usage in Opengl(OpenGL 中的 glm 旋转用法)
本文介绍了OpenGL 中的 glm 旋转用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

这篇关于OpenGL 中的 glm 旋转用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)