如何使用 OpenGL 使地球绕太阳旋转?

发布于 2024-10-16 04:51:09 字数 1826 浏览 0 评论 0原文

请原谅我的问题

我发现了我的错误,而且这是一个愚蠢的错误。一切正常。那么我怎样才能删除这个问题呢?


我似乎无法使用 OpenGL 让地球绕太阳旋转,我也不知道为什么会这样。

这是(我认为是)相关源代码(C++ 语言)的精简版本。

这是 Init() 函数,在调用任何其他 OpenGL 函数之前被调用一次

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

然后这是 Draw() 函数,它是重复调用以便“动画”场景:

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

  glPushMatrix();

  // 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..
}

我没有发现我的错误,但地球正在螺旋式地远离太阳越来越远尽管场景的其余部分(太阳和地球的轨道-路径)绘制正确。

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

  glPushMatrix();

  // 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

桃酥萝莉 2024-10-23 04:51:09

问题一定出在其他地方,也许在你的 sphere() 方法内部。我正在按照与您相同的步骤操作,但我的有效。这是我得到的,它按照你想要的方式工作(一个球体围绕另一个球体平滑旋转)(另外,请注意我的 gluLookAt 和 glFrustum 是不同的,但这应该没有什么区别。我只是这样做,这样我就可以使用我的鼠标改变我的观察点):

// 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(); 
}

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