// David Jackson // Project // CS 420 // project.cc /*************************************************************************** USER"S MANUAL This program runs dependant on user interaction. The following commands will result in the corresponding actions: '->' The right arrow will move the crane to the right '<-' The left arrow will move the crane to the left The up arrow will move the middle section of the crane up The down arrow will move the middle section of the crane down 'a' Will move the top part of the crane up 's' Will move the top part of the crane down 'i' Will zoom the camera Into the scene(-Z direction) 'o' Will zoom the camera Out the scene(+Z direction) 'l' Will zoom the camera to the the Left of the scene(-X direction) 'r' Will zoom the camera to the Right of the scene(+X direction) 'u' Will zoom the camera Up in the scene(-Y direction) 'd' Will zoom the camera Down in the scene(+Y direction) 'w' Will reset all variables to allow viewer to best Watch the jump 'j' Will cause the little man to jump off of the crane. ***************************************************************************/ #include #include #include /* OpenGL Utility Toolkit header */ #include static GLfloat angle = 40.0; static GLfloat angle2 = -10.0; static GLfloat place = 0.0; static GLfloat ballx = 0.0; static GLfloat bally = 0.0; static GLint jump = 0; static GLint dead = 0; static float zoomX = 0.0, zoomY = 10.0, zoomZ = 40.0; static GLfloat xpos = 0.0; static GLfloat ypos = 0.0; static GLfloat bottom = 0.0; static GLfloat final_position = 0; #define boxImageWidth 8 #define boxImageHeight 8 GLubyte boxImage[boxImageWidth][boxImageHeight][3]; static char *box[] = { "x.......", ".....xx.", "...xxx..", "xxx..x..", ".x.xx...", "........", "........", "........", }; void makeBoxImage(void){ //This function establishes the color scheme for the texture int s, t; for (t = 0; t < boxImageWidth; t++) { for (s = 0; s < boxImageHeight; s++) { if (box[t][s] == 'x') { boxImage[t][s][1] = 100; boxImage[t][s][3] = 10; boxImage[t][s][1] = 10; } if(box[t][s] == '.'){ boxImage[t][s][0] = 10; boxImage[t][s][1] = 200; boxImage[t][s][2] = 180; } } } } unitCube(){ //This function makes a basic 1 unit box with the origin at 1 // corner and point(1, 1, 1) at the opposite corner glTranslatef(0.5, 0.5, 0.5); glutSolidCube(1.0); } buildings(){ //draws a series of 4 buildings each with a different colors glPushMatrix(); glScalef(5.0, 10.0, 5.0); glPushMatrix(); glColor3f(0.7, 0.7, 0.7); glTranslatef(-10.0, 0.0, -9.0); unitCube(); glPopMatrix(); glPushMatrix(); glColor3f(0.4, 0.4, 0.4); glTranslatef(-3.0, 0.0, -5.0); unitCube(); glPopMatrix(); glPushMatrix(); glColor3f(0.56, 0.86, 0.86); glTranslatef(2.0, 0.0, -2.3); unitCube(); glPopMatrix(); glPushMatrix(); glColor3f(0.1, 0.7, 0.66); glTranslatef(-0.7, 0.0, -7.1); unitCube(); glPopMatrix(); glPopMatrix(); } littleMan(){ //draws a little person with 2 arms, 2 legs and a head glPushMatrix(); glColor3f(1, 0.0, 0.5); glTranslatef(-0.5, -0.5, 0.0); glPushMatrix(); glRotatef(-90.0, 1.0, 0.0, 0.0); glPushMatrix(); //right leg glRotatef(35.0, 0.0, 1.0, 0.0); glScalef(0.1, 0.1, 0.5) ; unitCube(); glPopMatrix(); glPushMatrix(); //right arm glRotatef(130.0, 0.0, 1.0, 0.0); glScalef(0.1, 0.1, 0.5) ; unitCube(); glPopMatrix(); glPushMatrix(); //left leg glRotatef(-35.0, 0.0, 1.0, 0.0); glScalef(0.1, 0.1, 0.5) ; unitCube(); glPopMatrix(); glPushMatrix(); //left arm glRotatef(-130.0, 0.0, 1.0, 0.0); glScalef(0.1, 0.1, 0.5) ; unitCube(); glPopMatrix(); glPopMatrix(); //head glTranslatef(0.0, 0.3, 0.0); glutSolidSphere(0.15, 6, 6); glPopMatrix(); glutPostRedisplay(); } splat(){ //this functions draws the splatter as the man hits the pavement dead = 1; glPushMatrix(); glDisable(GL_LIGHTING); glColor3f(1.0, 0.0, 0.0); glBegin(GL_QUADS); glVertex3f(-.7, 0.1, 1.0); glVertex3f(0.5, 0.1, 1.0); glVertex3f(0.5, 0.1, 2.0); glVertex3f(-0.7, 0.1, 2.0); glVertex3f(-1.0, 0.1, 1.5); glVertex3f(-0.1, 0.1, 0.85); glVertex3f(0.8, 0.1, 1.6); glVertex3f(0.12, 0.1, 2.3); glEnd(); glEnable(GL_LIGHTING); glPopMatrix(); } jumper(){ //This function determines the falling position of the jumper xpos = xpos + 0.1; ypos = ypos - 0.55; if(ypos <= - 11.5) { bottom = xpos + place; splat(); } glutPostRedisplay(); } crane(){ //This function draws a 3 segment crane glPushMatrix(); glTranslatef(-0.5, -0.0, -1.5); glPushMatrix(); //crane base glColor3f (1.0, 0.0, 0.0); glScalef(4.0, 2.0, 4.0); unitCube(); glPopMatrix(); glColor3f(0.0, 1.0, 0.0); glTranslatef(4.0, 1.0, 1.5); glRotatef(angle, 0.0, 0.0, 1.0); glPushMatrix(); //crane middle glScalef(7.0, 1.0, 1.0); unitCube(); glPopMatrix(); glColor3f(0.0, 0.0, 1.0); glTranslatef(7.0, 0.0, 0.0); glRotatef( angle2, 0.0, 0.0, 1.0); glPushMatrix(); //crane top glScalef(2.5, 1.0, 1.0); unitCube(); glPopMatrix(); glPopMatrix(); } divider(int j){ //This function draws the white divider for the road glPushMatrix(); glColor3f(1.0, 1.0, 1.0); glTranslatef(j, 0.0, 0.11); glScalef(2.5, 0.10, 0.3); unitCube(); glPopMatrix(); } road(){ //This function draws a black road, also calls the divider function glPushMatrix(); glDisable(GL_LIGHTING); glColor3f(0.1, 0.1, 0.1); glBegin(GL_QUADS); glVertex3f(-100.0, 0.1, -4.0); glVertex3f(-100.0, 0.1, 4.0); glVertex3f(100.0, 0.1, 4.0); glVertex3f(100.0, 0.1, -4.0); glEnd(); for(int j = -100; j <= 100; j = j + 5){ divider(j); } glEnable(GL_LIGHTING); glPopMatrix(); } streetLights(){ //This function draws a street light glPushMatrix(); glColor3f(0.5, 0.6, 0.7); glTranslatef(15.0, 0.0, -5.0); glPushMatrix(); //base of street light glRotatef(-90.0, 1.0, 0.0, 0.0); glutSolidCone(0.5, 5.0, 10, 10); glPopMatrix(); glColor3f(1.0, 1.0, 1.0); glTranslatef(0.0, 5., 0.0); glutSolidSphere(0.5, 15, 15); //top of street light glPopMatrix(); } ground(){ //draws a green ground cover like grass, it also calls the road function glPushMatrix(); glColor3f(0.0, 0.3, 0.01); glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-100.0, 0.0, -100.0); glTexCoord2f(0.0, 1.0); glVertex3f(-100.0, 0.0, 100.0); glTexCoord2f(1.0, 1.0); glVertex3f(100.0, 0.0, 100.0); glTexCoord2f(1.0, 0.0); glVertex3f(100.0, 0.0, -100.0); glEnd(); glDisable(GL_TEXTURE_2D); road(); glEnable(GL_LIGHTING); glPopMatrix(); } void init (void){ //This function sets the initialal values for the program glEnable(GL_DEPTH_TEST); static float ambient[] = {0.1, 0.1, 0.1, 1.0}; static float diffuse[] = {1.0, 1.0, 1.0, 1.0}; static float position[] = {15.0, 0.0, 10.0, 0.0}; GLfloat glob_amb[]= {0.1, 0.1, 0.1, 0.1}; glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glob_amb); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); makeBoxImage(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, 3, boxImageWidth, boxImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, &boxImage[0][0][0]); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glClearColor (1.0, 1.0, 1.0, 0.0); glShadeModel(GL_SMOOTH); gluLookAt(15.0, 25.0, 80.0, 15.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void display(void){ //This function calls the objects to be displayed glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(zoomX, zoomY, zoomZ, 0.0, 0.0, -1000.0, 0.0, 1.0, 0.0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); ground(); streetLights(); glPushMatrix(); glTranslatef(place, 0.0, 0.0); crane(); glPopMatrix(); glPushMatrix(); buildings(); glTranslatef(34.3, 0.0, -1.28); buildings(); glPopMatrix(); glPushMatrix(); //condition to allow man to commit suicide glTranslatef(place + 4.0, 11.5, 0.0); if(bottom == 0.0) { if( (angle == 90) && (angle2 == 0) && (jump == 1) ){ glTranslatef(xpos, ypos, 0.0); jumper(); littleMan(); } } glPopMatrix(); glPushMatrix(); if(dead == 1){ glTranslatef(bottom + 4.0, 0.1, 0.0); glRotatef(90.0, 1.0, 0.0, 0.0); glRotatef(-90.0, 0.0, 0.0, 1.0); littleMan(); } glPopMatrix(); glutSwapBuffers(); } void reshape (int w, int h){ //This function reshapes the display glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); } void keyboard (unsigned char key, int x, int y){ //This function allows user to change conditions for the display switch (key) { case 'a': //raise upper section of crane if( angle2 >= 0) angle2 = -10; angle2 += 10.0; glutPostRedisplay(); break; case 's': //lower upper section of crane if( angle2 == -80) angle2 = -70; angle2 -= 10.0; glutPostRedisplay(); break; case 'j': //Allows the person to jump off the crane jump = 1; glutPostRedisplay(); break; case 'i': //set viewing position closer on z-axis zoomZ --; glutPostRedisplay(); break; case 'o': //set viewing position further on z-axis zoomZ ++; glutPostRedisplay(); break; case 'r': //set viewing position more to the right zoomX ++; glutPostRedisplay(); break; case 'l': //set viewing position more to the left zoomX --; glutPostRedisplay(); break; case 'u': //set viewing position lower on y-axis zoomY ++; glutPostRedisplay(); break; case 'd': //set viewing position lower on y-axis zoomY --; glutPostRedisplay(); break; case 'w': //sets values to the watch position zoomX = 6.0; zoomY = 4.0; zoomZ = 14.0; place = 0; angle = 90; angle2 = 0; jump = 0; dead = 0; bottom = 0; xpos = 0; ypos = 0; glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } void specialkey(int key, int x, int y) { switch(key){ case GLUT_KEY_LEFT: //move crane to the right place -= 0.3; glutPostRedisplay(); break; case GLUT_KEY_RIGHT: //move crane to the left place += 0.3; glutPostRedisplay(); break; case GLUT_KEY_UP: //raise crane's mid section if( angle == 90) angle = 80; angle += 10.0; glutPostRedisplay(); break; case GLUT_KEY_DOWN: //lower crane's mid section if( angle == 20) angle = 30; angle -= 10; glutPostRedisplay(); break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(specialkey); glutMainLoop(); return 0; }