【发布时间】:2016-09-24 13:49:52
【问题描述】:
在我的 openGL 项目中,我试图实现以下目标:让对象围绕其原点(其自身世界中的 (0,0,0) 点)旋转并同时平移它。我想出了以下结构:
/***
Install shader and upload data to openGL, initialize and stuffs...
***/
// transfomration matrix is a global variable
// note the transformation matrix eventually will be right multiplied
// by the vertex data in the vertex shader
void keyboard(){
// Inside this function I capture the keyboard input.
// if rotation key is captured, set the rotation flag
// if translation or scaling key is captured, modify the matrix like this
transformation = glm::translate() * transformation;
}
void paintGL(){
// Inside this function I check if the rotation flag is up and modify the
// model to wolrd transformation matrix. I choose to modify the transform
// matrix here because I can take use of the main loop to let my object
// continuously rotate unless the flag is off.
if(rotation flag is set)
transformation = transformation * glm::rotate() * inverse(transformation)
}
int main(){
glutInit();
glutDisplayFunc(paintGL);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
现在我的问题是,当我只旋转或只平移或缩放时,程序按预期工作。但是如果我平移对象然后按下旋转,对象会回到它的初始位置然后旋转。或者消失!
我知道顺序很重要,但在我看来,只要我应用逆矩阵,对象就应该回到原来的世界并围绕原点旋转,然后我可以将它翻译回来。我的问题是由键盘和paintGL的调用顺序引起的吗?有人可以帮帮我吗?
【问题讨论】:
-
在
paintGL,你不应该先逆变换,然后旋转,然后再变换回来吗? -
@reproduktor 是的,这就是我所做的,如上所述。
-
对不起,我的错误,我认为 OpenGL 以另一种顺序应用矩阵。无论如何,如果您更改
keyboard函数中的翻译,您将处理可能已经包含旋转的变换。尝试单独维护平移变换,然后在paintGL中组合平移和旋转。 -
但你不应该。把倒数放在一边。此外,如果您还想在对象局部坐标系中进行平移,请在右侧乘以
glm::translate。 -
但是为了让对象绕其初始原点旋转,我需要先撤消对它所做的一切,对吧?