开发者

How do I rotate the Earth about the Sun with OpenGL?

Please excuse my question

I spotted my misstake, and it is a dumb one. Everything is working ok. So how can I delete the question?


I can't seem to make an Earth rotate about a Sun with OpenGL and I have no idea as to why that is.

Here's a stripped down version of the (what I believe to be) relevant source code (in c++).

This is the Init() function that is called once before any other OpenGL function is called:

void Init() {

  glMatrixMode(GL_MODELVIEW);

  gluLookAt (
    0,  1, -3, // Where am I
    0,  0,  0, // Where am I looking at
    0,  1,  0  // Direction of 'my head'
  );

  // Going to Projection mode ...
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  // ... to set up perspective viewing.
  glFrustum   (-0.5,   0.5, 
               -0.5,   0.5, 
                1.0, 100.0);

  // Going back to Model view since drawing
  // is done entierly in Modelview.
  glMatrixMode(GL_MODELVIEW);
}

Then this is the Draw() function that is called repeadetly in order to "animate" the scene:

void Draw() {

  glClear(
    GL_COLOR_BUFFER_BIT | 
    GL_DEPTH_BUFFER_BIT
  );

  // Draw yellow Sun
  glColor3d(1.0, 1.0, 0.0);
  sphere(radius_sun);


  // draw the path of the Earth around
  // the Sun. Assume distance Earth to Sun = 1

  glColor4d( 0.0, 0.0, 1.0, 0.5);
  glBegin(GL_LINE_LOOP);

    for (double q=0; q<=1; q+=0.01) {
      double rad = q * M_PI * 2.0;
      glVertex3d(std::cos(rad), 0, std::sin(rad));
    }

  glEnd(); 

  // the Earth
  glColor3d(1.0, 1.0, 1.0);

  glPushMat开发者_开发百科rix();

  // Assign a value to t dependant on the
  // time already passed.
  double t = IncreasingDouble(); 

  double angle = t * M_PI * 2.0;

  // Rotate scene angle degres about y-axis:
  glRotated   (angle, 0, 1, 0);

  // And then translate coordinate system 
  glTranslated(1, 0, 0);

  sphere(radius_earth);

  // Restore matrix for next call
  glPopMatrix();

  // Swap Buffers and the more snipped..
}

I don't spot my mistake but the earth is moving spirally away from the sun further and further although the rest of the scene (Sun and Earth's orbit-path) are drawn correctly.


The problem must be elsewhere, maybe inside your sphere() method. I'm following the same steps as you, but mine works. Here's what I've got, and it's working the way you want (one sphere rotates smoothly around another) (Also, note my gluLookAt and glFrustum are different, but that should make no difference. I just do it this way so I can use my mouse to change my viewing point):

// draw the scene
void myGlutDisplay( void )
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    // projection transform
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1, 1, -1, 1, 1, 1000);

    // camera transform
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(eye[0], eye[1], eye[2], lookat[0], lookat[1], lookat[2], 0, 1, 0);


    //
    // draw some stuff
    //

    // Draw yellow Sun
    glColor3d(1.0, 1.0, 0.0);
    glutSolidSphere(1, 128, 128);

    glPushMatrix();
    double angle = (t+=.1) * 3.14156 * 2.0;
    glRotated(angle, 0, 1, 0);
    glTranslated(5, 0, 0);
    glutSolidSphere(1,128,128);
    glPopMatrix();

    glutSwapBuffers(); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜