//Perform per frame updates void UpdateFrame() { window.Update(); camera.Update(); //Change object type if(window.isKeyPressed(' ')) { ++objectType; if(objectType==3) objectType=0; window.SetKeyReleased(' '); } //Change render path if(window.isKeyPressed('1') && renderPath!=CHROMATIC_SINGLE && pathOneSupported) { renderPath=CHROMATIC_SINGLE; InitChromaticSingleStates(); } if(window.isKeyPressed('2') && renderPath!=SIMPLE_SINGLE) { renderPath=SIMPLE_SINGLE; InitSimpleSingleStates(); } if(window.isKeyPressed('3') && renderPath!=CHROMATIC_TWIN) { renderPath=CHROMATIC_TWIN; //Set States every pass, as it is a 2 pass algorithm } }
//Perform per frame updates void UpdateFrame() { window.Update(); camera.Update(); //Update the light's position lightPosition+=lightMovement; //reflect the velocity if necessary if(lightPosition.x>0.9f || lightPosition.x<-0.9f) lightMovement.x=-lightMovement.x; if(lightPosition.y>0.9f || lightPosition.y<-0.9f) lightMovement.y=-lightMovement.y; if(lightPosition.z>0.9f || lightPosition.z<-0.9f) lightMovement.z=-lightMovement.z; if(window.isKeyPressed('1') && codePath!=TEXTURE_ATTEN) { codePath=TEXTURE_ATTEN; SetTextureAttenStates(); } if( window.isKeyPressed('2') && codePath!=PASS_THROUGH_ATTEN && NV_texture_shader_supported) { codePath=PASS_THROUGH_ATTEN; SetPassThroughAttenStates(); } if( window.isKeyPressed('3') && codePath!=TEXTURE_3D_ATTEN && EXT_texture3D_supported) { codePath=TEXTURE_3D_ATTEN; SetTexture3DAttenStates(); } if( window.isKeyPressed('4') && codePath!=GAUSSIAN_ATTEN) { codePath=GAUSSIAN_ATTEN; SetGaussianAttenStates(); } if( window.isKeyPressed(' ')) { currentLightColor++; if(currentLightColor==4) currentLightColor=0; window.SetKeyReleased(' '); } }
//draw a frame void RenderFrame() { //Clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //reset modelview matrix glEnable(GL_LIGHTING); glTranslatef(0.0f, 0.0f, -30.0f); glRotatef((float)timer.GetTime()/30, 1.0f, 0.0f, 1.0f); cubeGrid.DrawSurface(threshold); glDisable(GL_LIGHTING); fpsCounter.Update(); //update frames per second counter glColor4f(0.0f, 0.0f, 1.0f, 1.0f); window.StartTextMode(); window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps()); //print the fps glColor4f(1.0f, 1.0f, 0.0f, 1.0f); window.Print(0, 48, "Grid Size: %d", gridSize); window.Print(0, 68, "%d triangles drawn", cubeGrid.numFacesDrawn); window.EndTextMode(); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); if(window.isKeyPressed(VK_F1)) { window.SaveScreenshot(); window.SetKeyReleased(VK_F1); } window.SwapBuffers(); //swap buffers //check for any opengl errors window.CheckGLError(); //quit if necessary if(window.isKeyPressed(VK_ESCAPE)) PostQuitMessage(0); }
//draw a frame void RenderFrame() { //Draw to pbuffer pbuffer.MakeCurrent(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //reset modelview matrix gluLookAt( 0.0f, 0.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); //Draw scene if(drawTextured) { glBindTexture(GL_TEXTURE_2D, decalTexture); glEnable(GL_TEXTURE_2D); glPushMatrix(); glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f); glutSolidTeapot(0.8f); glPopMatrix(); glDisable(GL_TEXTURE_2D); } else { glPushMatrix(); glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f); glRotatef(55.0f, 1.0f, 0.0f, 0.0f); glutWireTorus(0.3f, 1.0f, 12, 24); glPopMatrix(); glPushMatrix(); glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f); glRotatef(-55.0f, 1.0f, 0.0f, 0.0f); glutWireTorus(0.3f, 1.0f, 12, 24); glPopMatrix(); } //Draw to window window.MakeCurrent(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); camera.SetupViewMatrix(); glLoadMatrixf(camera.viewMatrix); glBindTexture(GL_TEXTURE_2D, pbufferTexture); //use the pbuffer as the texture wglBindTexImageARB(pbuffer.hBuffer, WGL_FRONT_LEFT_ARB); //Draw simple rectangle glBegin(GL_TRIANGLE_STRIP); { glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); } glEnd(); //release the pbuffer for further rendering wglReleaseTexImageARB(pbuffer.hBuffer, WGL_FRONT_LEFT_ARB); fpsCounter.Update(); //update frames per second counter glColor4f(1.0f, 1.0f, 0.0f, 1.0f); window.StartTextMode(); window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps()); //print the fps glColor4f(1.0f, 0.0f, 0.0f, 1.0f); window.Print(0, 48, "%dx Anisotropy", currentAnisotropy); glColor4f(0.0f, 1.0f, 0.0f, 1.0f); window.Print(0, 68, "%s", useMipmapFilter ? "LINEAR_MIPMAP_LINEAR filtering" : "LINEAR filtering"); window.EndTextMode(); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); if(window.isKeyPressed(VK_F1)) { window.SaveScreenshot(); window.SetKeyReleased(VK_F1); } window.SwapBuffers(); //swap buffers //check for any opengl errors window.CheckGLError(); //quit if necessary if(window.isKeyPressed(VK_ESCAPE)) PostQuitMessage(0); }
//Perform per frame updates void UpdateFrame() { window.Update(); camera.Update(); //Change anisotropy level if( window.isKeyPressed(VK_UP) && EXT_texture_filter_anisotropic_supported && currentAnisotropy<maxAnisotropy) { window.MakeCurrent(); currentAnisotropy*=2; glBindTexture(GL_TEXTURE_2D, pbufferTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy); window.SetKeyReleased(VK_UP); } if( window.isKeyPressed(VK_DOWN) && EXT_texture_filter_anisotropic_supported && currentAnisotropy>1) { window.MakeCurrent(); currentAnisotropy/=2; glBindTexture(GL_TEXTURE_2D, pbufferTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy); window.SetKeyReleased(VK_DOWN); } //toggle mipmaps if( window.isKeyPressed('M') && useMipmapFilter==false && SGIS_generate_mipmap_supported) { window.MakeCurrent(); glBindTexture(GL_TEXTURE_2D, pbufferTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true); useMipmapFilter=true; } if( window.isKeyPressed('L') && useMipmapFilter==true && SGIS_generate_mipmap_supported) { window.MakeCurrent(); glBindTexture(GL_TEXTURE_2D, pbufferTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, false); useMipmapFilter=false; } //Pause/unpause if(window.isKeyPressed('P')) timer.Pause(); if(window.isKeyPressed('U')) timer.Unpause(); //Swap between scenes in the pbuffer if(window.isKeyPressed('1') && drawTextured) { //Draw wire tori drawTextured=false; } if(window.isKeyPressed('2') && !drawTextured) { //draw textured sphere drawTextured=true; } }
//draw a frame void RenderFrame() { //Clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //reset modelview matrix camera.SetupViewMatrix(); glMultMatrixf(camera.viewMatrix); DrawBackground(); glPushAttrib(GL_ALL_ATTRIB_BITS); //Enable vertex program glEnable(GL_VERTEX_PROGRAM_NV); //Enable register combiners glEnable(GL_REGISTER_COMBINERS_NV); //Enable textures glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cubeMapTexture); glEnable(GL_TEXTURE_CUBE_MAP_ARB); glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cubeMapTexture); glEnable(GL_TEXTURE_CUBE_MAP_ARB); //Set up CHROMATIC_SINGLE tex units if(renderPath==CHROMATIC_SINGLE) { glActiveTextureARB(GL_TEXTURE2_ARB); glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cubeMapTexture); glEnable(GL_TEXTURE_CUBE_MAP_ARB); glActiveTextureARB(GL_TEXTURE3_ARB); glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cubeMapTexture); glEnable(GL_TEXTURE_CUBE_MAP_ARB); } glActiveTextureARB(GL_TEXTURE0_ARB); //Set the texture Matrix to be the inverse view matrix glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMultMatrixf(camera.viewMatrix.GetInverse()); glMatrixMode(GL_MODELVIEW); //Create object display lists static GLuint objectLists; if(!objectLists) { objectLists=glGenLists(3); glNewList(objectLists, GL_COMPILE); { glutSolidSphere(1.0, 120, 60); } glEndList(); glNewList(objectLists+1, GL_COMPILE); { glutSolidTorus(0.3, 0.7, 120, 60); } glEndList(); glNewList(objectLists+2, GL_COMPILE); { glutSolidTeapot(1.0f); } glEndList(); } //If using CHROMATIC_TWIN, need to do two passes, so set states every frame if(renderPath==CHROMATIC_TWIN) InitChromaticTwin1States(); //draw object glCallList(objectLists+objectType); //Do the second path if necessary if(renderPath==CHROMATIC_TWIN) { InitChromaticTwin2States(); glCallList(objectLists+objectType); } //Reset texture matrix glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glPopAttrib(); fpsCounter.Update(); //update frames per second counter glColor4f(1.0f, 1.0f, 0.0f, 1.0f); window.StartTextMode(); window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps()); //print the fps glColor4f(0.5f, 0.0f, 1.0f, 1.0f); if(renderPath==CHROMATIC_SINGLE) window.Print(0, 48, "Single Pass Chromatic Aberration"); if(renderPath==SIMPLE_SINGLE) window.Print(0, 48, "Single Pass Simple Refraction"); if(renderPath==CHROMATIC_TWIN) window.Print(0, 48, "Two Pass Chromatic Aberration"); window.EndTextMode(); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); if(window.isKeyPressed(VK_F1)) { window.SaveScreenshot(); window.SetKeyReleased(VK_F1); } window.SwapBuffers(); //swap buffers //check for any opengl errors window.CheckGLError(); //quit if necessary if(window.isKeyPressed(VK_ESCAPE)) PostQuitMessage(0); }
//draw a frame void RenderFrame() { //Clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //reset modelview matrix camera.SetupViewMatrix(); glLoadMatrixf(camera.viewMatrix); //Enable box texture glEnable(GL_TEXTURE_2D); //Enable attenuation textures if(codePath==TEXTURE_ATTEN || codePath==GAUSSIAN_ATTEN) { //2D atten glActiveTextureARB(GL_TEXTURE1_ARB); glEnable(GL_TEXTURE_2D); //1D atten glActiveTextureARB(GL_TEXTURE2_ARB); glEnable(GL_TEXTURE_1D); glActiveTextureARB(GL_TEXTURE0_ARB); } else if(codePath==PASS_THROUGH_ATTEN) //set up pass through states { //enable texture shaders glEnable(GL_TEXTURE_SHADER_NV); } else if(codePath==TEXTURE_3D_ATTEN) { //3D atten glActiveTextureARB(GL_TEXTURE1_ARB); glEnable(GL_TEXTURE_3D); glActiveTextureARB(GL_TEXTURE0_ARB); } //Enable vertex program glEnable(GL_VERTEX_PROGRAM_NV); glBindProgramNV(GL_VERTEX_PROGRAM_NV, vertexProgram); //Send the light's color to primary color glColor4fv(lightColors[currentLightColor]); //set the texture matrix glMatrixMode(GL_TEXTURE); glLoadIdentity(); glTranslatef(0.5f, 0.5f, 0.5f); glScalef(0.5f, 0.5f, 0.5f); glScalef(1/lightRadius, 1/lightRadius, 1/lightRadius); glTranslatef(-lightPosition.x, -lightPosition.y, -lightPosition.z); glMatrixMode(GL_MODELVIEW); //Enable register combiners glEnable(GL_REGISTER_COMBINERS_NV); //Draw Cube glDrawArrays(GL_QUADS, 0, 24); //Disable box texture glDisable(GL_TEXTURE_2D); //Disable attenuation textures if(codePath==TEXTURE_ATTEN || codePath==GAUSSIAN_ATTEN) { //2D atten glActiveTextureARB(GL_TEXTURE1_ARB); glDisable(GL_TEXTURE_2D); //1D atten glActiveTextureARB(GL_TEXTURE2_ARB); glDisable(GL_TEXTURE_1D); glActiveTextureARB(GL_TEXTURE0_ARB); } else if(codePath==PASS_THROUGH_ATTEN) //set up pass through states { //disable texture shaders glDisable(GL_TEXTURE_SHADER_NV); } else if(codePath==TEXTURE_3D_ATTEN) { //3D atten glActiveTextureARB(GL_TEXTURE1_ARB); glDisable(GL_TEXTURE_3D); glActiveTextureARB(GL_TEXTURE0_ARB); } //Disable vertex program glDisable(GL_VERTEX_PROGRAM_NV); //reset the texture matrix glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); //Disable register combiners glDisable(GL_REGISTER_COMBINERS_NV); //Draw light in a display list glColor4fv(lightColors[currentLightColor]); glTranslatef(lightPosition.x, lightPosition.y, lightPosition.z); static GLuint sphereList=0; if(!sphereList) { sphereList=glGenLists(1); glNewList(sphereList, GL_COMPILE); { gluSphere(sphere, 0.1f, 24, 12); } glEndList(); } glCallList(sphereList); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); fpsCounter.Update(); //update frames per second counter glColor4f(0.0f, 0.0f, 1.0f, 1.0f); window.StartTextMode(); window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps()); //print the fps glColor4f(1.0f, 1.0f, 0.0f, 0.0f); if(codePath==TEXTURE_ATTEN) window.Print(0, 48, "Using Texture Attenuation"); if(codePath==PASS_THROUGH_ATTEN) window.Print(0, 48, "Using Pass Through Attenuation"); if(codePath==TEXTURE_3D_ATTEN) window.Print(0, 48, "Using 3D Texture Attenuation"); if(codePath==GAUSSIAN_ATTEN) window.Print(0, 48, "Using Gaussian Texture Attenuation"); window.EndTextMode(); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); if(window.isKeyPressed(VK_F1)) { window.SaveScreenshot(); window.SetKeyReleased(VK_F1); } window.SwapBuffers(); //swap buffers //check for any opengl errors window.CheckGLError(); //quit if necessary if(window.isKeyPressed(VK_ESCAPE)) PostQuitMessage(0); }
//Perform per frame updates void UpdateFrame() { window.Update(); //update balls' position float c = 2.0f*(float)cos(timer.GetTime()/600); metaballs[0].position.x=-4.0f*(float)cos(timer.GetTime()/700) - c; metaballs[0].position.y=4.0f*(float)sin(timer.GetTime()/600) - c; metaballs[1].position.x=5.0f*(float)sin(timer.GetTime()/400) + c; metaballs[1].position.y=5.0f*(float)cos(timer.GetTime()/400) - c; metaballs[2].position.x=-5.0f*(float)cos(timer.GetTime()/400) - 0.2f*(float)sin(timer.GetTime()/600); metaballs[2].position.y=5.0f*(float)sin(timer.GetTime()/500) - 0.2f*(float)sin(timer.GetTime()/400); //increase or decrease density if(window.isKeyPressed(VK_UP) && gridSize<maxGridSize) { gridSize++; cubeGrid.Init(gridSize); } if(window.isKeyPressed(VK_DOWN) && gridSize>minGridSize) { gridSize--; cubeGrid.Init(gridSize); } //pause & unpause if(window.isKeyPressed('P')) { timer.Pause(); } if(window.isKeyPressed('U')) { timer.Unpause(); } //clear the field for(int i=0; i<cubeGrid.numVertices; i++) { cubeGrid.vertices[i].value=0.0f; cubeGrid.vertices[i].normal.LoadZero(); } //evaluate the scalar field at each point VECTOR3D ballToPoint; float squaredRadius; VECTOR3D ballPosition; float normalScale; for(int i=0; i<numMetaballs; i++) { squaredRadius=metaballs[i].squaredRadius; ballPosition=metaballs[i].position; //VC++6 standard does not inline functions //by inlining these maually, in this performance-critical area, //almost a 100% increase in speed is found for(int j=0; j<cubeGrid.numVertices; j++) { //ballToPoint=cubeGrid.vertices[j].position-ballPosition; ballToPoint.x=cubeGrid.vertices[j].position.x-ballPosition.x; ballToPoint.y=cubeGrid.vertices[j].position.y-ballPosition.y; ballToPoint.z=cubeGrid.vertices[j].position.z-ballPosition.z; //get squared distance from ball to point //float squaredDistance=ballToPoint.GetSquaredLength(); float squaredDistance= ballToPoint.x*ballToPoint.x + ballToPoint.y*ballToPoint.y + ballToPoint.z*ballToPoint.z; if(squaredDistance==0.0f) squaredDistance=0.0001f; //value = r^2/d^2 cubeGrid.vertices[j].value+=squaredRadius/squaredDistance; //normal = (r^2 * v)/d^4 normalScale=squaredRadius/(squaredDistance*squaredDistance); //cubeGrid.vertices[j].normal+=ballToPoint*normalScale; cubeGrid.vertices[j].normal.x+=ballToPoint.x*normalScale; cubeGrid.vertices[j].normal.y+=ballToPoint.y*normalScale; cubeGrid.vertices[j].normal.z+=ballToPoint.z*normalScale; } } //toggle wireframe if(window.isKeyPressed('W')) { glPolygonMode(GL_FRONT, GL_LINE); } if(window.isKeyPressed('F')) { glPolygonMode(GL_FRONT, GL_FILL); } //Change color if(window.isKeyPressed(VK_SPACE)) { currentDiffuseColor++; if(currentDiffuseColor==numDiffuseColors) currentDiffuseColor=0; glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseColors[currentDiffuseColor]); window.SetKeyReleased(VK_SPACE); } }