/* OpenGL Tutor : Cube Copyright (c) 1999 Nate Miller File -- cube.c Date -- 6/22/99 Author -- Nate 'm|d' Miller Contact -- vandals1@home.com Web -- http://nate.scuzzy.net If you want to compile this make sure that you link too the following libraries: opegl32.lib glu32.lib glut32.lib */ #define APP_NAME "OpenGL Tutor : Cube" #include //#include #include /* This is my vertex structure 0 = x, 1 = y, 2 = Z */ typedef float vec3_t[3]; /* Window width and height */ int winW = 640; int winH = 640; /*480;*/ /* Rendering state variables */ int aniOn = 0; int fillOn = 1; /* Vertex data for our cube */ vec3_t cube[] = { {-10,-10, 10}, { 10,-10, 10}, { 10, 10, 10}, {-10, 10, 10}, {-10,-10,-10}, { 10,-10,-10}, { 10, 10,-10}, {-10, 10,-10}, {-10, 10, 10}, { 10, 10, 10}, { 10, 10,-10}, {-10, 10,-10}, {-10,-10, 10}, { 10,-10, 10}, { 10,-10,-10}, {-10,-10,-10}, {-10,-10,-10}, {-10,-10, 10}, {-10, 10, 10}, {-10, 10,-10}, { 10,-10,-10}, { 10,-10, 10}, { 10, 10, 10}, { 10, 10,-10}, }; /* Colors for our cube */ vec3_t colors[] = { {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 1.0f}, }; /* Amount to rotate */ vec3_t rot = {0,0,0}; /* Position of our eye or camera */ vec3_t eye = {0,0,30}; /* ============= drawCube ============= Draws the colored cube. There are 24 verts in the cube data above so we have to go through the array 24 times. This will result in 6 faces which make up our cube. */ void drawCube(void) { int i; glBegin(GL_QUADS); for (i = 0; i < 24; i ++) { glColor3fv(colors[i % 4]); glVertex3fv(cube[i]); } glEnd(); } /* ============= glutDisplay ============= Our main display function. This will clear the color and depth buffers. After that it will translate the world to our eye position and rotate it by the rotation amounts. Finally the cube is drawn and the buffers are swapped. */ void glutDisplay( void ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( -eye[0], -eye[1], -eye[2] ); glRotatef( rot[0], 1.0f, 0.0f, 0.0f ); glRotatef( rot[1], 0.0f, 1.0f, 0.0f ); glRotatef( rot[2], 0.0f, 0.0f, 1.0f ); drawCube(); glutSwapBuffers(); } /* ============= clamp ============= Used to keep the rotations in the range of -360 to 360 */ void clamp( vec3_t p ) { int i; for (i = 0; i < 3; i ++) if (p[i] >= 360 || p[i] < -360) p[i] = 0; } /* ============= glutIdle ============= The idle function that animates our cube. Advances the rotation variables. */ void glutIdle(void) { if (aniOn) { rot[0] += 0.7f; rot[1] += 0.5f; rot[2] += 0.3f; clamp(rot); glutPostRedisplay(); } } /* ============= glutResize ============= Called when the application starts by default and when the window is resized. When called the winW and winH are updated. Then the depth and color buffers are cleared and a viewing projection is established. The viewing projection needs to be set only when the width and height of the window change. */ void glutResize (int w, int h) { if (!h) return; winW = w; winH = h; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glViewport( 0, 0, winW, winH ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 90, winW / winH, 1, 9999 ); glutPostRedisplay(); } /* ============= glutKeyboard ============= Handles keyboard input. */ void glutKeyboard( unsigned char key, int x, int y ) { switch (key) { case ' ': aniOn = !aniOn; break; case 'f': case 'F': fillOn = !fillOn; if (fillOn) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL ); else glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glutPostRedisplay(); /*BB October 29, 2001 */ break; case 'h': case 'H': printf("%s\n", APP_NAME); printf("Space Bar -- On/Off Animation\n"); printf("f -- On/Off Wireframe\n"); break; } } /* ============= glInit ============= Sets up depth test and tells opengl to fill our polys. */ void glInit(void) { glEnable( GL_DEPTH_TEST ); glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); } int main( int argc, char** argv ) { glutInit( &argc, argv ); /*BB October 16, 2001 */ glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); glutInitWindowSize( winW, winH ); glutCreateWindow( APP_NAME ); /* "Cube"); */ glutDisplayFunc( glutDisplay ); glutReshapeFunc( glutResize ); glutIdleFunc( glutIdle ); glutKeyboardFunc( glutKeyboard ); glInit(); glutMainLoop(); return 0; }