OpenGL glBegin和glPushMatrix

时间:2015-12-17 20:33:21

标签: c opengl glut

我尝试设计一个三个球体和一个水平线作为赤道的场景。我得画了3个球,但我不知道为什么这条线没有画。

这是我的代码,因为如果你能看到我错在哪里:

#include <GL/gl.h>
#include <GL/glut.h>

void render(void);

void reshape(int w, int h);

int angle = 90;

int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(50, 50);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Planets");

  glutDisplayFunc(render);
  glutReshapeFunc(reshape);

  glutMainLoop();
  return 0;
}


void render(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(0, 0, 0, 1);

  // Equator
  glBegin(GL_LINES);
  glColor3f(1,1,1);
  glLineWidth(1);
  glTranslated(0, 0, 0);
  glVertex2f(0, 2);
  glVertex2f(2,2);
  glEnd();

  // Sun
  glPushMatrix();
  glLoadIdentity();
  glColor3f(1.0, 1.0, 0.0);
  glTranslated(0, 0, -2);
  glRotated(angle, 1, 0, 0);
  glutWireSphere(.3, 20, 20);
  glPopMatrix();

  //Earth
  glPushMatrix();
  glLoadIdentity();
  glColor3f(0.0, 0.0, 1.0);
  glTranslated(0.7, 0, -2);
  glRotated(angle, 1, 0, 0);
  glutWireSphere(.15, 20, 20);
  glPopMatrix();

  // Moon
  glPushMatrix();
  glLoadIdentity();
  glColor3f(1.0, 0.0, 1.0);
  glTranslated(1, 0, -2);
  glRotated(angle, 1, 0, 0);
  glutWireSphere(.05, 10, 10);
  glPopMatrix();

  glutSwapBuffers();
}

void reshape(int w, int h) {
  const double ar = (double) w / (double) h;
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

1 个答案:

答案 0 :(得分:1)

指定在z = -2处具有近剪裁平面的平截头体。您的预期线将在z = 0处绘制,因此在投影体积之外,从而剪切为非渲染。

glTranslate(0,0,0)是无操作BTW。