如何在全局轴上而不是局部轴上旋转图形? [英] How to rotate a graphic over global axes, and not to local axes?

查看:93
本文介绍了如何在全局轴上而不是局部轴上旋转图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题很简单,但是我是Opengl的新手,我不知道该怎么办.

I think my problem is very simple, but I´m new in Opengl and I don't know what to do.

我正在尝试旋转金字塔(数字无关紧要),我想相对于X旋转 然后相对于Y轴旋转.

I'm trying to rotate a pyramid (the figure doesn't matter), I want to rotate with respect to X and then rotate with respect to Y axe.

但是当我在X轴上旋转时,Y轴也会旋转.然后,当我想相对于Y旋转时,Y轴不再是原始的.

But when I do a rotation on X, the Y axe rotates too. And then when I want to rotate whit respect to Y, Y axis is no longer the original.

下一张图片显示了mi金字塔在原点的星状:

The next image shows how mi pyramid stars in the origin:

然后旋转尊重X:

Y和Z轴也旋转,然后,如果我要相对于Y旋转,则金字塔相对于旋转的Y轴旋转,而不是相对于原始的Y轴(深绿色)旋转.

The axes Y and Z rotate too, and then if I want to rotate with respect to Y, the pyramid rotates with respect to the Y axe rotated, and not to the original Y axe(strong green).

我使用PushMatrix();和PopMatrix(); :

I use PushMatrix(); and PopMatrix(); :

gl.PushMatrix();             
    gl.Rotate(bx, 1.0f, 0.0f, 0.0f);           

        gl.PushMatrix();
        gl.Rotate(by, 0.0f, 1.0f, 0.0f); 

            //Pyramid                      
            gl.Begin(OpenGL.GL_TRIANGLES);
            gl.Color(1.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 1.0f, 0.0f);
            gl.Vertex(-1.0f, -1.0f, 1.0f);
            gl.Vertex(1.0f, -1.0f, 1.0f);

             gl.Color(0.0f, 1.0f, 0.0f);
             gl.Vertex(0.0f, 1.0f, 0.0f);
             gl.Vertex(1.0f, -1.0f, 1.0f);
             gl.Vertex(1.0f, -1.0f, -1.0f);

             gl.Color(0.0f, 0.0f, 1.0f);
             gl.Vertex(0.0f, 1.0f, 0.0f);
             gl.Vertex(1.0f, -1.0f, -1.0f);
             gl.Vertex(-1.0f, -1.0f, -1.0f);

             gl.Color(1.0f, 1.0f, 1.0f);
             gl.Vertex(0.0f, 1.0f, 0.0f);
             gl.Vertex(-1.0f, -1.0f, -1.0f);
             gl.Vertex(-1.0f, -1.0f, 1.0f);
             gl.End();

         gl.PopMatrix();
   gl.PopMatrix();

bx by 单击按钮时仅增加或减小角度(Z不再重要)

bx and by just increment or decrement the angle when you click the button (Z doesn't matter now)

好吧,我想相对于X和Y旋转金字塔,但是原始轴(通用轴)独立于局部轴.

Well I want to rotate the pyramid, with respect to X and Y, but the original axes, (universal axes) independently of local axes.

注意:我使用SharpGL(适用于c#的OpenGL),但是相同.

Note: I use SharpGL (OpenGL for c#), but it's the same.

更新

我更改了代码:

gl.PushMatrix();             
    gl.Rotate(bx, 1.0f, 0.0f, 0.0f);           

      gl.PushMatrix();
       //gl.Rotate(by, 0.0f, 1.0f, 0.0f); This changes
        gl.Rotate(by, 0.0f, Math.Cos(bx*Math.PI / 180), -Math.Sin(bx*Math.PI / 180));           
         //Pyramid                      

      gl.PopMatrix();
 gl.PopMatrix();

我将矩阵Rx逆[1 0 0; 0 Cos Sin;由向量Y [0 1 0]生成0 -Sin Cos],然后将得到的向量[0 Cos -Sin]放入Y旋转.

I multiply the matrix Rx inverse [1 0 0; 0 Cos Sin; 0 -Sin Cos] by the vector Y [0 1 0], and the resulting vector [0 Cos -Sin], I put in the rotation by Y.

这可以按我想要的方式工作,我先进行X旋转,然后再进行Y旋转,但是当我再次进行X旋转时,现在旋转是X旋转,而不是相对于原始旋转!!!,但是Y的旋转仍保持我想要的状态.

This works as I want, I do a rotation by X, after a rotation by Y, but when I do another rotation by X, now the rotation is by the X rotated and not with respect to the original!!!, but the rotation by Y keeps as I want.

还有其他想法吗??非常感谢!!!

Any another idea?? Thank you so much!!!

推荐答案

操作顺序很重要,根据OpenGL中使用的语义,实际转换以与代码中发生的顺序相反的顺序应用.因此,如果要先旋转X,然后旋转Y,则Y旋转必须首先出现在代码中.

Order of operations matter and by the semantics used in OpenGL the actual transformations are applied in the reverse order as they happen in the code. So if you want to first rotate by X, then by Y, the Y rotation must come first in your code.

更深层的原因是OpenGL使用(出于各种明智的原因)列主,正确的关联矩阵排序.像

The deeper reason for this is that OpenGL uses (for various, sensible reasons) column major, right associative matrix ordering. A transformation chain like

MV = V · M · R_x · R_y

将从右到左,即在列(位置)向量中相乘

Will multiply in a column (position) vector from the right toward left, i.e.

MV · ↑v

分解成

V · ( M · ( R_x · ( R_y · ↑v ) ) )

并且由于矩阵不是阿贝尔矩阵,因此存在无数的矩阵A,B

And since matrices are not abelian there is are uncountably many matrices A, B for which

A · B =/= B · A

这篇关于如何在全局轴上而不是局部轴上旋转图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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