#include #include #include int sWidth = 640; int sHeight = 480; int freez = 0; float spin = 0; // Angle For The Quad int mouseLastX=0; int mouseLastY=0; float yrot = 0; float xrot = 0; float xpos = 0; float zpos = 0; float zAcc = 0; float zMax = 0.04f; float speedAcc = 0.01f; float slowAcc = 0.0002f; float playerHigh = 0; float inMovement = 0; const float piover180 = 0.0174532925f; /////////////////////////////////////////// SETUPS void init (void) { glClearColor (.95,.95,.95,0); glShadeModel (GL_SMOOTH); glEnable (GL_DEPTH_TEST); GLfloat light_position[]= {10,20,5,0}; glLightfv(GL_LIGHT0,GL_POSITION,light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } void reshape (int w,int h) { glViewport (0,0,w,h); // Set the size of the render according to the window's size glMatrixMode (GL_PROJECTION); // go to the Projection Stack glLoadIdentity (); // Reset The View gluPerspective (90,1,1.5,1000); // set its Persective to Fov=90, Ratio=1, NearClip=1.5, FarClip=1000 glMatrixMode (GL_MODELVIEW); // go back in the Model Stack glLoadIdentity(); // and reset it } /////////////////////////////////////////// OBJECTS void drawGrid (int size, int step) { float x,z; glColor3f (.7,.7,.7); glDisable (GL_LIGHTING); glBegin (GL_LINES); for (z=-size; z -zMax){ // MOVE Backward zAcc -= speedAcc; } } if (key == 'f'){ // FREEZ if(freez == 0){ freez = 1;} else{ freez = 0; } } } void mouseMotion (int x, int y) { if(freez == 0){ yrot -= ((x-320)-mouseLastX)*0.4f; xrot -= ((y-240)-mouseLastY)*0.2f; if(xrot > 70){ xrot = 70; } if(xrot < -70){ xrot = -70; } mouseLastX = x-320; mouseLastY = y-240; } } void computeMovements (void) { xpos -= sin( yrot * piover180 ) * zAcc; zpos -= cos( yrot * piover180 ) * zAcc; if(zAcc > slowAcc){ zAcc -= slowAcc; inMovement += (zAcc*0.4f); playerHigh = sin(inMovement); }else if(zAcc < -slowAcc){ zAcc += slowAcc; inMovement += (zAcc*0.4f); playerHigh = sin(inMovement); }else{ zAcc = 0; if(playerHigh > 0.005f){ playerHigh -= 0.005f; }else if(playerHigh < - 0.005f){ playerHigh += 0.005f; }else{ playerHigh = 0; } } } /////////////////////////////////////////// DRAW void draw (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View //gluLookAt (0, 5, 5, 0, 5, 0, 0, 1, 0); // define a viewing transformation // (eyePosition) (centerReferencePosition) (directionVector) glRotatef (-xrot, 1, 0, 0); // Rotate depending on head's slope glRotatef (-yrot, 0, 1, 0); // Rotate depending on direction player is facing glTranslatef(-xpos, playerHigh-10, -zpos); // Move depending on the player's position drawGrid (120, 5); glRotatef (spin, 0, 1, 0); glColor3f (1,1,1); glutSolidCube (10); glutSwapBuffers (); } void idle (void) { spin+=0.2f; if (spin>360.0){ spin=spin-360; } computeMovements(); glutPostRedisplay (); } /////////////////////////////////////////// MAIN int main (int argc,char**argv) { glutInit (&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (sWidth, sHeight); glutInitWindowPosition ((1600/2)-(640/2), (1200/2)-(480/2)); glutCreateWindow ("XPac3DE v.1"); init (); glutDisplayFunc (draw); glutReshapeFunc (reshape); glutKeyboardFunc (keyboard); glutPassiveMotionFunc (mouseMotion); glutIdleFunc (idle); glutMainLoop(); return 0; }