OpenGL:动画时所有行星绕太阳的速度相同

发布于 2024-11-02 20:35:57 字数 2149 浏览 4 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚你发端 2024-11-09 20:35:57

您的呼叫顺序错误。所以每个行星都会沿着 y 轴绕其中心旋转。
以水星为例。

glTranslatef(2.0, 0.0, 0.0);//Move to new position
gluSphere(quad[1], 0.2f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0); //Rotate y axis

正确的顺序应该是

glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0); //Rotate y axis
glTranslatef(2.0, 0.0, 0.0); //Move to new position
gluSphere(quad[1], 0.2f, 64, 64);

但是,如果这是唯一的错误,您的行星将根本不会绕太阳旋转。使你的行星绕太阳旋转的是第二个错误,你忘记在与太阳进行变换之前推动矩阵。因此,每个星球都会继承这些转变。
正确的代码应该是

//Put every global transformation here


//Drawing the sun
glPushMatrix();
glColor3f(1.0, 1.0, 0.0);
glRotatef(360.0 * DayOfYear / 365.0, 0.0, 1.0, 0.0); //This will self-rotate the sun
gluSphere(quad[0], 1.f, 64, 64);
glPopMatrix();

//Drawing mercurius
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
gluSphere(quad[1], 0.2f, 64, 64);
glPopMatrix();

//Drawing the earth1
glPushMatrix();
glColor3f(0.2, 0.2, 1.0);
glRotatef(360.0*3*DayOfYear/365.0, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.6f, 64, 64);
glPopMatrix();

另外,您应该只在主渲染函数中使用 glutSwapBuffers() ,并在回调中使用 glutPostRedisplay() 。不要在同一函数中同时使用它们。

Your call order is wrong. So every planet will rotate around its center along the y axis.
Take mercurius for example.

glTranslatef(2.0, 0.0, 0.0);//Move to new position
gluSphere(quad[1], 0.2f, 64, 64);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0); //Rotate y axis

The correct order should be

glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0); //Rotate y axis
glTranslatef(2.0, 0.0, 0.0); //Move to new position
gluSphere(quad[1], 0.2f, 64, 64);

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

//Put every global transformation here


//Drawing the sun
glPushMatrix();
glColor3f(1.0, 1.0, 0.0);
glRotatef(360.0 * DayOfYear / 365.0, 0.0, 1.0, 0.0); //This will self-rotate the sun
gluSphere(quad[0], 1.f, 64, 64);
glPopMatrix();

//Drawing mercurius
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
gluSphere(quad[1], 0.2f, 64, 64);
glPopMatrix();

//Drawing the earth1
glPushMatrix();
glColor3f(0.2, 0.2, 1.0);
glRotatef(360.0*3*DayOfYear/365.0, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
gluSphere(quad[2], 0.6f, 64, 64);
glPopMatrix();

Also you should only use glutSwapBuffers() in your main rendering function, and glutPostRedisplay() in callbacks. Don't use them both in the same function.

怪异←思 2024-11-09 20:35:57

注意 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.

不…忘初心 2024-11-09 20:35:57

您必须在调用 glTranslateglRotate 之后实际渲染球体。现在,您正在中间执行此操作,因此它会忽略旋转!

例如:

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);

应该是

glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
gluSphere(quad[1], 0.2f, 64, 64); // <-- after you rotate!

You have to actually render the sphere after you call glTranslate and glRotate. Right now, you're doing it inbetween, so it'll just ignore the rotation!

For example:

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);

should be

glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
gluSphere(quad[1], 0.2f, 64, 64); // <-- after you rotate!
初熏 2024-11-09 20:35:57

您确定不是旋转对象本身,而是旋转它们相对于中心点的位置吗?尝试更改 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() and glPopMatrix().

灯下孤影 2024-11-09 20:35:57

您实际上是在旋转和平移您渲染的所有对象。我建议对每个行星进行独特的增量,这样它们就会以不同的速度旋转。您可以创建一个包含所有必要信息(例如自转和公转速度)的结构,并使用该结构数据来创建所有行星。这样,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文