Sara
Sara

Reputation: 823

Setting camera and frustum in OpenGL

Why isn't the quad rendered? I have tried with glOrtho and glFrustrum, and the output is the same: a black screen. From what I understand, this code says that the camera is set in position (0,0,-3) and its looking at the origin. The near plane is at -2 (1 m away from the camera). What am I doing wrong?

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

Upvotes: 3

Views: 6716

Answers (1)

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

It works fine for me, something must be wrong in another part of your code.

Here's the complete code I used:

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

I compiled it like this:

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

Here's a screenshot:

enter image description here

Upvotes: 4

Related Questions