简体   繁体   English

OpenGL-OpenTK旋转问题

[英]OpenGL-OpenTK rotation problems

I'm a real noob who just started learning 3d programming and i have a really hard time learning about rotation in 3D space. 我是一个真正的菜鸟,刚开始学习3D编程,而且我很难学习3D空间中的旋转。 My problem is that I can't seem to figure out how to rotate an object using it's local coordinates. 我的问题是我似乎无法弄清楚如何使用对象的局部坐标旋转对象。

I have a basic class for 3d objects and, for starters, i want to implement functions that will rotate the object on a certain axis with x degrees. 我有一个用于3d对象的基本类,对于初学者来说,我想实现一些函数,这些函数将使对象在某个轴上旋转x度。 So far i have the following: 到目前为止,我有以下内容:

 public void RollDeg(float angle)
    {
        this.rotation = Matrix4.Mult(rotation, 
            Matrix4.CreateRotationX(MyMath.Conversions.DegToRad(angle)));
    }

    public void PitchDeg(float angle)
    {
        this.rotation = Matrix4.Mult(rotation, 
             Matrix4.CreateRotationY(MyMath.Conversions.DegToRad(angle)));
    }

    public void YawDeg(float angle)
    {
        this.rotation = Matrix4.Mult(rotation,
             Matrix4.CreateRotationZ(MyMath.Conversions.DegToRad(angle)));
    }

'rotation' is a 4x4 matrix which starts as the identity matrix. “旋转”是一个4x4矩阵,从单位矩阵开始。 Each time i want to roll/pitch/yaw the object, i call one of the functions above. 每当我想滚动/倾斜/偏航对象时,我都会调用上面的功能之一。

for drawing, i use another function that pushes a matrix onto the ModelView stack, multiplies it with the translation, rotation and scale matrices of the object (in this order) and begins drawing the vertices. 为了进行绘制,我使用了另一个函数,该函数将矩阵推入ModelView堆栈,将其与对象的平移,旋转和缩放矩阵(按此顺序)相乘,然后开始绘制顶点。 ofcourse, finally i pop the matrix off the stack. 当然,最后我将矩阵从堆栈中弹出。

the problem is that the functions above rotate the object on the GLOBAL axis, not on the LOCAL ones, even if, from my understanding, every time you rotate an object, the local system changes it's axis and then, when a new rotation is applyied on top of the others, the local axis are used for the new one. 问题是上面的函数在GLOBAL轴上旋转对象,而不是在LOCAL上,即使按照我的理解,每次旋转对象时,本地系统都会更改其轴,然后应用新的旋转除此以外,局部轴用于新轴。

i read different tutorials about the math behind it and how to rotate objects, but i couldn't find one the could help me. 我阅读了有关其背后的数学以及如何旋转对象的不同教程,但找不到能帮助我的教程。

if anyone has the time, i would really appreciate if he could help me understand HOW to rotate around local axis and, maybe even more important, what i did wrong on my current implementation. 如果有人有时间,如果他能帮助我理解如何绕局部轴旋转,以及可能甚至更重要的是,我在当前实现中做错的事情,我将不胜感激。

If you want to perform your transformations in this order : translation -> rotation -> scale (which makes perfectly sense, it's what's wanted usually), you have to multiply your matrices in the reverse order. 如果要按以下顺序执行转换:平移->旋转->缩放(这很有意义,通常是通常需要的),则必须以相反的顺序乘以矩阵。

In a right-handed coordinate system (ie the one openGL uses), matrix multiplication must be performed from right to left. 在右手坐标系(即openGL使用的一个坐标系)中,必须从右到左执行矩阵乘法。 This is why : 这就是为什么 :

ModelViewTransform = Transform * View * Model // <- you begin by the model, right ? ModelViewTransform = Transform * View * Model // //-您是从模型开始的,对吧? so it's this way 所以就是这样

Note that in directX they use a left-handed coordinate system. 请注意,在directX中,它们使用左手坐标系。 It has his shortcomings, but it's more intuitive. 它有他的缺点,但是更直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM