double cameraPos[3] = {0, 0, -100};
double cameraRot[3] = {30, -30, 0};
void begin3d() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(60.0, (double)windowGetWidth()/(double)windowGetHeight(), 0.1, 10000.0);
const double mm[16] = {
0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1 };
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslated(cameraPos[0], cameraPos[1], cameraPos[2]);
glRotated(cameraRot[0], 1, 0, 0);
glRotated(cameraRot[1], 0, 1, 0);
glRotated(cameraRot[2], 0, 0, 1);
glMultMatrixd(mm);
float dir[4] = { 1, 1, 1, 0 };
float k = 1.f/sqrtf(dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2]);
dir[0] *= k; dir[1] *= k; dir[2] *= k;
glLightfv(GL_LIGHT0, GL_POSITION, dir);
}
void end3d() {
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
}
void cubeDraw(double s) {
s *= 0.5;
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glNormal3d(0, 0, -1);
glVertex3d(-s, -s, -s);
glVertex3d(-s, s, -s);
glVertex3d( s, s, -s);
glVertex3d( s, -s, -s);
glNormal3d(0, 0, 1);
glVertex3d(-s, -s, s);
glVertex3d( s, -s, s);
glVertex3d( s, s, s);
glVertex3d(-s, s, s);
glColor3d(0, 1, 0);
glNormal3d(0, -1, 0);
glVertex3d(-s, -s, -s);
glVertex3d( s, -s, -s);
glVertex3d( s, -s, s);
glVertex3d(-s, -s, s);
glNormal3d(0, 1, 0);
glVertex3d(-s, s, -s);
glVertex3d(-s, s, s);
glVertex3d( s, s, s);
glVertex3d( s, s, -s);
glColor3d(0, 0, 1);
glNormal3d(-1, 0, 0);
glVertex3d(-s, -s, -s);
glVertex3d(-s, -s, s);
glVertex3d(-s, s, s);
glVertex3d(-s, s, -s);
glNormal3d(1, 0, 0);
glVertex3d( s, -s, -s);
glVertex3d( s, s, -s);
glVertex3d( s, s, s);
glVertex3d( s, -s, s);
glEnd();
glColor3d(1, 1, 1);
}