Somehow I've managed to screw up on lesson 05. I was able to play around with lessons 1 to 4 properly with no peculiar effect but with lesson 5....
**Read Edit at the end first**
Part of my
Render() function:
glLoadIdentity();
static float rcube = 0.0f;
glTranslatef(1.5f,0.0f,-10.0f);
glRotatef(rcube, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glEnd();
Screenshot 1
Ok, so I had no clue why the top and bottom quads turned red with no front quad at all (similar effect for the pyramid).
In order to experiment, I tried with putting the Z vertext = 0.0f instead of 1.0f for the FRONT quad (which should show 5 sides of the cube right and the front side halfway inside the box), now look at what happens:
Screenshot 2
So basically the front quad is appearing superimposed on the top and bottom quads instead of showing upright... now what maybe causing this effect?
Note: When making 2D images in previous lessons all worked fine, so there shouldnt be any problem with the OpenGL initializations (I'm using GLUT btw)
** EDIT ** :
Ok after a bit of playing around I found the problem. The problem is that because the quad for the BACK is after the one for the FRONT, the FRONT one gets rendered first, and then the BACK one gets rendered on top of it... even if it's Z co-ords are furthur away, the rendering overlaps the FRONT quad... same with other sides... so I think I'm getting somekind of unwanted transperency... any idea how to opaquify this? :)
Thanks.
** EDIT 2 **
It is indeed, a problem with transperency... if I put the code for the FRONT pane after all but the sides, here is what I get:
Screenshot 3
Maybe I initialized OpenGL wrongly? Here is how I initialized:
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("OpenGL Test");
glutReshapeFunc(ResizeWindow);
glutDisplayFunc(Render);
glutKeyboardFunc(KeyboardEvent);
glutIdleFunc(Idle);
glutMainLoop();
return EXIT_SUCCESS;
}
void ResizeWindow(int width, int height){
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}