在我的openGL项目中,我试图实现以下目标:让对象围绕其原点(在其自己的世界中的(0,0,0)点)旋转,并同时将其翻译。我拿出follwing结构:OpenGL在同一时间翻译和旋转
/***
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'中编写转换和旋转。 –