void displayCallback( ){ const float centerPosition[] = {0.0, 0.0, 0.0}; float viewingTransform[16]; float modelingTransform[16]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); myLookAt( viewingTransform, eyePosition[0], eyePosition[1], eyePosition[2], centerPosition[0], centerPosition[1], centerPosition[2], upVector[0], upVector[1], upVector[2], camZ ); // Set light & material properties for the teapot; // lights are transformed by current modelview matrix // such that they are positioned correctly in the scene. matMultVec4f( light0, light0_position, viewingTransform ); matMultVec4f( light1, light1_position, viewingTransform ); matMultVec4f( light2, light2_position, viewingTransform ); matMultVec4f( light3, light3_position, viewingTransform ); shaderProgram_A->activate( ); activateUniforms_A( ); glLoadIdentity( ); glMultMatrixf(viewingTransform); myScalef(modelingTransform, teapotScale_A[0], teapotScale_A[1], teapotScale_A[2]); glMultMatrixf(modelingTransform); myTranslatef(modelingTransform, teapotTranslation_A[0], teapotTranslation_A[1], teapotTranslation_A[2]); glMultMatrixf(modelingTransform); _glutSolidTeapot(1.3); shaderProgram_B->activate( ); activateUniforms_B( ); glLoadIdentity( ); glMultMatrixf(viewingTransform); myTranslatef(modelingTransform, teapotTranslation_B[0], teapotTranslation_B[1], teapotTranslation_B[2]); glMultMatrixf(modelingTransform); _glutSolidTeapot(1.3); glutSwapBuffers(); }
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 drawPolys(void) { myMatrixMode(GL_MODELVIEW); myScalef(50.0,50.0); drawHouse(); myPopMatrix(); myTranslatef(-50.0,0.0); myScalef(30.0,30.0); drawHouse(); myPopMatrix(); myTranslatef(0.0,-100.0); myScalef(40.0,40.0); drawHouse(); glFlush(); }
void myLookAt(GLfloat *matrix, GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ){ // Please implement this function. GLdouble gaze[3] = { centerX - eyeX, centerY - eyeY, centerZ - eyeZ }; GLdouble up[3] = { upX, upY, upZ }; GLdouble normGaze[3]; double gazeLength; GLdouble normUp[3]; double upLength; GLdouble right[3]; GLdouble normRight[3]; double rightLength; GLfloat translate[16]; GLfloat modelView[16]; 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(up[0]) + SQR(up[1]) + SQR(up[2])); for (int i = 0; i < 3; i++) { normUp[i] = up[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; } modelView[0] = normRight[0]; modelView[4] = normRight[1]; modelView[8] = normRight[2]; modelView[12] = 0; modelView[1] = normUp[0]; modelView[5] = normUp[1]; modelView[9] = normUp[2]; modelView[13] = 0; modelView[2] = -normGaze[0]; modelView[6] = -normGaze[1]; modelView[10] = -normGaze[2]; modelView[14] = 0; modelView[3] = 0; modelView[7] = 0; modelView[11] = 0; modelView[15] = 1; myTranslatef(translate, -eyeX, -eyeY, -eyeZ); matrixMult(matrix, modelView, translate); }
void myLookAt(GLfloat *matrix, GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat upX, GLfloat upY, GLfloat upZ){ GLfloat normGaze[3]; // will hold the normalized gaze vector GLfloat normUp[3]; // will hold the normalized up vector GLfloat normRight[3]; // will hold the normalized right vector GLfloat right[3]; // will hold the right vector GLfloat modelView[16]; // will hold the pre-translated model view matrix GLfloat translate[16]; // will hold the translate matrix GLfloat gaze[] = { centerX - eyeX, centerY - eyeY, centerZ - eyeZ }; // holds the gaze vector GLfloat up[] = { upX, upY, upZ }; // holds the up vector float gazeLength; float upLength; float rightLength; gazeLength = sqrt(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 = sqrt(SQR(up[0]) + SQR(up[1]) + SQR(up[2])); // find the length of the up vector for (int i = 0; i < 3; i++) // normalize the up vector { normUp[i] = up[i] / upLength; } // cross the normalized gaze vector with the normalized up vector 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 = sqrt(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 because I can { normRight[i] = right[i] / rightLength; } // create the model view matrix matrix[0] = normRight[0]; matrix[4] = normRight[1]; matrix[8] = normRight[2]; matrix[12] = 0; matrix[1] = normUp[0]; matrix[5] = normUp[1]; matrix[9] = normUp[2]; matrix[13] = 0; matrix[2] = -normGaze[0]; matrix[6] = -normGaze[1]; matrix[10] = -normGaze[2]; matrix[14] = 0; matrix[3] = 0; matrix[7] = 0; matrix[11] = 0; matrix[15] = 1; myTranslatef(matrix, -eyeX, -eyeY, -eyeZ); // translate the camera to be at (eyeX, eyeY, eyeZ) }