// Create a color gradient ranging from ZMin to Zmax using the color // inflections defined in grid int nwt_LoadColors( NWT_RGB * pMap, int mapSize, NWT_GRID * pGrd ) { int i; NWT_RGB sColor; int nWarkerMark = 0; createIP( 0, 255, 255, 255, pMap, &nWarkerMark ); // If Zmin is less than the 1st inflection use the 1st inflections color to // the start of the ramp if( pGrd->fZMin <= pGrd->stInflection[0].zVal ) { createIP( 1, pGrd->stInflection[0].r, pGrd->stInflection[0].g, pGrd->stInflection[0].b, pMap, &nWarkerMark ); } // find what inflections zmin is between for( i = 0; i < pGrd->iNumColorInflections; i++ ) { if( pGrd->fZMin < pGrd->stInflection[i].zVal ) { // then we must be between i and i-1 linearColor( &sColor, &pGrd->stInflection[i - 1], &pGrd->stInflection[i], pGrd->fZMin ); createIP( 1, sColor.r, sColor.g, sColor.b, pMap, &nWarkerMark ); break; } } // the interesting case of zmin beig higher than the max inflection value if( i >= pGrd->iNumColorInflections ) { createIP( 1, pGrd->stInflection[pGrd->iNumColorInflections - 1].r, pGrd->stInflection[pGrd->iNumColorInflections - 1].g, pGrd->stInflection[pGrd->iNumColorInflections - 1].b, pMap, &nWarkerMark ); createIP( mapSize - 1, pGrd->stInflection[pGrd->iNumColorInflections - 1].r, pGrd->stInflection[pGrd->iNumColorInflections - 1].g, pGrd->stInflection[pGrd->iNumColorInflections - 1].b, pMap, &nWarkerMark ); } else { int index = 0; for( ; i < pGrd->iNumColorInflections; i++ ) { if( pGrd->fZMax < pGrd->stInflection[i].zVal ) { // then we must be between i and i-1 linearColor( &sColor, &pGrd->stInflection[i - 1], &pGrd->stInflection[i], pGrd->fZMax ); index = mapSize - 1; createIP( index, sColor.r, sColor.g, sColor.b, pMap, &nWarkerMark ); break; } // save the inflections between zmin and zmax index = (int)( ( (pGrd->stInflection[i].zVal - pGrd->fZMin) / (pGrd->fZMax - pGrd->fZMin) ) * mapSize); if ( index >= mapSize ) index = mapSize - 1; createIP( index, pGrd->stInflection[i].r, pGrd->stInflection[i].g, pGrd->stInflection[i].b, pMap, &nWarkerMark ); } if( index < mapSize - 1 ) createIP( mapSize - 1, pGrd->stInflection[pGrd->iNumColorInflections - 1].r, pGrd->stInflection[pGrd->iNumColorInflections - 1].g, pGrd->stInflection[pGrd->iNumColorInflections - 1].b, pMap, &nWarkerMark ); } return 0; }
//Called to update the display. //You should call glutSwapBuffers after all of your rendering to display what you rendered. //If you need continuous updates of the screen, call glutPostRedisplay() at the end of the function. void display() { const float epsilon = 0.001f; // necessary for the trackball viewer const float period = 10.0; // repeat time in seconds of movement float fElapsedTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f; // time since programs started in seconds float timeParameter = (float) fmodf(fElapsedTime, period)/period; // parameterized time //float timeParameter = 0.0f; // no animation - also comment out the glutPostRedisplay() at the bottom of this function glClearColor(0.0f, 0.0f, 0.2f, 0.0f); // unshaded objects will show up on this almost-black background glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // light stuff glUniform3f(PhongShade.cameraSpaceLightPosUnif, 0.0, 0.0, 100.0); // this is a headlight glUniform4f(PhongShade.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f); // light color // compute trackball interface (world to camera) transform -------------------------------- // don't change any of this (unless you can make it better!) glm::mat4 camMatrix; glm::mat4 cam2Matrix; camMatrix = glm::translate(camMatrix, glm::vec3(modelTrans[0], modelTrans[1], modelTrans[2])); // in the middle of a left-button drag if (trackballMove) { // check to make sure axis is not zero vector if (!(-epsilon < axis[0] && axis[0] < epsilon && -epsilon < axis[1] && axis[1] < epsilon && -epsilon < axis[2] && axis[2] < epsilon)) { cam2Matrix = glm::rotate(cam2Matrix, angle, glm::vec3(axis[0], axis[1], axis[2])); cam2Matrix = cam2Matrix * (glm::mat4(trackballXform[0], trackballXform[1], trackballXform[2], trackballXform[3], trackballXform[4], trackballXform[5], trackballXform[6], trackballXform[7], trackballXform[8], trackballXform[9], trackballXform[10], trackballXform[11], trackballXform[12], trackballXform[13], trackballXform[14], trackballXform[15])); glm::mat4 tempM = cam2Matrix; // copy current transform back into trackball matrix for (int i=0; i<4; i++) { for (int j=0; j<4; j++) trackballXform[i*4 + j] = tempM[i][j]; } } } camMatrix = camMatrix * (glm::mat4(objectXformPtr[0], objectXformPtr[1], objectXformPtr[2], objectXformPtr[3], objectXformPtr[4], objectXformPtr[5], objectXformPtr[6], objectXformPtr[7], objectXformPtr[8], objectXformPtr[9], objectXformPtr[10], objectXformPtr[11], objectXformPtr[12], objectXformPtr[13], objectXformPtr[14], objectXformPtr[15])); // end of world to camera transform ----------------------------------------------- // pass it on to the shaders glUniformMatrix4fv(PhongShade.worldToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(camMatrix)); // draw objects - pass in the transform matrix to use glm::mat4 modelMatrix; //starts with identity drawFirstSphere(modelMatrix); // modelMatrix is not modified by the draw functions drawTopSphere(modelMatrix, sqrColor(timeParameter), timeParameter); drawMiddleSphere(modelMatrix, linearColor(timeParameter), timeParameter); drawMiddleSphere2(modelMatrix, linearColor(timeParameter), timeParameter); eyeRight(modelMatrix, eyeColor(timeParameter), timeParameter); eyeLeft(modelMatrix, eyeColor(timeParameter), timeParameter); drawMouth(modelMatrix, linearColor(timeParameter), timeParameter); drawBottomSphere(modelMatrix, sineColor(timeParameter), timeParameter); drawThirdSphere(modelMatrix); glutSwapBuffers(); glutPostRedisplay(); }