使用局部旋转的OpenGL [英] Opengl using local rotation

查看:93
本文介绍了使用局部旋转的OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使旋转工作,但似乎可以正常工作,但是当我尝试移动对象时出现了我的问题,当时我期望对象的轴随网格一起旋转,但是它保持了全局轴和继续在该轴上移动对象. 我对旋转的理解有缺陷吗?还是导致这种情况发生的代码,如果我的理解是问题所在,那么有没有一种方法可以将对象轴与对象进行平移.我的代码如下.

I have been attempting to get rotation working and it seems to work, my issue occurs when I then try to move the object, I was expecting the axis of the object to rotate with the mesh but instead it keeps the global axis and continues to move the object on that axis. Is my understanding of rotation flawed? or is it the code that is causing this to occur, also if my understanding is the issue is there a way to translate the objects axis with the object. my code is as follows.

Projection= glm::perspective(45.f, 4.0f / 3.0f, 0.1f, 100.0f);

//position and direction are calculated based on mouse position
View      = glm::lookAt(
position,
position+direction,
up
);
glm::mat4 myMatrix = glm::translate(x,y,z);
glm::mat4 myScalingMatrix = glm::scale(0.2f, 0.2f ,0.2f);
glm::vec3 myRotationAxis( 0, 1, 0);
glm::mat4 tempModel = glm::mat4(1.f);
glm::mat4 myRotationMatrix =glm::rotate( tempModel,45.f, myRotationAxis );

glm::mat4 Model= myMatrix* myRotationMatrix *myScalingMatrix;

glm::mat4 MVP = Projection* View * Model;

推荐答案

由于旋转矩阵仅适用于以原点为中心(即(0,0,0))的旋转,因此您需要将原点切换到来回移动物体的中心.

Since the rotation matrix only works for those rotations centered to the origin (i.e., (0,0,0)), you need to switch the origin to the center of your object in a back-and-forth manner.

说您的对象位于(X,Y,Z).然后,如果您想以该对象为中心旋转,则需要先平移(-X,-Y,-Z),然后进行旋转,然后平移(X,Y,Z)回到原始位置,以便对对象执行旋转中心.

Say your object locates at (X, Y, Z). Then if you would like to rotate centered to that object, you need to first translate(-X, -Y, -Z), then do the rotation, then translate(X, Y, Z) back to the original position in order to perform rotation center to your object.

此图显示了其实际工作方式:

This picture shows how it actually works:

这是链接,其中包含上述图片,其中还包括有关旋转的一些基础知识.

Here is the link containing the above picture, which also includes some basics about rotation.

这篇关于使用局部旋转的OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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