OpenGL - 这是在舞台上绘制旋转环的正确方法吗?
我已经创建了旋转螺丝并将它们放在舞台上,两者都可以旋转,但只是想知道这是否是正确的方法,或者是否有更简单的方法
glPushMatrix();
glColor3ub(0,0,255);
glTranslatef(110.0f,30.0f,-30.0f);
glRotatef(torusRotation1,0.0f, 1.0f, 0.0f);
glutSolidTorus(5.0, 30.0, 50, 50);
torusRotation1+= -1.0f;
if(torusRotation1 > 360.0f)
{
torusRotation1 = 0.0f;
}
glPopMatrix();
glPushMatrix();
glColor3ub(0,0,255);
glTranslatef(80.0f,-80.0f,0.0f);
glRotatef(torusRotation2,0.0f, 1.0f, 0.0f);
glutSolidTorus(5.0, 30.0, 50, 50);
torusRotation2+= 1.0f;
if(torusRotation2 > 360.0f)
{
torusRotation2 = 0.0f;
}
glPopMatrix();
i have created to rotating wrings and places them on the stage which both rotate it works fin but just wanted to know if this is a correct way of doing it or is there a more simple way
glPushMatrix();
glColor3ub(0,0,255);
glTranslatef(110.0f,30.0f,-30.0f);
glRotatef(torusRotation1,0.0f, 1.0f, 0.0f);
glutSolidTorus(5.0, 30.0, 50, 50);
torusRotation1+= -1.0f;
if(torusRotation1 > 360.0f)
{
torusRotation1 = 0.0f;
}
glPopMatrix();
glPushMatrix();
glColor3ub(0,0,255);
glTranslatef(80.0f,-80.0f,0.0f);
glRotatef(torusRotation2,0.0f, 1.0f, 0.0f);
glutSolidTorus(5.0, 30.0, 50, 50);
torusRotation2+= 1.0f;
if(torusRotation2 > 360.0f)
{
torusRotation2 = 0.0f;
}
glPopMatrix();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:您知道您正在使用已弃用的 OpenGL 命令,对吗?我知道出于学习 OpenGL 1/2 的目的是可以的,但您仍然应该知道哪些功能已被弃用(矩阵堆栈,我认为 glut 使用即时模式)。
假设您这样做纯粹是为了教育目的:如果它保持那么简单那就没问题,但如果它是更大程序的一部分,那么您可能应该进行概括。
为了清楚起见,您可以将该代码放入函数中,例如:
但请注意,您更改了该函数内的
rotation
值。此外,所有这些硬编码值(颜色、位置、大小、矢量)在我看来只能在测试程序中容忍。对于较大的项目,应使用场景描述格式。但对于具有大量元素的较大场景,强烈建议使用场景图结构。此外,如果它到达该区域,那么您应该将动画速度与绘制时间解耦,并引入一个
tick()
函数来更新相对于已过去时间的动画。如果您已经做到了这一点,那么您不妨将其移植到更现代的 OpenGL 版本。First off: you are aware that you are using a deprecated OpenGL commands, correct? I know that for the purposes of learning OpenGL 1/2 are fine, but you should still be aware of what features are deprecated (matrix stack and I think glut uses immediate mode).
Assuming that you are doing this purely for educational purposes: if it stays that simple then it's fine but if it's part of a bigger program then you should probably generalize.
For clarity you could put that code into a function e.g.:
but be aware that you change the
rotation
value inside that function. Also all those hard-coded values (colors, positions, sizes, vectors) are imo only tolerable in test programs. For bigger projects a scene description format should be used.But for bigger scenes with a bunch of elements a scenegraph structure is very recommended. Also if it get to that territory then you should decouple the animation speed from the draw time and introduce a
tick()
function that updates the animations relative tot the time that has passed. If you are taking it that far then you might as well port it to a more modern version of OpenGL.