【发布时间】:2019-11-28 05:19:15
【问题描述】:
我在 OpenGL 中制作一个风扇,里面有杆子和旋转的东西,当我尝试旋转它们时,所有的风扇都随着杆子旋转,我应该如何修复它,我使用了 @ 987654321@,同样当我使用push和pup矩阵旋转时,它根本不旋转,它只以我写的角度旋转一次,任何建议有帮助吗??
【问题讨论】:
-
你能发布你目前得到的代码吗?
标签: c++ opengl opengl-compat glrotate
我在 OpenGL 中制作一个风扇,里面有杆子和旋转的东西,当我尝试旋转它们时,所有的风扇都随着杆子旋转,我应该如何修复它,我使用了 @ 987654321@,同样当我使用push和pup矩阵旋转时,它根本不旋转,它只以我写的角度旋转一次,任何建议有帮助吗??
【问题讨论】:
标签: c++ opengl opengl-compat glrotate
我使用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();
// ...
}
【讨论】:
在老派的 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 矩阵作为顶点着色器统一,用于绘制每个网格(并且有几种方法可以做到这一点,或者在统一变量 Uniform-Buffer-对象、Shader-Storage-Buffer 或某些实例化缓冲区输入)。
【讨论】: