I am taking my first steps on OpenGL, and I have the following code written using GLUT Library:
The code was supposed to show 2 squares, connected to each other like an open book. So far so good. But when I try to rotate the image, the red square always overlaps the yellow one. Why is this happening?
Code:
#include <Carbon/Carbon.h>
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <iostream>
#include <cmath>
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_BACK,GL_FILL);
//glEnable(GL_CULL_FACE);
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,1.0f,0.0f);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(50,0,0);
glVertex3f(50,50,0);
glVertex3f(0,50,0);
glEnd();
glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(50,50,0);
glVertex3f(50,50,50);
glVertex3f(50,0,50);
glVertex3f(50,0,0);
glEnd();
glPopMatrix();
glFlush();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
// Set color shading model to flat
glShadeModel(GL_FLAT);
// Clock wise wound polygons are front facing, this is reversed
// because we are using triangle fans
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
}
///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);
else
glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
///////////////////////////////////////////////////////////
// Program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("OpenGL Test");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutSpecialFunc(SpecialKeys);
glutMainLoop();
SetupRC();
return 0;
}
The code was supposed to show 2 squares, connected to each other like an open book. So far so good. But when I try to rotate the image, the red square always overlaps the yellow one. Why is this happening?