Created
October 17, 2011 14:15
-
-
Save benjgorman/1292678 to your computer and use it in GitHub Desktop.
Two spinning OpenGl Cubes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <gl/freeglut.h> | |
GLfloat angle = 0.0; | |
void cube(void) { | |
//front | |
glColor3f(1.0,0.0,0.0); | |
glBegin(GL_POLYGON); | |
glNormal3f(0.0f, 0.0f, 1.0f); | |
glVertex3f(0.5f,0.5f,0.5f); | |
glVertex3f(-0.5f,0.5f,0.5f); | |
glVertex3f(-0.5f,-0.5f,0.5f); | |
glVertex3f(0.5f,-0.5f,0.5f); | |
glEnd(); | |
//right | |
glColor3f(0.0,1.0,0.0); | |
glBegin(GL_POLYGON); | |
glNormal3f(1.0f, 0.0f, 0.0f); | |
glVertex3f(0.5,0.5,0.5); | |
glVertex3f(0.5,-0.5,0.5); | |
glVertex3f(0.5,-0.5,-0.5); | |
glVertex3f(0.5,0.5,-0.5); | |
glEnd(); | |
//back | |
glColor3f(0.0,1.0,1.0); | |
glBegin(GL_POLYGON); | |
glNormal3f(0.0f, 0.0f, -1.0f); | |
glVertex3f(0.5,0.5,-0.5); | |
glVertex3f(0.5,-0.5,-0.5); | |
glVertex3f(-0.5,-0.5,-0.5); | |
glVertex3f(-0.5,0.5,-0.5); | |
glEnd(); | |
//left | |
glColor3f(1.0,0.0,1.0); | |
glBegin(GL_POLYGON); | |
glNormal3f(-1.0f, 0.0f, 0.0f); | |
glVertex3f(-0.5,0.5,0.5); | |
glVertex3f(-0.5,0.5,-0.5); | |
glVertex3f(-0.5,-0.5,-0.5); | |
glVertex3f(-0.5,-0.5,0.5); | |
glEnd(); | |
//top | |
glColor3f(0.0,0.0,1.0); | |
glBegin(GL_POLYGON); | |
glNormal3f(0.0f, 1.0f, 0.0f); | |
glVertex3f(0.5,0.5,0.5); | |
glVertex3f(0.5,0.5,-0.5); | |
glVertex3f(-0.5,0.5,-0.5); | |
glVertex3f(-0.5,0.5,0.5); | |
glEnd(); | |
//bottom | |
glColor3f(1.0,1.0,0.0); | |
glBegin(GL_POLYGON); | |
glNormal3f(0.0f, -1.0f, 0.0f); | |
glVertex3f(0.5,-0.5,0.5); | |
glVertex3f(-0.5,-0.5,0.5); | |
glVertex3f(-0.5,-0.5,-0.5); | |
glVertex3f(0.5,-0.5,-0.5); | |
glEnd(); | |
} | |
void display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
// set up the camera position | |
gluLookAt(0.0, 10.0, -20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
angle+=0.1f; | |
glRotatef(angle, 0.0, 1.0, 0.0); | |
cube(); | |
glTranslatef(2.0f, 0, 0); | |
glutSolidCube(1); | |
glutSwapBuffers(); | |
} | |
void init(void) | |
{ | |
static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0}; | |
glClearColor (0.0, 0.0, 0.0, 0.0); | |
glEnable(GL_DEPTH_TEST); | |
glShadeModel(GL_FILL); | |
glLightfv(GL_LIGHT0, GL_POSITION, pos); | |
glEnable(GL_CULL_FACE); | |
glEnable(GL_LIGHTING); | |
glEnable(GL_LIGHT0); | |
glEnable(GL_DEPTH_TEST); | |
} | |
/* new window size or exposure */ | |
static void reshape(int width, int height) | |
{ | |
GLfloat h = (GLfloat) height / (GLfloat) width; | |
glViewport(0, 0, (GLint) width, (GLint) height); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glFrustum(-1.0, 1.0, -h, h, 5.0, 400.0); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
} | |
void keyboard(unsigned char key, int x, int y) | |
{ | |
x=y; | |
switch (key) { | |
case 27: | |
exit(0); | |
break; | |
case 'x': | |
// Modify some variable | |
glutPostRedisplay(); | |
break; | |
} | |
} | |
void mouse (int button, int state, int x, int y) | |
{ | |
x=y; | |
switch (button) { | |
case GLUT_LEFT_BUTTON: | |
if (state == GLUT_DOWN) { | |
angle = angle + 10; | |
glutPostRedisplay(); | |
} | |
break; | |
case GLUT_RIGHT_BUTTON: | |
if (state == GLUT_DOWN) { | |
angle = angle - 10; | |
glutPostRedisplay(); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
static void idle(void) | |
{ | |
glutPostRedisplay(); | |
} | |
int main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE); | |
glutInitWindowSize (500, 500); | |
glutInitWindowPosition (100, 100); | |
glutCreateWindow ("Simple 3-d program"); | |
init (); | |
glutDisplayFunc(display); | |
glutIdleFunc(idle); | |
glutReshapeFunc(reshape); | |
glutKeyboardFunc(keyboard); | |
glutMouseFunc(mouse); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment