void User::setUserCamera(const CameraSP& userCamera) { this->userCamera = userCamera; if (userCamera.get()) { this->position = userCamera->getEye(); this->direction = userCamera->getDirection(); this->up = userCamera->getUp(); this->left = up.cross(direction); this->rotation = Quaternion(Matrix3x3(-left, up, -direction)); } dirtyFlag = true; }
void createMazeScene(ViewerSP viewer, CameraSP camera, GroupSP& scene, LightSP& light) { ShaderCoreFactory shaderFactory("resources/shaders"); #ifdef SCG_CPP11_INITIALIZER_LISTS auto shaderPhong = shaderFactory.createShaderFromSourceFiles({ ShaderFile("phong_vert.glsl", GL_VERTEX_SHADER), ShaderFile("phong_frag.glsl", GL_FRAGMENT_SHADER), ShaderFile("blinn_phong_lighting.glsl", GL_FRAGMENT_SHADER), ShaderFile("texture_none.glsl", GL_FRAGMENT_SHADER) }); auto shaderPhongTex = shaderFactory.createShaderFromSourceFiles({ ShaderFile("phong_vert.glsl", GL_VERTEX_SHADER), ShaderFile("phong_frag.glsl", GL_FRAGMENT_SHADER), ShaderFile("blinn_phong_lighting.glsl", GL_FRAGMENT_SHADER), ShaderFile("texture2d_modulate.glsl", GL_FRAGMENT_SHADER) }); // Gouraud shader auto shaderGouraud = shaderFactory.createShaderFromSourceFiles({ ShaderFile("simple_gouraud_vert.glsl", GL_VERTEX_SHADER), ShaderFile("simple_gouraud_frag.glsl", GL_FRAGMENT_SHADER) }); // Phong shader with texture mapping auto shaderBumpTex = shaderFactory.createShaderFromSourceFiles({ ShaderFile("bump_vert.glsl", GL_VERTEX_SHADER), ShaderFile("texture2d_modulate.glsl", GL_FRAGMENT_SHADER), ShaderFile("bump_frag.glsl", GL_FRAGMENT_SHADER) }); // Skybox shader auto shaderSkybox = shaderFactory.createShaderFromSourceFiles( { ShaderFile("skybox_vert.glsl", GL_VERTEX_SHADER), ShaderFile("skybox_frag.glsl", GL_FRAGMENT_SHADER) }); #else std::vector<ShaderFile> shaderFiles; shaderFiles.push_back(ShaderFile("simple_gouraud_vert.glsl", GL_VERTEX_SHADER)); shaderFiles.push_back(ShaderFile("simple_gouraud_frag.glsl", GL_FRAGMENT_SHADER)); auto shaderGouraud = shaderFactory.createShaderFromSourceFiles(shaderFiles); // Phong shader with texture mapping shaderFiles.clear(); shaderFiles.push_back(ShaderFile("bump_vert.glsl", GL_VERTEX_SHADER)); shaderFiles.push_back(ShaderFile("texture2d_modulate.glsl", GL_FRAGMENT_SHADER)); shaderFiles.push_back(ShaderFile("bump_frag.glsl", GL_FRAGMENT_SHADER)); auto shaderBumpTex = shaderFactory.createShaderFromSourceFiles(shaderFiles); shaderFiles.clear(); shaderFiles.push_back(ShaderFile("skybox_vert.glsl", GL_VERTEX_SHADER)); shaderFiles.push_back(ShaderFile("skybox_frag.glsl", GL_FRAGMENT_SHADER)); auto shaderSkybox = shaderFactory.createShaderFromSourceFiles(shaderFiles); #endif // camera controllers camera->translate(glm::vec3(0.f, 2.f, 1.f))->dolly(0.f); #ifdef SCG_CPP11_INITIALIZER_LISTS viewer->addControllers({ FirstPersonController::create(camera), MouseController::create(camera) }); #else viewer->addController(FirstPersonController::create(camera)) ->addController(MouseController::create(camera)); #endif maze::Maze maze1 = maze::generateMaze(); GeometryCoreFactory geometryFactory; TextureCoreFactory textureFactory("resources/textures"); auto matWhite = MaterialCore::create(); matWhite->setAmbientAndDiffuse(glm::vec4(1.f, 1.f, 1.f, 1.f)) ->setSpecular(glm::vec4(0.1f, 0.1f, 0.1f, 1.f)) ->setShininess(5.f) ->init(); auto skyboxTrans = createSkybox(geometryFactory, textureFactory, shaderSkybox, matWhite); auto slenderTrans = createSlenderman(geometryFactory, textureFactory, shaderPhongTex, matWhite); auto mazeScene = Group::create(); mazeScene->addCore(shaderPhong); mazeScene->addChild(camera)->addChild(light); light->addChild(skyboxTrans); light->addChild(slenderTrans); for (int i = 0; i < maze::MAZE_SIZE; i++) { for (int j = 0; j < maze::MAZE_SIZE; j++) { light->addChild(cell::createCell(j, i, maze1, shaderBumpTex)); } } scene = mazeScene; }
void PostProcessor::render() const { if (!frameBuffer.get() || !tempFrameBuffer.get() || !bloomFrameBuffer.get() || !depthOfFieldFrameBuffer.get() || !blurTexture1DArray.get() || !bloomTexture1DArray.get() || !depthOfFieldTexture1DArray.get() ) { return; } CameraSP currentCamera = CameraManager::getInstance()->getDefaultOrthographicCamera(); Matrix4x4 modelMatrix; modelMatrix.scale(static_cast<GLfloat>(frameBuffer->getWidth()), static_cast<GLfloat>(frameBuffer->getHeight()), 1.0f); program->use(); setUniforms(); glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); glUniformMatrix4fv(program->getUniformLocation(u_projectionMatrix), 1, GL_FALSE, currentCamera->getProjectionMatrix().getM()); glUniformMatrix4fv(program->getUniformLocation(u_viewMatrix), 1, GL_FALSE, currentCamera->getViewMatrix().getM()); glUniformMatrix4fv(program->getUniformLocation(u_modelMatrix), 1, GL_FALSE, modelMatrix.getM()); // const CameraSP& defaultCamera = CameraManager::getInstance()->getDefaultPerspectiveCamera(); Point4 focalPoint = defaultCamera->getEye() + (defaultCamera->getDirection() * focal); Point4 focusedObjectPoint = defaultCamera->getEye() + (defaultCamera->getDirection() * focusedObject); Point4 biasedFocalPoint = defaultCamera->getBiasedProjectionMatrix() * defaultCamera->getViewMatrix() * focalPoint; Point4 biasedFocusedObjectPoint = defaultCamera->getBiasedProjectionMatrix() * defaultCamera->getViewMatrix() * focusedObjectPoint; glUniform1f(program->getUniformLocation(u_aperture), aperture); glUniform1f(program->getUniformLocation(u_focal), glusClampf(biasedFocalPoint.getZ(), 0.0f, 1.0f)); glUniform1f(program->getUniformLocation(u_focusedObject), glusClampf(biasedFocusedObjectPoint.getZ(), 0.0f, 1.0f)); // // Depth texture glActiveTexture(GL_TEXTURE3); glBindTexture(target, frameBuffer->getDepthTexture()->getTextureName()); glUniform1i(program->getUniformLocation(depthTexture), 3); glActiveTexture(GL_TEXTURE0); // Create bloom textures if (useBloom) { use(false); // Only the bright texture is needed glActiveTexture(GL_TEXTURE0); glBindTexture(target, frameBuffer->getColor1Texture()->getTextureName()); glUniform1i(program->getUniformLocation(screenTexture), 0); // Not used. Just take the color texture as a dummy value glActiveTexture(GL_TEXTURE1); glBindTexture(target, frameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(bloomTexture), 1); // Blur using the bloom texture glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_1D_ARRAY, bloomTexture1DArray->getTextureName()); glUniform1i(program->getUniformLocation(u_blurTexture), 2); glActiveTexture(GL_TEXTURE0); glUniform1i(program->getUniformLocation(u_useDoF), false); glUniform1i(program->getUniformLocation(u_useBlur), true); glUniform1i(program->getUniformLocation(u_useBloom), false); glUniform1f(program->getUniformLocation(u_bloomLevel), bloomLevel); glUniform1i(program->getUniformLocation(u_useExposure), false); glUniform1f(program->getUniformLocation(u_exposure), exposure); glUniform1i(program->getUniformLocation(u_useGamma), false); glUniform1f(program->getUniformLocation(u_gamma), gamma); postProcessorVAO->bind(); // First pass ... glUniform1i(program->getUniformLocation(u_blurHorizontal), 1); glUniform1i(program->getUniformLocation(u_blurVertical), 0); tempFrameBuffer->use(true); glDrawElements(GL_TRIANGLES, numberIndices, GL_UNSIGNED_INT, 0); tempFrameBuffer->use(false); // ... and final pass glBindTexture(target, tempFrameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(u_blurHorizontal), 0); glUniform1i(program->getUniformLocation(u_blurVertical), 1); bloomFrameBuffer->use(true); glDrawElements(GL_TRIANGLES, numberIndices, GL_UNSIGNED_INT, 0); bloomFrameBuffer->use(false); // postProcessorVAO->unbind(); glBindTexture(target, 0); } FrameBufferSP usedFrameBuffer = frameBuffer; // Bloom the framebuffer if (useBloom) { use(false); // Original frame buffer glActiveTexture(GL_TEXTURE0); glBindTexture(target, frameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(screenTexture), 0); // Bloom glActiveTexture(GL_TEXTURE1); glBindTexture(target, bloomFrameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(bloomTexture), 1); // Dummy glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_1D_ARRAY, bloomTexture1DArray->getTextureName()); glUniform1i(program->getUniformLocation(u_blurTexture), 2); glActiveTexture(GL_TEXTURE0); glUniform1i(program->getUniformLocation(u_useDoF), false); glUniform1i(program->getUniformLocation(u_useBlur), false); glUniform1i(program->getUniformLocation(u_useBloom), true); glUniform1f(program->getUniformLocation(u_bloomLevel), bloomLevel); glUniform1i(program->getUniformLocation(u_useExposure), false); glUniform1f(program->getUniformLocation(u_exposure), exposure); glUniform1i(program->getUniformLocation(u_useGamma), false); glUniform1f(program->getUniformLocation(u_gamma), gamma); glUniform1i(program->getUniformLocation(u_blurHorizontal), 0); glUniform1i(program->getUniformLocation(u_blurVertical), 0); postProcessorVAO->bind(); // One pass tempFrameBuffer->use(true); glDrawElements(GL_TRIANGLES, numberIndices, GL_UNSIGNED_INT, 0); tempFrameBuffer->use(false); // postProcessorVAO->unbind(); glBindTexture(target, 0); usedFrameBuffer = tempFrameBuffer; } // Depth of field if (useDoF) { use(false); // Only the color texture is needed glActiveTexture(GL_TEXTURE0); glBindTexture(target, usedFrameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(screenTexture), 0); // Not used. Just take the color texture as a dummy value glActiveTexture(GL_TEXTURE1); glBindTexture(target, frameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(bloomTexture), 1); // Blur using the DOF texture glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_1D_ARRAY, depthOfFieldTexture1DArray->getTextureName()); glUniform1i(program->getUniformLocation(u_blurTexture), 2); glActiveTexture(GL_TEXTURE0); glUniform1i(program->getUniformLocation(u_useDoF), true); glUniform1i(program->getUniformLocation(u_useBlur), false); glUniform1i(program->getUniformLocation(u_useBloom), false); glUniform1f(program->getUniformLocation(u_bloomLevel), bloomLevel); glUniform1i(program->getUniformLocation(u_useExposure), false); glUniform1f(program->getUniformLocation(u_exposure), exposure); glUniform1i(program->getUniformLocation(u_useGamma), false); glUniform1f(program->getUniformLocation(u_gamma), gamma); postProcessorVAO->bind(); // First pass ... glUniform1i(program->getUniformLocation(u_blurHorizontal), 1); glUniform1i(program->getUniformLocation(u_blurVertical), 0); tempFrameBuffer->use(true); glDrawElements(GL_TRIANGLES, numberIndices, GL_UNSIGNED_INT, 0); tempFrameBuffer->use(false); // ... and final pass glBindTexture(target, tempFrameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(u_blurHorizontal), 0); glUniform1i(program->getUniformLocation(u_blurVertical), 1); depthOfFieldFrameBuffer->use(true); glDrawElements(GL_TRIANGLES, numberIndices, GL_UNSIGNED_INT, 0); depthOfFieldFrameBuffer->use(false); // postProcessorVAO->unbind(); glBindTexture(target, 0); usedFrameBuffer = depthOfFieldFrameBuffer; } // use(false); // Color glActiveTexture(GL_TEXTURE0); glBindTexture(target, usedFrameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(screenTexture), 0); // Not used. glActiveTexture(GL_TEXTURE1); glBindTexture(target, bloomFrameBuffer->getColor0Texture()->getTextureName()); glUniform1i(program->getUniformLocation(bloomTexture), 1); // Blur glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_1D_ARRAY, blurTexture1DArray->getTextureName()); glUniform1i(program->getUniformLocation(u_blurTexture), 2); glActiveTexture(GL_TEXTURE0); glUniform1i(program->getUniformLocation(u_useDoF), false); glUniform1i(program->getUniformLocation(u_useBlur), useBlur); glUniform1i(program->getUniformLocation(u_blurHorizontal), 1); glUniform1i(program->getUniformLocation(u_blurVertical), 1); glUniform1i(program->getUniformLocation(u_useBloom), false); glUniform1f(program->getUniformLocation(u_bloomLevel), bloomLevel); glUniform1i(program->getUniformLocation(u_useExposure), useExposure); glUniform1f(program->getUniformLocation(u_exposure), exposure); glUniform1i(program->getUniformLocation(u_useGamma), useGamma); glUniform1f(program->getUniformLocation(u_gamma), gamma); postProcessorVAO->bind(); glDrawElements(GL_TRIANGLES, numberIndices, GL_UNSIGNED_INT, 0); postProcessorVAO->unbind(); // glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); glBindTexture(target, 0); }
bool ModelEntity::setOrthographicCameraCascadedShadowMap(const string& lightName, const CameraSP& camera, const OrthographicCameraCascadedShadowMap2DSP& orthographicCameraCascadedShadowMap2D, int32_t section) const { Quaternion baseRotation(-90.0f, Vector3(1.0f, 0.0f, 0.0f)); baseRotation *= Quaternion(90.0f, Vector3(0.0f, 1.0f, 0.0f)); auto walker = allLights.begin(); InstanceNodeSP instanceNode; while (walker != allLights.end()) { instanceNode = *walker; if (instanceNode->getNode()->getName().compare(lightName) == 0) { orthographicCameraCascadedShadowMap2D->getOrthographicCamera(section)->adjustToFrustum(camera->getViewFrustum(), section, instanceNode->getPosition(), instanceNode->getRotation() * baseRotation); ProgramManagerProxy::setCameraByType(GeneralEntity::currentProgramType, orthographicCameraCascadedShadowMap2D->getOrthographicCamera(section), Point4(), Quaternion(), false); return true; } walker++; } return false; }
GLUSboolean updateGame(GLUSfloat deltaTime) { //string currentProgramType = ProgramManager::RENDER_TO_SHADOWMAP_PROGRAM_TYPE; string currentProgramType = ProgramManager::DEFAULT_PROGRAM_TYPE; if (!updateEngine(deltaTime)) { return GLUS_FALSE; } // Update everything GeneralEntityManager::getInstance()->update(); // // Shadow part // GeneralEntity::setCurrentValues(ProgramManager::RENDER_TO_SHADOWMAP_PROGRAM_TYPE, orthographicCameraShadowMap2D->getOrthographicCamera(), deltaTime, false); // Camera modelEntity->setOrthographicShadowCamera("Lamp", orthographicCameraShadowMap2D); orthographicCameraShadowMap2D->updateShadowMatrix(); // Lights, not used, so set to zero lights. ProgramManagerProxy::setNumberLightsByType(ProgramManager::RENDER_TO_SHADOWMAP_PROGRAM_TYPE, 0); ProgramManagerProxy::setAmbientLightColorByType(ProgramManager::RENDER_TO_SHADOWMAP_PROGRAM_TYPE); ProgramManagerProxy::setNoShadowByType(ProgramManager::RENDER_TO_SHADOWMAP_PROGRAM_TYPE); // GeneralEntityManager::getInstance()->sort(); // orthographicCameraShadowMap2D->use(true); glClear(GL_DEPTH_BUFFER_BIT); // All the primitves glEnable(GL_POLYGON_OFFSET_FILL); glFrontFace(GL_CW); GeneralEntityManager::getInstance()->render(); glDisable(GL_POLYGON_OFFSET_FILL); glFrontFace(GL_CCW); orthographicCameraShadowMap2D->use(false); // // Color rendering // ViewportSP defaultViewport = ViewportManager::getInstance()->getDefaultViewport(); defaultViewport->use(); GeneralEntity::setCurrentValues(currentProgramType, currentCamera, deltaTime, false); // Camera if (User::defaultUser.getUserCamera().get()) { ProgramManagerProxy::setCameraByType(currentProgramType, currentCamera, Point4(), Quaternion()); } else { modelEntity->setCamera("Camera"); } // Lights int32_t numberLights = 0; numberLights = modelEntity->setLights(numberLights); ProgramManagerProxy::setNumberLightsByType(currentProgramType, numberLights); ProgramManagerProxy::setAmbientLightColorByType(currentProgramType); ProgramManagerProxy::setNoShadowByType(currentProgramType); ProgramManagerProxy::setShadowByType(currentProgramType, 0, orthographicCameraShadowMap2D->getShadowMap2D(), orthographicCameraShadowMap2D->getShadowMatrix(), 0); // GeneralEntityManager::getInstance()->sort(); // glClearColor(0.9f, 0.9f, 0.9f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // All the primitves GeneralEntityManager::getInstance()->render(); // // Debug // if (drawDebug) { CameraManager::getInstance()->getDefaultPerspectiveCamera()->debugDraw(Point4(), Quaternion()); } // // Debug plane groundPlane.draw(currentCamera->getEye(), Color::GREY); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // FPS FpsPrinter::printer.print(deltaTime); // Debug Menu FontSP font = FontManager::getInstance()->getFont("CourierNew"); font->print(760.0f, 10.0f, Color::RED, "Toggle camera: [c]"); return GLUS_TRUE; }