I’ve been trying to get 3d-scenes with opengl and glut. At the moment, I have a piece of code (basic) to get more and more familiar with glut and opengl (writing in C). But there is a problem with that code, because I draw a floor (GL_QUAD) and four walls of a cube (left, right, front, behind), but when running the code, it seems that the latter QUADS drawn are overlapping the former QUADS. Is this a known problem? I don’t see what I’ve done wrong here.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
GLfloat step1 = 0.0;
GLfloat step2 = 0.0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
gluLookAt (0.0, 8.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glRotatef(step1, 1.0, 0.0, 0.0);
glRotatef(step2, 0.0, 1.0, 0.0);
glColor3f (1.0, 1.0, 1.0);
glBegin (GL_QUADS);
glVertex3f (-8, 0, -10);
glVertex3f (8, 0, -10);
glVertex3f (8, 0, 10);
glVertex3f (-8, 0, 10);
glEnd ();
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-3.0, 0.0, -3.0);
glVertex3f(-3.0, 9.0, -3.0);
glVertex3f(-3.0, 9.0, 3.0);
glVertex3f(-3.0, 0.0, 3.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-3.0, 0.0, 3.0);
glVertex3f(-3.0, 9.0, 3.0);
glVertex3f(3.0, 9.0, 3.0);
glVertex3f(3.0, 0.0, 3.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 9.0, 3.0);
glVertex3f(3.0, 9.0, -3.0);
glVertex3f(3.0, 0.0, -3.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(3.0, 0.0, -3.0);
glVertex3f(3.0, 9.0, -3.0);
glVertex3f(-3.0, 9.0, -3.0);
glVertex3f(-3.0, 0.0, -3.0);
glEnd();
glFlush();
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 40.0);
glMatrixMode (GL_MODELVIEW);
}
void keyPressed(unsigned char key, int x, int y)
{
switch(key)
{
case 'q':
case 27 :
exit(1);
break;
case 'a':
step2 += 1.5;
break;
case 'd':
step2 -= 1.5;
break;
case 'w':
step1 += 1.5;
break;
case 's':
step1 -= 1.5;
break;
case 'r':
step1 = 0.0;
step2 = 0.0;
break;
}
if(step1 > 360) step1 -= 360;
else if(step1 < 360) step1 += 360;
if(step2 > 360) step2 -= 360;
else if(step2 < 360) step2 += 360;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Biljart");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyPressed);
glutMainLoop();
return 0;
}
I think I’m doing something really stupid, but I just don’t see what:P