glm在Opengl中循环使用 [英] glm rotate usage in Opengl

查看:797
本文介绍了glm在Opengl中循环使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的Cone.cpp目前为止的代码:

  // PROJECTION 
glm :: mat4 Projection = glm :: perspective(45.0f,1.0f,0.1f,100.0f);

//查看
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);
//按比例缩放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函数?

解决方案

因为这是模型位置,缩放和旋转的地方(这就是为什么它被称为模型矩阵)。



所有你需要做的是:

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

模型矩阵,并对已经存在的所有操作应用旋转。其他功能translate和scale做同样的。这样,就可以在单个矩阵中组合多个变换。


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:

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)

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.

这篇关于glm在Opengl中循环使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆