glm::vec4 CalcLightPosition(const Framework::Timer &timer, float alphaOffset) { const float fLoopDuration = 5.0f; const float fScale = 3.14159f * 2.0f; float timeThroughLoop = timer.GetAlpha() + alphaOffset; glm::vec4 ret(0.0f, g_fLightHeight, 0.0f, 1.0f); ret.x = cosf(timeThroughLoop * fScale) * g_fLightRadius; ret.z = sinf(timeThroughLoop * fScale) * g_fLightRadius; return ret; }
//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); }
//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_persViewPole.Reset(); break; case 't': g_bDrawCameraPos = !g_bDrawCameraPos; break; case 'y': g_bDepthClampProj = !g_bDepthClampProj; 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; } g_viewPole.CharPress(key); }
void operator()(Framework::Timer &timer) {timer.Fastforward(secFF);}
void operator()(Framework::Timer &timer) {timer.Rewind(secRewind);}
void operator()(Framework::Timer &timer) {timer.SetPause(pause);}
void operator()(Framework::Timer &timer) {timer.Update();}
//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); glutil::MatrixStack modelMatrix; modelMatrix *= g_viewPole.CalcMatrix(); BuildLights(modelMatrix.Top()); 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))); glm::ivec2 displaySize(g_displayWidth / 2, g_displayHeight); { glutil::MatrixStack persMatrix; persMatrix.Perspective(60.0f, (displaySize.x / (float)displaySize.y), 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); } glViewport(0, 0, (GLsizei)displaySize.x, (GLsizei)displaySize.y); g_pScene->Render(modelMatrix.Top()); if(g_bDrawCameraPos) { glutil::PushStack stackPush(modelMatrix); //Draw lookat point. 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"); } { glutil::MatrixStack persMatrix; persMatrix.ApplyMatrix(glm::mat4(glm::mat3(g_persViewPole.CalcMatrix()))); persMatrix.Perspective(60.0f, (displaySize.x / (float)displaySize.y), 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); } if(!g_bDepthClampProj) glDisable(GL_DEPTH_CLAMP); glViewport(displaySize.x + (g_displayWidth % 2), 0, (GLsizei)displaySize.x, (GLsizei)displaySize.y); g_pScene->Render(modelMatrix.Top()); glEnable(GL_DEPTH_CLAMP); 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() { 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(); }