void DrawGLScene(void) { double stretch,height,position,rotation; glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(5.0, 5.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); position=cos((double)frame*speed)*2; height=fabs(sin((double)frame*speed)*10)+1; rotation=cos((double)frame*speed)*90; stretch=1+0.3*sin((double)frame*speed/2); if(useMyTransformations) { if(doTranslate) myTranslatef(position,height-5,0.0); if(doRotate) myRotatef(rotation,0.2,0.6,0.77); if(doScale) myScalef(1/sqrt(stretch),stretch,1/sqrt(stretch)); } else { if(doTranslate) glTranslatef(position,height-5,0.0); if(doRotate) glRotatef(rotation,0.2,0.6,0.77); if(doScale) glScalef(1/sqrt(stretch),stretch,1/sqrt(stretch)); } //drawCube(); drawTeapot(); frame++; glutSwapBuffers(); }
void rotateCameraLeft(float degrees, float *eyePosition, float *centerPosition, float *upVector){ // Please implement this function. GLdouble gaze[3] = { centerPosition[0] - eyePosition[0], centerPosition[1] - eyePosition[1], centerPosition[2] - eyePosition[2] }; GLdouble normGaze[3]; double gazeLength; GLdouble normUp[3]; double upLength; GLdouble right[3]; GLdouble normRight[3]; double rightLength; GLfloat rotate[16]; GLfloat rotEye[3]; GLfloat rotUp[3]; gazeLength = sqrt(SQR(gaze[0]) + SQR(gaze[1]) + SQR(gaze[2])); for (int i = 0; i < 3; i++) { normGaze[i] = gaze[i] / gazeLength; } upLength = sqrt(SQR(upVector[0]) + SQR(upVector[1]) + SQR(upVector[2])); for (int i = 0; i < 3; i++) { normUp[i] = upVector[i] / upLength; } right[0] = normGaze[1] * normUp[2] - normGaze[2] * normUp[1]; right[1] = normGaze[2] * normUp[0] - normGaze[0] * normUp[2]; right[2] = normGaze[0] * normUp[1] - normGaze[1] * normUp[0]; rightLength = sqrt(SQR(right[0]) + SQR(right[1]) + SQR(right[2])); for (int i = 0; i < 3; i++) { normRight[i] = right[i] / rightLength; } myRotatef(rotate, degrees, normUp[0], normUp[1], normUp[2]); rotEye[0] = rotate[0] * eyePosition[0] + rotate[4] * eyePosition[1] + rotate[8] * eyePosition[2]; rotEye[1] = rotate[1] * eyePosition[0] + rotate[5] * eyePosition[1] + rotate[9] * eyePosition[2]; rotEye[2] = rotate[2] * eyePosition[0] + rotate[6] * eyePosition[1] + rotate[10] * eyePosition[2]; rotUp[0] = rotate[0] * normUp[0] + rotate[4] * normUp[1] + rotate[8] * normUp[2]; rotUp[1] = rotate[1] * normUp[0] + rotate[5] * normUp[1] + rotate[9] * normUp[2]; rotUp[2] = rotate[2] * normUp[0] + rotate[6] * normUp[1] + rotate[10] * normUp[2]; eyePosition[0] = rotEye[0]; eyePosition[1] = rotEye[1]; eyePosition[2] = rotEye[2]; upVector[0] = rotUp[0]; upVector[1] = rotUp[1]; upVector[2] = rotUp[2]; }
void display( void ) { /* * Set clear color to gray */ myClearColor ( 0.8, 0.8, 0.8, 0.0 ); /* * Clear screen window */ myClear( GL_COLOR_BUFFER_BIT ); myLoadIdentity(); myPushMatrix(); /* * plain old polygon test */ if ( (displayNumber % 4) == 1) { /* default clipping */ myOrtho2D( -100.0, 100.0, -100.0, 100.0 ); /* default viewport */ myViewport( 0, 0, screenWidth, screenHeight ); myPushMatrix(); /* draw the polys */ drawPolys(); } else if ( (displayNumber % 4) == 2) { /* clipping test */ myOrtho2D( -65.0, 75.0, -80.0, 65.0 ); /* default viewport */ myViewport( 0, 0, screenWidth, screenHeight ); /* flip screen around! */ myScalef(-1.0,1.0); myPushMatrix(); /* draw the polys */ drawPolys(); } else if ( (displayNumber % 4) == 3) { /* default clipping */ myOrtho2D( -100.0, 100.0, -100.0, 100.0 ); /* default viewport */ myViewport( 0, 0, screenWidth, screenHeight ); /* rotate screen */ myRotatef(15.0); myPushMatrix(); /* draw the tranformed polys */ drawPolys(); } else /* displayNumber == 0 */ { /* default clipping */ myOrtho2D( -100.0, 100.0, -100.0, 100.0 ); /* default viewport */ myViewport( 0, 0, screenWidth, screenHeight ); myRotatef(-15.0); myScalef(.5,.5); myPushMatrix(); myRotatef(15.0); myPushMatrix(); /* draw the tranformed polys */ drawPolys(); } /* * Flush GL buffers to the display */ glFlush( ); }
void rotateCameraLeft(float degrees, float *eyePosition, float *centerPosition, float *upVector){ GLfloat identity[16]; // will hold the identity matrix GLfloat rMatrix[16]; // will hold the rotation matrix found by myRotatef() GLfloat gaze[] = { centerPosition[0] - eyePosition[0], centerPosition[1] - eyePosition[1], centerPosition[2] - eyePosition[2] }; // z-axis vector GLfloat normGaze[3]; // normalized z-axis vector GLfloat rotEye[3]; // rotated eye position GLfloat normUp[3]; // normalized up vector GLfloat rotUp[3]; // rotated up vector GLfloat right[3]; // right vector GLfloat normRight[3]; // normalized right vector float gazeLength; float upLength; float rightLength; gazeLength = sqrtf(SQR(gaze[0]) + SQR(gaze[1]) + SQR(gaze[2])); // find the length of the gaze vector for (int i = 0; i < 3; i++) // normalize the gaze vector { normGaze[i] = gaze[i] / gazeLength; } upLength = sqrtf(SQR(upVector[0]) + SQR(upVector[1]) + SQR(upVector[2])); // find the length of the up vector for (int i = 0; i < 3; i++) // normlaize the up vector { normUp[i] = upVector[i] / upLength; } // cross product of the normalized gaze and up vectors to get the right vector right[0] = normUp[2] * normGaze[1] - normUp[1] * normGaze[2]; right[1] = normUp[0] * normGaze[2] - normUp[2] * normGaze[0]; right[2] = normUp[1] * normGaze[0] - normUp[0] * normGaze[1]; rightLength = sqrtf(SQR(right[0]) + SQR(right[1]) + SQR(right[2])); // find the length of the right vector for (int i = 0; i < 3; i++) // normalize the right vector (even though it already should be normalized) { normRight[i] = right[i] / rightLength; } // fill out the identity matrix to pull the rotation matrix into this function identity[0] = 1; identity[4] = 0; identity[8] = 0; identity[12] = 0; identity[1] = 0; identity[5] = 1; identity[9] = 0; identity[13] = 0; identity[2] = 0; identity[6] = 0; identity[10] = 1; identity[14] = 0; identity[3] = 0; identity[7] = 0; identity[11] = 0; identity[15] = 1; for (int i = 0; i < 16; i++) // set matrix to the identity matrix { rMatrix[i] = identity[i]; } myRotatef(rMatrix, -degrees, normUp[0], normUp[1], normUp[2]); // get the rotation matrix for a rotation of degrees around the up vector // get the rotated eye position rotEye[0] = rMatrix[0] * eyePosition[0] + rMatrix[4] * eyePosition[1] + rMatrix[8] * eyePosition[2]; rotEye[1] = rMatrix[1] * eyePosition[0] + rMatrix[5] * eyePosition[1] + rMatrix[9] * eyePosition[2]; rotEye[2] = rMatrix[2] * eyePosition[0] + rMatrix[6] * eyePosition[1] + rMatrix[10] * eyePosition[2]; // get the rotated up vector rotUp[0] = rMatrix[0] * normUp[0] + rMatrix[4] * normUp[1] + rMatrix[8] * normUp[2]; rotUp[1] = rMatrix[1] * normUp[0] + rMatrix[5] * normUp[1] + rMatrix[9] * normUp[2]; rotUp[2] = rMatrix[2] * normUp[0] + rMatrix[6] * normUp[1] + rMatrix[10] * normUp[2]; // set the eye position to be equal to the rotated eye position eyePosition[0] = rotEye[0]; eyePosition[1] = rotEye[1]; eyePosition[2] = rotEye[2]; // set the up vector to be equal to the rotated up vector upVector[0] = rotUp[0]; upVector[1] = rotUp[1]; upVector[2] = rotUp[2]; }