在OpenGL 4.0中有没有标准的方法围绕局部坐标(即从模型视图矩阵)旋转? [英] Is there a standard way to rotate around local coordinates (that is, from a modelview matrix) in OpenGL 4.0?

查看:92
本文介绍了在OpenGL 4.0中有没有标准的方法围绕局部坐标(即从模型视图矩阵)旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用游戏引擎,最近开始使用点,我可以轻松地创建一个Camera类来管理视图矩阵(或允许一个简单的切换到另一个)。我在概念上知道它必须做什么,但是我有麻烦的是弄清楚如何围绕本地原点旋转(对于视图矩阵,它将与被转换矩阵相乘的转换矩阵相同) / p>

我知道在直接模式下,你会

  glTranslatef ,y,z); // move back to origin 
glRotatef(angle,x_rot,y_rot,z_rot); // turn using a quaternion
glTranslatef(-x,-y,-z); //移回原始位置

但我不知道如何转换为更现代应用。你会做基本相同的事情,使翻译矩阵回原点,旋转,并翻译回来?对于你想要旋转的每个单个对象?从我到目前为止测试的方法,该方法适用于查看,但控制在3D空间是不方便的(我不知道如何调整旋转不断。将运动组件转换为单个vec4,乘以旋转,应用于翻译,然后进行实际旋转?)



似乎有一个更有效的方式比做三个或更多的4x4矩阵乘法,我缺少。 / p>

你可以做基本相同的事情作为你在运行时没有做完整矩阵乘法的序列。由于平移矩阵非常简单,您可以手动进行乘法。 R 是一个3x3旋转矩阵, p 是你想要旋转的位置,你可以建立你的4x4 matrix by:


  1. R 存储在4x4矩阵的旋转部分。 / li>
  2. 计算 p - R * p ,并将结果存储在4x4矩阵的翻译部分。

这将减少到一个矩阵*向量乘法和向量减法。



另一种方法是在CPU代码中保持旋转和翻译分开,将它们更新为需要的,并且只有在更新统一时才将它们组合成一个4x4矩阵。



由于你可以控制着色器代码,你甚至不必使用4x4矩阵以包含整个转换。例如,如果在旋转之前应用转换更方便,则可以将mat3旋转矩阵和vec3平移向量馈送到着色器中,并在与旋转矩阵相乘之前将平移向量添加到位置。如果您更新其中一个的频率比另一个更频繁,那么保持两个单独的操作也是有益的。


I've been working on a game engine and recently got to the point where I could easily create a Camera class to manage a view matrix (or allow an easy switch to another). I know conceptually what it must do, but where I'm having trouble is figuring out how to rotate about the local origin (for the view matrix, it would be the same as the translation matrix that is multiplied into a rotation matrix).

I know that in direct mode, you would

glTranslatef(x, y, z);   //move back to origin
glRotatef(angle, x_rot, y_rot, z_rot);   //turn using a quaternion
glTranslatef(-x, -y, -z);   //move back to original location

but I'm not sure how that would translate to a more modern application. Would you do essentially the same thing, make a translation matrix back to origin, rotate, and translate back? For every single object you want to rotate? From what I've tested so far, that method works for viewing, but controlling that in 3D space is unwieldy (I'm not sure how to adjust for the rotation constantly. Convert movement components into a single vec4, multiply by the rotation, apply to the translation, then do the actual rotation?)

It seems like there's a more efficient way than doing three or more 4x4 matrix multiplications that I'm missing.

Here's the specific section after trying out a suggested solution.

解决方案

You can do basically the same thing as the sequence you posted without doing full matrix multiplies at runtime. Since the translation matrices are very simple, you can to the multiplication manually. With R being a 3x3 rotation matrix, and p being the position you want to rotate around, you can build your 4x4 matrix by:

  1. Store R in the rotation part of the 4x4 matrix.
  2. Calculate p - R * p, and store the result in the translation part of the 4x4 matrix.

This would reduce it to one matrix * vector multiply, and a vector subtraction. You may want to double check my math, since I just derived it on paper.

Another approach is that you keep rotations and translations separate in your CPU code, update them as needed, and only combine them into a 4x4 matrix when updating the uniform.

Since you have control over the shader code, you don't even have to use a 4x4 matrix to contain the entire transformation. For example, if it's more convenient for you to apply translations before rotations, you can feed a mat3 rotation matrix and a vec3 translation vector into your shader, and add the translation vector to the position before multiplying with the rotation matrix. Keeping the two separate can also be beneficial if you update one of them much more frequently than the other.

这篇关于在OpenGL 4.0中有没有标准的方法围绕局部坐标(即从模型视图矩阵)旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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