OpenGL-旋转后平移 [英] OpenGL - Translate after rotate
问题描述
我尝试在原点旋转对象后在世界空间中平移对象.问题是,当我旋转对象的原点时,它的局部坐标系也会旋转,因此它的轴不再与世界坐标轴平行.因此,例如,如果在绕y轴旋转对象后需要将对象平移到点(3,0,5),我将无法执行此操作,因为对象的局部坐标系不是与世界坐标系相同.例如:
I am trying to translate an object in world space after rotating it at the origin. The problem is, after I rotate an object while it's at the origin, its local coordinate system rotates as well, so its axes are not parallel with the world coordinate axes anymore. So, for example, if I needed to translate an object to the point (3, 0, 5) after rotating the object around the y-axis, I would not be able to do this since the object's local coordinate system isn't the same as the world coordinate system. For example:
glRotatef(45, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 5.0);
glutSolidCube(1); // This won't be at (3,0,5) like how I need it
我该如何克服?
推荐答案
这并不是OpenGL的特性,而是矩阵数学.
This is not so much a property of OpenGL, but matrix math.
将顶点发送到OpenGL固定函数管线时,它会通过矩阵的模型视图和投影进行变换,即
When you send in a vertex to the OpenGL fixed function pipeline, it gets transformed by the matrices modelview and projection, i.e.
v_eye = MODELVIEW · v
v_clip = PROJECTION · v_eye
现在,模型视图矩阵由所有这些子转换组成.它们是不可交换的,即操作顺序很重要.例如
Now the modelview matrix is composed of all those sub-transformations. And they're non commutative, i.e. order of operations matters. For example
MODELVIEW = T_0 · R_0 · R_1 · T_1 · S_1 · R_2
现在您可以将上面的v_eye计算重写为
Now you can rewrite the v_eye calculation above as
v_eye = T_0 · ( R_0 · ( R_1 · (T_1 · (S_1 · (R_2 · v)))))
如您所见,v从右到左乘(就像矩阵一样)到您组成modelview的矩阵堆栈中.
As you can see, the v is multiplied (just like the matrices) right to left onto the stack of matrices you composed modelview of.
然而,在OpenGL中发生的是,仅计算了一个矩阵"MODELVIEW",并且仅使用该4×4数字方案对v进行了转换. OpenGL不会重新执行"所有矩阵乘法步骤.
However what happens in OpenGL is, that the one matrix "MODELVIEW" is calculated, and v is transformed only with that 4×4 scheme of numbers. OpenGL does not "re-execute" all the matrix multiplication steps.
这篇关于OpenGL-旋转后平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!