Draw A Cube With C
Draw A Cube With C
#include <GL/gl.h>
#include <GL/glut.h>
int rotateX = 15, rotateY = -15, rotateZ = 0; // rotation amounts about axes,
controlled by keyboard
glPushMatrix();
glScalef(size,size,size); // scale unit cube to desired size
glPushMatrix();
glRotatef(90, 0, 1, 0);
square(0, 1, 0); // green right face
glPopMatrix();
glPushMatrix();
glRotatef(-90, 1, 0, 0);
square(0, 0, 1); // blue top face
glPopMatrix();
glPushMatrix();
glRotatef(180, 0, 1, 0);
square(0, 1, 1); // cyan back face
glPopMatrix();
glPushMatrix();
glRotatef(-90, 0, 1, 0);
square(1, 0, 1); // magenta left face
glPopMatrix();
glPushMatrix();
glRotatef(90, 1, 0, 0);
square(1, 1, 0); // yellow bottom face
glPopMatrix();
// ----------------------------------------------------------------------
void display() {
// called when the display needs to be drawn
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cube(1);
glutSwapBuffers();
}
void initGL() {
// called by main() to do initialization for this program.
glMatrixMode(GL_PROJECTION);
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
}