我在 OpenGL 中制作风扇,里面有杆子和旋转的东西,当我尝试旋转它们时,所有风扇都随着杆子旋转,我应该如何修复它,我glutPostRedisplay()
也使用过在旋转中使用push和pup矩阵,它根本不旋转,它只以我写的角度旋转一次,有什么建议有帮助吗?
问问题
636 次
2 回答
1
在老派的 opengl 中,您将封装一对 glPushMatrix(矩阵堆栈的存储状态)和 glPopMatrix(恢复先前的变换)调用之间的转换变化。
glPushMatrix();
glRotatef(45.0f, 0, 1, 0); //< or whatever your rotation is.
drawObjectToBeRotated();
glPopMatrix();
// now any objects drawn here will not be affected by the rotation.
在现代 OpenGL 中,您将上传一个单独的 ModelViewProjection 矩阵作为顶点着色器统一,用于绘制的每个网格(并且有几种方法可以做到这一点,或者在统一变量中,统一缓冲区对象,着色器存储 -缓冲区,或一些实例化缓冲区输入)。
于 2019-11-28T05:35:58.630 回答
1
我在旋转中使用 push 和 pup 矩阵,它根本不旋转,它只旋转一次
如果您使用glPushMatrix
/glPopMatrix
,则当前矩阵存储在矩阵堆栈中,glPushMatrix
并在调用时恢复glPopMatrix
。在两者之间应用的所有矩阵变换都将丢失。
要实现连续旋转,您必须使用增加的旋转角度。创建一个全局变量angle
并递增它。例如:
float angle = 0.0f;
void draw()
{
// ...
glPushMatrix();
// apply rotation and increase angle
glRotatef(angle, 1.0f, 0.0f, 0.0f);
angle += 1.0f;
// draw the object
// ...
glPopMatrix();
// ...
}
于 2019-11-28T05:46:15.027 回答