I am trying to draw a simple quad with OpenGL.The same code wouldn’t fail if I weren’t using the depth and draw the quad with 0.0 as z coordinate.
It seems like the depth isn’t enabled or whatsoever:
#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
int width=500, height=500, depth=500;
void init()
{
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, width, height, 0);
gluLookAt(250, 250, 250, 250, 250, -250, 0, 1, 0);
gluPerspective(45, 1, 1.0, 200.0);
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1, 0, 0, 0);
glBegin(GL_QUADS);
glVertex3i(100, 100, 100);
glVertex3i(100, 300, 100);
glVertex3i(300, 300, 100);
glVertex3i(300, 100, 100);
glEnd();
glutSwapBuffers();
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow("Test");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
I just see a black window, but I should see a red quad inside it.
Change your
init()function to:Your problem is that
gluLookAt()should be used to define the model-view matrix, but you are multiplying the projection one. Also theglViewport()call is unrelated to the matrix calculations, so I moved it to the beggining of the function. Not really needed, but I find that clearer.