4次旋转后,物体返回初始位置

时间:2015-12-03 16:48:35

标签: c++ opengl

我有一个OpenGL / C ++的太阳系,我的行星在围绕太阳转了4圈后继续从初始位置重新启动,我无法弄清楚原因。

我认为它与使其重新启动的DayOfYear变量有关,但我尝试改变它并且没有取得任何成功,所以我希望有人可以在这里帮助我。所以我的问题是,为什么我的行星在4次旋转后回到初始位置,我怎么能阻止它呢?

以下是我的主要绘图功能:

static void Animate(void)
{
    // Clear the redering window
    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;
    }

    // Clear the current matrix (Modelview)
    glLoadIdentity();

    // Back off eight units to be able to view from the origin.
    glTranslatef ( 0.0, 0.0, -40.0 );

    // Rotate the plane of the elliptic
    // (rotate the model's plane about the x axis by fifteen degrees)
    glRotatef( 15.0, 1.0, 0.0, 0.0 );

    // Draw the sun -- as a yellow, wireframe sphere
    glColor3f( 1.0, 1.0, 0.0 );
    glutWireSphere( 4.0, 15, 15 );

    /*
     |
     | MERCURY
     |
     */
    glPushMatrix();
    // Draw the Earth
    // First position it around the sun
    //      Use DayOfYear to determine its position
    glRotatef( 360.0*DayOfYear/88.0, 0.0, 1.0, 0.0 );
    glTranslatef( 6, 0.0, 0.0 );
    // Save matrix state
    // Second, rotate the earth on its axis.
    //      Use HourOfDay to determine its rotation.
    glRotatef( 360.0*HourOfDay/24.0, 0.0, 1.0, 0.0 );
    // Third, draw the earth as a wireframe sphere.
    glColor3ub(255,255,255);
    glutWireSphere( 0.1, 10, 10);
    glPopMatrix();



    // Flush the pipeline, and swap the buffers
    glFlush();
    glutSwapBuffers();

    glutPostRedisplay();        // Request a re-draw for animation purposes

}

1 个答案:

答案 0 :(得分:1)

好吧,我想如果你想用天做轨道的话 按小时旋转地球。

在这里你试着添加到HourOfDayelnúmerodehoras que han pasado son 24这很好。

HourOfDay  + =  AnimateIncrement; 
DayOfYear   + =  AnimateIncrement / 24.0;

删除它总是返回零。

HourOfDay  =  HourOfDay  -  ((int) (HourOfDay / 24)) * 24; 
DayOfYear   =  DayOfYear   -  ((int) (DayOfYear  / 365)) * 365;

嗯,这条线对88的划分没有多大意义。

glRotatef(360.0*DayOfYear/88, 0.0, 1.0, 0.0 );

每356分。

glRotatef(360.0*DayOfYear/365, 0.0, 1.0, 0.0 );