//Called whenever a key on the keyboard was pressed. //The key is given by the ''key'' parameter, which is in ASCII. //It's often a good idea to have the escape key (ASCII value 27) call glutLeaveMainLoop() to //exit the program. void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: delete g_pScene; g_pScene = NULL; glutLeaveMainLoop(); return; case 32: g_lightPole.Reset(); break; case 't': g_bDrawCameraPos = !g_bDrawCameraPos; break; case 'g': g_bShowOtherLights = !g_bShowOtherLights; break; case 'p': g_timer.TogglePause(); break; case '\r': //Enter key. { try { LoadAndSetupScene(); } catch(std::exception &except) { printf("Failed to reload, due to: %s\n", except.what()); return; } } break; } { int possibleIndex = (int)key - (int)'1'; if((0 <= possibleIndex) && (possibleIndex < NUM_LIGHT_TEXTURES)) { g_currTextureIndex = key - '1'; printf("%s\n", g_texDefs[g_currTextureIndex].name); } } g_viewPole.CharPress(key); g_lightPole.CharPress(key); }
void display() { g_LightTimer.Update(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(g_pPlaneMesh && g_pCylinderMesh && g_pCubeMesh) { glutil::MatrixStack modelMatrix; modelMatrix.SetMatrix(g_viewPole.CalcMatrix()); const glm::vec4 &worldLightPos = CalcLightPosition(); const glm::vec4 &lightPosCameraSpace = modelMatrix.Top() * worldLightPos; ProgramData &whiteProg = g_Programs[g_eLightModel].whiteProg; ProgramData &colorProg = g_Programs[g_eLightModel].colorProg; glUseProgram(whiteProg.theProgram); glUniform4f(whiteProg.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(whiteProg.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform3fv(whiteProg.cameraSpaceLightPosUnif,1, glm::value_ptr(lightPosCameraSpace)); glUniform1f(whiteProg.lightAttenuationUnif, g_fLightAttenuation); glUniform1f(whiteProg.shininessFactorUnif, g_matParams); glUniform4fv(whiteProg.baseDiffuseColorUnif, 1, g_bDrawDark ? glm::value_ptr(g_darkColor) : glm::value_ptr(g_lightColor)); glUseProgram(colorProg.theProgram); glUniform4f(colorProg.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(colorProg.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform3fv(colorProg.cameraSpaceLightPosUnif, 1, glm::value_ptr(lightPosCameraSpace)); glUniform1f(colorProg.lightAttenuationUnif, g_fLightAttenuation); glUniform1f(colorProg.shininessFactorUnif, g_matParams); glUseProgram(0); { glutil::PushStack push(modelMatrix); //Render the ground plane. { glutil::PushStack push(modelMatrix); glm::mat3 normMatrix(modelMatrix.Top()); normMatrix = glm::transpose(glm::inverse(normMatrix)); glUseProgram(whiteProg.theProgram); glUniformMatrix4fv(whiteProg.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniformMatrix3fv(whiteProg.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pPlaneMesh->Render(); glUseProgram(0); } //Render the Cylinder { glutil::PushStack push(modelMatrix); modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix()); if(g_bScaleCyl) modelMatrix.Scale(1.0f, 1.0f, 0.2f); glm::mat3 normMatrix(modelMatrix.Top()); normMatrix = glm::transpose(glm::inverse(normMatrix)); ProgramData &prog = g_bDrawColoredCyl ? colorProg : whiteProg; glUseProgram(prog.theProgram); glUniformMatrix4fv(prog.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniformMatrix3fv(prog.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); if(g_bDrawColoredCyl) g_pCylinderMesh->Render("lit-color"); else g_pCylinderMesh->Render("lit"); glUseProgram(0); } //Render the light if(g_bDrawLightSource) { glutil::PushStack push(modelMatrix); modelMatrix.Translate(glm::vec3(worldLightPos)); modelMatrix.Scale(0.1f, 0.1f, 0.1f); glUseProgram(g_Unlit.theProgram); glUniformMatrix4fv(g_Unlit.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniform4f(g_Unlit.objectColorUnif, 0.8078f, 0.8706f, 0.9922f, 1.0f); g_pCubeMesh->Render("flat"); } } } glutPostRedisplay(); glutSwapBuffers(); }
void display() { g_LightTimer.Update(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(g_pPlaneMesh && g_pCylinderMesh && g_pCubeMesh) { glutil::MatrixStack modelMatrix; modelMatrix.SetMatrix(g_viewPole.CalcMatrix()); const glm::vec4 &worldLightPos = CalcLightPosition(); glm::vec4 lightPosCameraSpace = modelMatrix.Top() * worldLightPos; glUseProgram(g_WhiteDiffuseColor.theProgram); glUniform3fv(g_WhiteDiffuseColor.lightPosUnif, 1, glm::value_ptr(lightPosCameraSpace)); glUseProgram(g_VertexDiffuseColor.theProgram); glUniform3fv(g_VertexDiffuseColor.lightPosUnif, 1, glm::value_ptr(lightPosCameraSpace)); glUseProgram(g_WhiteDiffuseColor.theProgram); glUniform4f(g_WhiteDiffuseColor.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(g_WhiteDiffuseColor.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUseProgram(g_VertexDiffuseColor.theProgram); glUniform4f(g_VertexDiffuseColor.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(g_VertexDiffuseColor.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUseProgram(0); { glutil::PushStack push(modelMatrix); //Render the ground plane. { glutil::PushStack push(modelMatrix); glUseProgram(g_WhiteDiffuseColor.theProgram); glUniformMatrix4fv(g_WhiteDiffuseColor.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(g_WhiteDiffuseColor.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pPlaneMesh->Render(); glUseProgram(0); } //Render the Cylinder { glutil::PushStack push(modelMatrix); modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix()); if(g_bDrawColoredCyl) { glUseProgram(g_VertexDiffuseColor.theProgram); glUniformMatrix4fv(g_VertexDiffuseColor.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(g_VertexDiffuseColor.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pCylinderMesh->Render("lit-color"); } else { glUseProgram(g_WhiteDiffuseColor.theProgram); glUniformMatrix4fv(g_WhiteDiffuseColor.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(g_WhiteDiffuseColor.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pCylinderMesh->Render("lit"); } glUseProgram(0); } //Render the light if(g_bDrawLight) { glutil::PushStack push(modelMatrix); modelMatrix.Translate(glm::vec3(worldLightPos)); modelMatrix.Scale(0.1f, 0.1f, 0.1f); glUseProgram(g_Unlit.theProgram); glUniformMatrix4fv(g_Unlit.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniform4f(g_Unlit.objectColorUnif, 0.8078f, 0.8706f, 0.9922f, 1.0f); g_pCubeMesh->Render("flat"); } } } glutPostRedisplay(); glutSwapBuffers(); }
//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() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(g_pPlaneMesh && g_pCylinderMesh) { glutil::MatrixStack modelMatrix; modelMatrix.SetMatrix(g_viewPole.CalcMatrix()); glm::vec4 lightDirCameraSpace = modelMatrix.Top() * g_lightDirection; glUseProgram(g_WhiteDiffuseColor.theProgram); glUniform3fv(g_WhiteDiffuseColor.dirToLightUnif, 1, glm::value_ptr(lightDirCameraSpace)); glUseProgram(g_VertexDiffuseColor.theProgram); glUniform3fv(g_VertexDiffuseColor.dirToLightUnif, 1, glm::value_ptr(lightDirCameraSpace)); glUseProgram(0); { glutil::PushStack push(modelMatrix); //Render the ground plane. { glutil::PushStack push(modelMatrix); glUseProgram(g_WhiteDiffuseColor.theProgram); glUniformMatrix4fv(g_WhiteDiffuseColor.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(g_WhiteDiffuseColor.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); glUniform4f(g_WhiteDiffuseColor.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f); g_pPlaneMesh->Render(); glUseProgram(0); } //Render the Cylinder { glutil::PushStack push(modelMatrix); modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix()); if(g_bScaleCyl) modelMatrix.Scale(1.0f, 1.0f, 0.2f); glUseProgram(g_VertexDiffuseColor.theProgram); glUniformMatrix4fv(g_VertexDiffuseColor.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); if(g_bDoInvTranspose) { normMatrix = glm::transpose(glm::inverse(normMatrix)); } glUniformMatrix3fv(g_VertexDiffuseColor.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); glUniform4f(g_VertexDiffuseColor.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f); g_pCylinderMesh->Render("lit-color"); glUseProgram(0); } } } glutSwapBuffers(); }
GLUSboolean display(GLUSfloat time) { g_LightTimer.Update(time); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutil::MatrixStack modelMatrix; modelMatrix.SetMatrix(g_viewPole.CalcMatrix()); const glm::vec4 &worldLightPos = CalcLightPosition(); const glm::vec4 &lightPosCameraSpace = modelMatrix.Top() * worldLightPos; glUseProgram(g_MonoDiffuseProgram.program); glUniform4f(g_MonoDiffuseProgram.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(g_MonoDiffuseProgram.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform3fv(g_MonoDiffuseProgram.cameraSpaceLightPosUnif,1, glm::value_ptr(lightPosCameraSpace)); glUniform1f(g_MonoDiffuseProgram.lightAttenuationUnif, g_fLightAttenuation); glUniform1f(g_MonoDiffuseProgram.shininessFactorUnif, g_fGaussianRoughness); glUniform4fv(g_MonoDiffuseProgram.baseDiffuseColorUnif, 1, g_bDrawDark ? glm::value_ptr(g_darkColor) : glm::value_ptr(g_lightColor)); glUseProgram(g_ColorDiffuseProgram.program); glUniform4f(g_ColorDiffuseProgram.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(g_ColorDiffuseProgram.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUniform3fv(g_ColorDiffuseProgram.cameraSpaceLightPosUnif, 1, glm::value_ptr(lightPosCameraSpace)); glUniform1f(g_ColorDiffuseProgram.lightAttenuationUnif, g_fLightAttenuation); glUniform1f(g_ColorDiffuseProgram.shininessFactorUnif, g_fGaussianRoughness); glUseProgram(0); { glutil::PushStack push(modelMatrix); { glutil::PushStack push(modelMatrix); glm::mat3 normMatrix(modelMatrix.Top()); normMatrix = glm::transpose(glm::inverse(normMatrix)); glUseProgram(g_MonoDiffuseProgram.program); glUniformMatrix4fv(g_MonoDiffuseProgram.modelViewUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniformMatrix3fv(g_MonoDiffuseProgram.modelViewForNormalUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pPlaneMesh->render(); glUseProgram(0); } { glutil::PushStack push(modelMatrix); modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix()); if (g_bScaleCyl) { modelMatrix.Scale(1.0f, 1.0f, 0.2f); } glm::mat3 normMatrix(modelMatrix.Top()); normMatrix = glm::transpose(glm::inverse(normMatrix)); ProgramData &prog = g_bDrawColoredCyl ? g_ColorDiffuseProgram : g_MonoDiffuseProgram; glUseProgram(prog.program); glUniformMatrix4fv(prog.modelViewUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniformMatrix3fv(prog.modelViewForNormalUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pCylinderMesh->render(g_bDrawColoredCyl ? "lit-color" : "lit"); glUseProgram(0); } if (g_bDrawLightSource) { glutil::PushStack push(modelMatrix); modelMatrix.Translate(glm::vec3(worldLightPos)); modelMatrix.Scale(0.1f, 0.1f, 0.1f); glUseProgram(g_UnlitProgram.program); glUniformMatrix4fv(g_UnlitProgram.modelViewUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniform4f(g_UnlitProgram.objectColorUnif, 0.8078f, 0.8706f, 0.9922f, 1.0f); g_pCubeMesh->render("flat"); } } return GLUS_TRUE; }
void display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(g_pPlaneMesh && g_pCylinderMesh) { glutil::MatrixStack modelMatrix; modelMatrix.SetMatrix(g_viewPole.CalcMatrix()); glm::vec4 lightDirCameraSpace = modelMatrix.Top() * g_lightDirection; ProgramData &whiteDiffuse = g_bShowAmbient ? g_WhiteAmbDiffuseColor : g_WhiteDiffuseColor; ProgramData &vertexDiffuse = g_bShowAmbient ? g_VertexAmbDiffuseColor : g_VertexDiffuseColor; if(g_bShowAmbient) { glUseProgram(whiteDiffuse.theProgram); glUniform4f(whiteDiffuse.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(whiteDiffuse.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); glUseProgram(vertexDiffuse.theProgram); glUniform4f(vertexDiffuse.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f); glUniform4f(vertexDiffuse.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f); } else { glUseProgram(whiteDiffuse.theProgram); glUniform4f(whiteDiffuse.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f); glUseProgram(vertexDiffuse.theProgram); glUniform4f(vertexDiffuse.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f); } glUseProgram(whiteDiffuse.theProgram); glUniform3fv(whiteDiffuse.dirToLightUnif, 1, glm::value_ptr(lightDirCameraSpace)); glUseProgram(vertexDiffuse.theProgram); glUniform3fv(vertexDiffuse.dirToLightUnif, 1, glm::value_ptr(lightDirCameraSpace)); glUseProgram(0); { glutil::PushStack push(modelMatrix); //Render the ground plane. { glutil::PushStack push(modelMatrix); glUseProgram(whiteDiffuse.theProgram); glUniformMatrix4fv(whiteDiffuse.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(whiteDiffuse.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pPlaneMesh->Render(); glUseProgram(0); } //Render the Cylinder { glutil::PushStack push(modelMatrix); modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix()); if(g_bDrawColoredCyl) { glUseProgram(vertexDiffuse.theProgram); glUniformMatrix4fv(vertexDiffuse.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(vertexDiffuse.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pCylinderMesh->Render("lit-color"); } else { glUseProgram(whiteDiffuse.theProgram); glUniformMatrix4fv(whiteDiffuse.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glm::mat3 normMatrix(modelMatrix.Top()); glUniformMatrix3fv(whiteDiffuse.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix)); g_pCylinderMesh->Render("lit"); } glUseProgram(0); } } } glutSwapBuffers(); }
//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() { if(!g_pScene) return; g_timer.Update(); glClearColor(0.8f, 0.8f, 0.8f, 1.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const glm::mat4 &cameraMatrix = g_viewPole.CalcMatrix(); const glm::mat4 &lightView = g_lightPole.CalcMatrix(); glutil::MatrixStack modelMatrix; modelMatrix *= cameraMatrix; BuildLights(cameraMatrix); g_nodes[0].NodeSetOrient(glm::rotate(glm::fquat(), 360.0f * g_timer.GetAlpha(), glm::vec3(0.0f, 1.0f, 0.0f))); g_nodes[3].NodeSetOrient(g_spinBarOrient * glm::rotate(glm::fquat(), 360.0f * g_timer.GetAlpha(), glm::vec3(0.0f, 0.0f, 1.0f))); { glutil::MatrixStack persMatrix; persMatrix.Perspective(60.0f, (g_displayWidth / (float)g_displayHeight), g_fzNear, g_fzFar); ProjectionBlock projData; projData.cameraToClipMatrix = persMatrix.Top(); glBindBuffer(GL_UNIFORM_BUFFER, g_projectionUniformBuffer); glBufferData(GL_UNIFORM_BUFFER, sizeof(ProjectionBlock), &projData, GL_STREAM_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); } glActiveTexture(GL_TEXTURE0 + g_lightProjTexUnit); glBindTexture(GL_TEXTURE_CUBE_MAP, g_lightTextures[g_currTextureIndex]); glBindSampler(g_lightProjTexUnit, g_samplers[g_currSampler]); { glutil::MatrixStack lightProjStack; lightProjStack.ApplyMatrix(glm::inverse(lightView)); lightProjStack.ApplyMatrix(glm::inverse(cameraMatrix)); g_lightProjMatBinder.SetValue(lightProjStack.Top()); glm::vec4 worldLightPos = lightView[3]; glm::vec3 lightPos = glm::vec3(cameraMatrix * worldLightPos); g_camLightPosBinder.SetValue(lightPos); } glViewport(0, 0, (GLsizei)g_displayWidth, (GLsizei)g_displayHeight); g_pScene->Render(modelMatrix.Top()); { //Draw axes glutil::PushStack stackPush(modelMatrix); modelMatrix.ApplyMatrix(lightView); modelMatrix.Scale(15.0f); glUseProgram(g_colroedProg); glUniformMatrix4fv(g_coloredModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); g_pAxesMesh->Render(); } if(g_bDrawCameraPos) { //Draw lookat point. glutil::PushStack stackPush(modelMatrix); modelMatrix.SetIdentity(); modelMatrix.Translate(glm::vec3(0.0f, 0.0f, -g_viewPole.GetView().radius)); modelMatrix.Scale(0.5f); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); glUseProgram(g_unlitProg); glUniformMatrix4fv(g_unlitModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top())); glUniform4f(g_unlitObjectColorUnif, 0.25f, 0.25f, 0.25f, 1.0f); g_pSphereMesh->Render("flat"); glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); glUniform4f(g_unlitObjectColorUnif, 1.0f, 1.0f, 1.0f, 1.0f); g_pSphereMesh->Render("flat"); } glActiveTexture(GL_TEXTURE0 + g_lightProjTexUnit); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glBindSampler(g_lightProjTexUnit, 0); glutPostRedisplay(); glutSwapBuffers(); }