3

为什么不渲染四边形?我尝试过 glOrtho 和 glFrustrum,输出是一样的:黑屏。据我了解,这段代码表示相机设置在位置 (0,0,-3) 并且它正在查看原点。近平面在 -2 处(距离相机 1 m)。我究竟做错了什么?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//glOrtho (-5,5,-5,5,-5,5);
glFrustum(-5,5,-5,5,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-3,0,0,0,0,1,0);

glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);                                  
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
// Don't wait start processing buffered OpenGL routines
glFlush();
glutSwapBuffers();
4

1 回答 1

4

它对我来说很好,你的代码的另一部分一定有问题。

这是我使用的完整代码:

#include <GL/glut.h>

void display(void) {

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  //glOrtho (-5,5,-5,5,-5,5);
  glFrustum(-5,5,-5,5,1,1000);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0,0,-3,0,0,0,0,1,0);

  glColor3f(1.0,1.0,1.0);
  glBegin(GL_QUADS);                                  
  glVertex3f(-0.5,-0.5,-0.5);
  glVertex3f(0.5,-0.5,-0.5);
  glVertex3f(0.5,0.5,-0.5);
  glVertex3f(-0.5,0.5,-0.5);
  glEnd();
  // Don't wait start processing buffered OpenGL routines
  glFlush();
  glutSwapBuffers();

}

int main(int argc, char **argv) {
  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);  
  glutInitWindowPosition(200,200);
  glutInitWindowSize(512,512);
  glutCreateWindow("Test");
  glutDisplayFunc(display);

  glutMainLoop();

  return 0;
}

我是这样编译的:

gcc test.cpp -lGL -lglut -lGLU -o test

这是一个屏幕截图:

在此处输入图像描述

于 2012-05-19T23:20:30.607 回答