/******************************************************************** simple airplane implementation - based upon Angel's rotating cube program Edited by H. Masterman to illustrate building a simple airplane from instances of the color cube. rev. 0 2 November 2003 initial modification rev. 1 20 October 2009 modifications in class rev. 2 21 Octover 2009 added comments to capture what was done in class modified as needed during the class disucssion, which is often quite a great deal. This version is as modified for the class in Oct. 2009 to include the use of the MODELVIEW stack to simplify the code ********************************************************************/ #include #include GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; void polygon(int a, int b, int c , int d) { /* draw a polygon via list of vertices */ glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]); glEnd(); } void colorcube(void) { /* map vertices to faces */ polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); } static GLfloat theta1 = 0.0, theta2 = 0.0; //stores two angles void display(void) //This is where all of the interesting stuff is { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear color buffer and z buffer glLoadIdentity(); // start with an identity in the MODELVIEW transformation This is because //all of the other functions that affect the MODELVIEW matrix multiply it to the right. gluLookAt(0.0,3.0,12.0, 0.0,0.0,0.0,0,1,0); //multiply viewing component of transformation //This is multiplied on first so that it is applied last. //Next we multiply on the transformations that define the orbit of the plane glRotatef(theta1, 0,1,0); //rotate the plane about the y axis glTranslatef(0,0,7); //translate the plane +7 in z axis //Next we save this state, since it is the orbit transform and will be applied to the propeller as well glPushMatrix(); //draw body glScalef(3.0,1.0,1.0); //transformation multliplied on to make the body long in the x axis colorcube(); //draw the body //Pop the matrix to restore the earlier state (and get rid of the scaling for the body) glPopMatrix(); //Store this state for later use glPushMatrix(); //Multiply on a scaling for the wing, flat in the vertical and long in z glScalef(1.0,0.1, 4.0); //draw the wing colorcube(); //Pop the matrix to restore the state glPopMatrix(); //move the propeller to the front of the plane glTranslatef(3.1,0.0,0.0); //Rotate the propeller glRotatef(theta2, 1,0,0); //scale the cube to a propeller stape glScalef(.1,.1,2); //Draw the propeller colorcube(); glFlush(); // To make sure that all of the commands are executed, not just queued glutSwapBuffers(); //After drawing, make the drawing buffer the viewing buffer } void spinCube() { //idle callback. Angles are incremented by varying amounts each time it is called. theta1 += 0.9; // slow rotation if( theta1 > 360.0 ) theta1 -= 360.0; theta2 += 3.0; // fast rotation if (theta2 > 360.0) theta2 -= 360.0; glutPostRedisplay(); // generate a display event } void mouse(int btn, int state, int x, int y) { exit(0); //exit the program on any mouse click } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1,1,-1,1,1,30); glMatrixMode(GL_MODELVIEW); //leave the switch in the MODELVIEW position } void main(int argc, char **argv) { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("Simple Airplane"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glutMainLoop(); }