如何在opengl中旋转向量? [英] How to rotate a vector in opengl?

查看:383
本文介绍了如何在opengl中旋转向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用glm :: rotate时,我想旋转对象.

I want to rotate my object,when I use glm::rotate.

它只能在X,Y,Z箭头上旋转.

It can only rotate on X,Y,Z arrows.

例如,Model = vec3(5,0,0)

For example,Model = vec3(5,0,0)

如果我使用Model = glm :: rotate(Model,glm :: radians(180),glm :: vec3(0,1,0));

if i use Model = glm::rotate(Model,glm::radians(180),glm::vec3(0, 1, 0));

它变成vec3(-5,0,0)

it become vec3(-5,0,0)

我想要一个API,因此我可以在vec3(0,4,0)上旋转180度,因此模型移至vec3(3,0,0)

i want a API,so i can rotate on vec3(0,4,0) 180 degree,so the Model move to vec3(3,0,0)

我可以使用任何API吗?

Any API can I use?

推荐答案

是的 OpenGL 使用 4x4统一变换内部矩阵.但是glRotate API 使用4个参数,而不是3个:

Yes OpenGL uses 4x4 uniform transform matrices internally. But the glRotate API uses 4 parameters instead of 3:

glMatrixMode(GL_MODELVIEW);
glRotatef(angle,x,y,z);

它将围绕点(0,0,0)和轴[(0,0,0),(x,y,z)]旋转选定的矩阵角度angle [deg].如果需要围绕特定点(x0,y0,z0)旋转,则还应平移:

it will rotate selected matrix around point (0,0,0) and axis [(0,0,0),(x,y,z)] by angle angle [deg]. If you need to rotate around specific point (x0,y0,z0) then you should also translate:

glMatrixMode(GL_MODELVIEW);
glTranslatef(+x0,+y0,+z0);
glRotatef(angle,x,y,z);
glTranslatef(-x0,-y0,-z0);

这是旧的 API ,但是在使用现代的 GL 时,您需要自己进行矩阵处理(例如,使用 GLM ),因为不再有矩阵堆栈. GLM 应该具有与glRotate相同的功能,只是找到模仿它的功能(看起来像 glm ::旋转大致相同).如果不是这样,您仍然可以使用 Rodrigues旋转公式.

This is old API however and while using modern GL you need to do the matrix stuff on your own (for example by using GLM) as there is no matrix stack anymore. GLM should have the same functionality as glRotate just find the function which mimics it (looks like glm::rotate is more or less the same). If not you can still do it on your own using Rodrigues rotation formula.

现在您的示例对我来说毫无意义:

Now your examples make no sense to me:

(5,0,0) -> glm::rotate (0,1,0) -> (-5,0,0)

暗示围绕y轴旋转180度?好吧,我可以看到轴,但在任何地方都看不到角度.第二个(您想要的API)甚至有问题:

implies rotation around y axis by 180 degrees? well I can see the axis but I see no angle anywhere. The second (your desired API) is even more questionable:

 (4,0,0) -> wanted API -> (3,0,0) 

向量在旋转后应该具有相同的大小,这显然不是这种情况(除非您要绕(0,0,0)以外的某个点旋转(也未提及).而且在旋转之后,通常会将一些大小泄漏到其他轴上您的y,z都为零,仅在特殊情况下(旋转90度的倍数时)为真.

vectors should have the same magnitude after rotation which is clearly not the case (unless you want to rotate around some point other than (0,0,0) which is also nowhere mentioned. Also after rotation usually you leak some of the magnitude to other axises your y,z are all zero that is true only in special cases (while rotation by multiples of 90 deg).

很显然,您忘记了重要信息,或者不知道轮换的方式.

So clearly you forgot to mention vital info or do not know how rotation works.

现在您想旋转X,Y,Z箭头是什么意思?是否希望按击键增加轮换?还是在场景中呈现了 GUI 这样的箭头,并且希望选择它们并在拖动时旋转?

Now what you mean by you want to rotate on X,Y,Z arrows? Want incremental rotations on key hits ? or have a GUI like arrows rendered in your scene and want to select them and rotate if they are drag?

[Edit1]新示例

我需要一个 API ,因此我可以将vec3(0,4,0)旋转180 deg并显示结果 将为vec3(3,0,0)

I want a API so I can rotate vec3(0,4,0) by 180 deg and result will be vec3(3,0,0)

仅当您在谈论点而不是向量时,这才可行.因此,您需要旋转中心,旋转轴和角度.

This is doable only if you are talking about points not vectors. So you need center of rotation and axis of rotation and angle.

// knowns
vec3 p0 = vec3(0,4,0);  // original point
vec3 p1 = vec3(3,0,0);  // wanted point
float angle = 180.0*(M_PI/180.0); // deg->rad
// needed for rotation
vec3 center = 0.5*(p0+p1); // vec3(1.5,2.0,0.0) mid point due to angle = 180 deg
vec3 axis = cross((p1-p0),vec3(0,0,1)); // any perpendicular vector to `p1-p0` if `p1-p0` is parallel to (0,0,1) then use `(0,1,0)` instead
// construct transform matrix
mat4 m =GLM::identity(); // unit matrix
m = GLM::translate(m,+center);
m = GLM::rotate(m,angle,axis);
m = GLM::translate(m,-center); // here m should be your rotation matrix
// use transform matrix
p1 = m*p0; // and finaly how to rotate any point p0 into p1 ... in OpenGL notation

我没有使用 GLM 进行编码,因此可能会有一些细微的差异.

I do not code in GLM so there might be some little differencies.

这篇关于如何在opengl中旋转向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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