I’ve just started OpenGL using the Red Book 7th Edition, and I’m coding it in an OOP approach with C++. I’m attempting to make a “Lego” at the moment, but after a long time figuring out how I wanted to design it, I figured out that I wasn’t even understanding the drawing concepts enough to pull it off. Anyway, I’ve got some code here that I don’t quite understand. I’ve got three different file, and I use freeglut for my windowing stuff. I’ll post the code, then my question. Note: some code is pointless, due to the fact I used the take files to learn from the book as I am trying to make a 3D rectangle. As you’ll see, there’s not 3D rectangle but only two squares. I’ll explain why after I post the code:
Main.cpp:
#include <GL/freeglut.h>
#include "Lego.h"
static GLfloat spin = 0.0;
Lego lego1(1.0, 0.0, 0.0, 4.0, 2.0, 1.0);
//Initialize the program
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Set default background color
glShadeModel(GL_FLAT); // Dunno yet <--
}
// The magic
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear Pixels
glPushMatrix(); // Dunno Yet <--
//glRotatef(30.0, 1.0, 1.0, 1.0);
lego1.draw();
glPopMatrix(); // Dunno yet <--
glutSwapBuffers(); // Switch to drawing next buffer when time is up
}
// Change rotation value
void spinDisplay() {
spin = spin + 2.0;
if(spin > 360.0) {
spin = spin - 360.0;
}
glutPostRedisplay(); // Tell glut to redraw screen
}
// Command what happens when window is resized
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h); // Change viewing area to continue to be the whole window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w/2, w/2, -h/2, h/2, -100.0, 100.0);
//glOrtho(-100, 100, -100, 100, -50.0, 50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Function for mouse input
void mouse(int button, int state, int x, int y) {
switch(button) {
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN) {
//glutIdleFunc(spinDisplay);
}
break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN) {
//glutIdleFunc(NULL);
}
break;
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Any commad line related stuff
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Sets display properties
glutInitWindowSize(250, 250); // Initial window size
glutInitWindowPosition(100, 100); // Initial window position
glutInitContextVersion(3, 0); // Use version 3.0
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE); // Make forward compatible
glutCreateWindow("Hello World"); // Create (but not display) window
init(); // Launch initializing function
glutDisplayFunc(display); // Set the display function
glutReshapeFunc(reshape); // Sets what happens when reshaped
glutMouseFunc(mouse); // Sets what happens with mouse input
glutMainLoop(); // Start the program
return 0;
}
Lego.h:
#ifndef _LEGO_H
#define _LEGO_H_
class Lego {
float r, g, b;
float l, w, h; // Each 1 unit = 5
float x, y, z;
float xRot, yRot, zRot;
public:
Lego(float, float, float, float, float, float);
void draw();
bool isMouseDown();
};
#endif
Lego.cpp:
#include <GL/freeglut.h>
#include "Lego.h"
Lego::Lego(float r, float g, float b, float l, float w, float h) {
this->r = r;
this->g = g;
this->b = b;
this->l = l;
this->w = w;
this->h = h;
this->xRot = 0.0;
this->yRot = 0.0;
this->zRot = 0.0;
// Temporary
this->x = 0.0;
this->y = 0.0;
this->z = 0.0;
}
void Lego::draw() {
glTranslatef(x, y, z);
GLfloat vertices[] = {-80, -80, 30, 1.0, 0.0, 0.0,
40, -80, 30, 1.0, 0.0, 0.0,
40, 40, 30, 1.0, 0.0, 0.0,
-80, 40, 30, 1.0, 0.0, 0.0,
-60, -60, -30, 0.0, 1.0, 0.0,
60, -60, -30, 0.0, 1.0, 0.0,
60, 60, -30, 0.0, 1.0, 0.0,
-60, 60, -30, 0.0, 1.0, 0.0,
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 6*sizeof(GL_FLOAT), &vertices[0]);
glColorPointer(3, GL_FLOAT, 6*sizeof(GL_FLOAT), &vertices[3]);
glDrawArrays(GL_QUADS, 0, 8);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glTranslatef(0, 0, 0);
glutSwapBuffers();
}
My main question is this:
No matter which set of Z points are negative (the green square’s or the red square’s), the green always appears in front. Why is that?