OpenGL:动画时所有行星绕太阳的速度相同
我试图在 GLUT 中制作太阳系的动画,但不知何故,所有行星都以相同的速度绕太阳飞行,而它们都有不同的值。
我是 OpenGL/GLUT 的新手,所以如果您能解释您的答案,我们将不胜感激:)
这是我的动画方法:
void renderScene(void)
{
glClearDepth(1);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (spinMode) {
// Update the animation state
HourOfDay += AnimateIncrement;
DayOfYear += AnimateIncrement/24.0;
HourOfDay = HourOfDay - ((int)(HourOfDay/24))*24;
DayOfYear = DayOfYear - ((int)(DayOfYear/365))*365;
}
glLoadIdentity();
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glRotatef(15.0, 1.0, 0.0, 0.0 );
//Drawing the sun
glTranslatef(0.0f, 0.0f, f);
glColor3f(1.0, 1.0, 0.0);
//glPushMatrix();
gluSphere(quad[0], 1.0f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing mercurius
glColor3f(0.5, 0.5, 0.5);
glPushMatrix();
glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
gluSphere(quad[1], 0.2f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.4f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth1
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.6f, 64, 64);
glRotatef(360.0*3*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth2
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(4.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.3f, 64, 64);
glRotatef(360.0*4*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth3
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(4.3, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.75f, 64, 64);
glRotatef(360.0*2*DayOfYear/365.00, 0.0, 1.0, 0.0);
glPopMatrix();
// Flush the pipeline, and swap the buffers
glFlush();
glutSwapBuffers();
if ( singleStep ) {
spinMode = GL_FALSE;
}
glutPostRedisplay(); // Request a re-draw for animation purposes
}
I'm trying to make an animation of the Solarsystem in GLUT, but somehow all the planets are flying around the sun at the same speed, while they all have different values.
I'm new at OpenGL/GLUT so if you could explain your answer it would be appreciated :)
This is my animation method:
void renderScene(void)
{
glClearDepth(1);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (spinMode) {
// Update the animation state
HourOfDay += AnimateIncrement;
DayOfYear += AnimateIncrement/24.0;
HourOfDay = HourOfDay - ((int)(HourOfDay/24))*24;
DayOfYear = DayOfYear - ((int)(DayOfYear/365))*365;
}
glLoadIdentity();
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glRotatef(15.0, 1.0, 0.0, 0.0 );
//Drawing the sun
glTranslatef(0.0f, 0.0f, f);
glColor3f(1.0, 1.0, 0.0);
//glPushMatrix();
gluSphere(quad[0], 1.0f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing mercurius
glColor3f(0.5, 0.5, 0.5);
glPushMatrix();
glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
gluSphere(quad[1], 0.2f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.4f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth1
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.6f, 64, 64);
glRotatef(360.0*3*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth2
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(4.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.3f, 64, 64);
glRotatef(360.0*4*DayOfYear/365.0, 0.0, 1.0, 0.0);
glPopMatrix();
//Drawing the earth3
glColor3f(0.2, 0.2, 1.0);
glPushMatrix();
glTranslatef(4.3, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.75f, 64, 64);
glRotatef(360.0*2*DayOfYear/365.00, 0.0, 1.0, 0.0);
glPopMatrix();
// Flush the pipeline, and swap the buffers
glFlush();
glutSwapBuffers();
if ( singleStep ) {
spinMode = GL_FALSE;
}
glutPostRedisplay(); // Request a re-draw for animation purposes
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的呼叫顺序错误。所以每个行星都会沿着 y 轴绕其中心旋转。
以水星为例。
正确的顺序应该是
但是,如果这是唯一的错误,您的行星将根本不会绕太阳旋转。使你的行星绕太阳旋转的是第二个错误,你忘记在与太阳进行变换之前推动矩阵。因此,每个星球都会继承这些转变。
正确的代码应该是
另外,您应该只在主渲染函数中使用 glutSwapBuffers() ,并在回调中使用 glutPostRedisplay() 。不要在同一函数中同时使用它们。
Your call order is wrong. So every planet will rotate around its center along the y axis.
Take mercurius for example.
The correct order should be
However, if this is the only error, your planet will not rotate around the sun at all. What make your planet rotate around the sun is a second error, you forget to push the matrix before performing transformation with the sun. Therefore, every planet inherit these transformation.
The correct code should be
Also you should only use glutSwapBuffers() in your main rendering function, and glutPostRedisplay() in callbacks. Don't use them both in the same function.
注意 360.0*DayOfYear/365.0 位于行星的每次自转中。如果您希望它们以不同的速度旋转,那么旋转它们的量需要不同。将 365.0 更改为不同的数字以生成不同长度的年份。数字越大,年份越长,数字越小,年份越短。
Note 360.0*DayOfYear/365.0 is in each of your planet's rotations. If you would like them to rotate at different speeds then the amount you rotate them needs to be different. Change 365.0 to a different number to produce years of different lengths. A larger number will create a longer year and a smaller number will produce a shorter year.
您必须在调用
glTranslate
和glRotate
之后实际渲染球体。现在,您正在中间执行此操作,因此它会忽略旋转!例如:
应该是
You have to actually render the sphere after you call
glTranslate
andglRotate
. Right now, you're doing it inbetween, so it'll just ignore the rotation!For example:
should be
您确定不是旋转对象本身,而是旋转它们相对于中心点的位置吗?尝试更改
glPushMatrix()
和glPopMatrix()
之间的语句顺序。Are you sure you're not rotating the objects themselves, rather than their position relative to a central point? Try changing the order of the statements between
glPushMatrix()
andglPopMatrix()
.您实际上是在旋转和平移您渲染的所有对象。我建议对每个行星进行独特的增量,这样它们就会以不同的速度旋转。您可以创建一个包含所有必要信息(例如自转和公转速度)的结构,并使用该结构数据来创建所有行星。这样,在 if(spinMode) 中,您可以执行类似 mars.revolve() 的操作。这个想法是为那些刚接触 OpenGL 的人准备的。
You are actually rotating and translating all objects you have rendered. I recommend to increment each planet uniquely so they will rotate at different speed. You may create a structure bearing all the necessary information like rotation and revolution speed and use the structure data to create all the planets. In that way, in your if(spinMode), you can do something like mars.revolve(). This idea is for those new at OpenGL.