/// x,y and height are in [meters] Eigen::Vector2f MetricCamera::project_ground_plane_point( const GroundPlane &ground_plane, const float x, const float y, const float height) const { //Eigen::Vector3f xyz_point = camera_focal_point + forward_axis*y + left_axis*x; //const Eigen::Vector3f closest_point_on_plane = ground_plane.projection(xyz_point); //xyz_point = closest_point_on_plane + ground_plane.normal()*height; if(false) { // mini test const float d1 = ground_plane.signedDistance(camera_focal_point); const Eigen::Vector3f xyz_point = camera_focal_point + forward_axis*10; // 10 meters foward const float d2 = ground_plane.signedDistance(xyz_point); //printf("d@0 == %.3f, d@10 == %.3f, d@10 <= d@0 %s\n", d1, d2, (d2 <= d1)? "true": "false"); printf("camera looking %s\n", (d2 <= d1)? "down": "up"); //assert(d2 <= d1); } const Eigen::Vector3f plane_zero_zero = ground_plane.projection(camera_focal_point); const Eigen::Vector3f &ground_left_axis = left_axis; const Eigen::Vector3f ground_plane_normal = ground_plane.normal(); const Eigen::AngleAxis<float> aa(M_PI/2, left_axis); const Eigen::Vector3f ground_forward_axis = aa * ground_plane_normal; const Eigen::Vector3f object_bottom = plane_zero_zero + ground_forward_axis*y + ground_left_axis*x; Eigen::Vector3f xyz_point = object_bottom + ground_plane.normal()*height; Eigen::Vector2f uv_point = project_3d_point(xyz_point); return uv_point; }
GameObject::GameObject( const GroundPlane& ground_plane, const Mesh& mesh, Shaders& shaders) : should_move_(true), should_blue_(false), center_( ground_plane.x_bounds().shrink(2*kRadius).randomInclusive(), kRadius, ground_plane.z_bounds().shrink(2*kRadius).randomInclusive()), velocity_(0.005f * cos(rand()), 0, 0.005f * sin(rand())), blue_uniform_(shaders.getUniforms(Uniform::BLUE)), mesh_(mesh), model_matrix_(shaders, glm::mat4()) {}
void GameObject::step(units::MS dt, const GroundPlane& ground_plane, std::vector<GameObject>& game_objects) { if (!should_move_) return; const glm::vec3 delta = velocity_ * static_cast<float>(dt); const glm::vec3 new_center = center_ + delta; if (Bounds(new_center.x - kRadius, new_center.x + kRadius).within(ground_plane.x_bounds()) && Bounds(new_center.z - kRadius, new_center.z + kRadius).within(ground_plane.z_bounds())) { bool collided = false; for (auto& g : game_objects) { if (this != &g && BoundingSphere{new_center, kRadius}.collides(g.bounding_sphere())) { onCollision(); g.onCollision(); collided = true; } } if (!collided) center_ = new_center; } else { velocity_ = -velocity_; step(dt, ground_plane, game_objects); } }
GLUSboolean updateGame(GLUSfloat deltaTime) { if (!updateEngine(deltaTime)) { return GLUS_FALSE; } updateInput(deltaTime); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); ProgramManagerProxy::setCameraByType(ProgramManager::DEFAULT_PROGRAM_TYPE, CameraManager::getInstance()->getDefaultPerspectiveCamera()); ModelEntity::setCurrentValues(ProgramManager::DEFAULT_PROGRAM_TYPE, CameraManager::getInstance()->getDefaultPerspectiveCamera(), deltaTime, false); ModelEntityManager::getInstance()->updateMetrics(); ModelEntityManager::getInstance()->sort(); ModelEntityManager::getInstance()->update(); // PostProcessorSP postProcessor; if (useMultisample) { postProcessor = PostProcessor2DMultisampleManager::getInstance()->getPostProcessor("PostProcessorMultisample"); } else { postProcessor = PostProcessor2DManager::getInstance()->getPostProcessor("PostProcessor"); } postProcessor->use(true); glEnable(GL_MULTISAMPLE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Debug output of the ground plane groundPlane.draw(CameraManager::getInstance()->getDefaultPerspectiveCamera()->getEye(), Color::GREY); SkyManager::getInstance()->getActiveSky()->render(CameraManager::getInstance()->getDefaultPerspectiveCamera()->getEye()); ModelEntityManager::getInstance()->render(); postProcessor->render(); // No MSAA for the text glDisable(GL_MULTISAMPLE); FpsPrinter::printer.print(deltaTime); // FontSP font = FontManager::getInstance()->getFont("CourierNew"); font->print(690.0f, 10.0f, Color::RED, "Toggle MSAA: [m]"); font->print(690.0f, 25.0f, Color::RED, "MSAA: %s", useMultisample ? "On" : "Off"); font->print(690.0f, 40.0f, Color::RED, "Toggle DoF: [f]"); font->print(690.0f, 55.0f, Color::RED, "Depth of Field: %s", useDepthOfField ? "On" : "Off"); font->print(690.0f, 70.0f, Color::RED, "Dec./Inc. aperture: [1]/[2]"); font->print(690.0f, 85.0f, Color::RED, "Aperture: %.1f", aperture); font->print(690.0f, 100.0f, Color::RED, "Dec./Inc. focal: [3]/[4]"); font->print(690.0f, 115.0f, Color::RED, "Focal: %.1f", focal); font->print(690.0f, 130.0f, Color::RED, "Dec./Inc. focused object: [5]/[6]"); font->print(690.0f, 145.0f, Color::RED, "Focused object: %.1f", focusedObject); return GLUS_TRUE; }
GLUSboolean updateGame(GLUSfloat deltaTime) { if (!updateEngine(deltaTime)) { return GLUS_FALSE; } // Update the paths PathEntityManager::getInstance()->updateEntities(deltaTime); // Update everything GeneralEntityManager::getInstance()->update(); // // Render the dynamic cube maps // auto allElements = DynamicEnvironmentManager::getInstance()->getKeyValueMap(); auto walker = allElements.begin(); while (walker != allElements.end()) { auto currentEntity = walker->first; entityExcludeList->addEntity(currentEntity); auto currentDynamicEnvironment = walker->second; currentDynamicEnvironment->use(currentEntity->getBoundingSphere().getCenter()); const Viewport& dynamicEnvironmentViewport = currentDynamicEnvironment->getCamera(0)->getViewport(); dynamicEnvironmentViewport.use(); ProgramManagerProxy::setCameraByType(ProgramManager::RENDER_TO_CUBEMAP_PROGRAM_TYPE, currentDynamicEnvironment->getCamera(0), Point4(), Quaternion()); GeneralEntity::setCurrentValues(ProgramManager::RENDER_TO_CUBEMAP_PROGRAM_TYPE, currentDynamicEnvironment->getCamera(0), deltaTime, false); GeneralEntityManager::getInstance()->sort(); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // GeneralEntity::setCurrentValues(ProgramManager::RENDER_TO_CUBEMAP_PROGRAM_TYPE, currentDynamicEnvironment->getCamera(0), deltaTime, false, RENDER_OPAQUE); GeneralEntityManager::getInstance()->render(true); GeneralEntity::setCurrentValues(ProgramManager::RENDER_TO_CUBEMAP_PROGRAM_TYPE, currentDynamicEnvironment->getCamera(0), deltaTime, true, RENDER_TRANSPARENT); glFrontFace(GL_CW); GeneralEntityManager::getInstance()->render(true); glFrontFace(GL_CCW); GeneralEntityManager::getInstance()->render(true); // entityExcludeList->clear(); currentDynamicEnvironment->unuse(); walker++; } // // Render the normal scene // ViewportSP defaultViewport = ViewportManager::getInstance()->getDefaultViewport(); defaultViewport->use(); ProgramManagerProxy::setCameraByType(ProgramManager::DEFAULT_PROGRAM_TYPE, CameraManager::getInstance()->getDefaultPerspectiveCamera(), Point4(), Quaternion()); GeneralEntity::setCurrentValues(ProgramManager::DEFAULT_PROGRAM_TYPE, CameraManager::getInstance()->getDefaultPerspectiveCamera(), deltaTime, false); 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); // Sky SkyManager::getInstance()->getActiveSky()->render(CameraManager::getInstance()->getDefaultPerspectiveCamera()->getEye()); // Debug plane groundPlane.draw(CameraManager::getInstance()->getDefaultPerspectiveCamera()->getEye(), Color::GREY); // All the primitves GeneralEntity::setCurrentValues(ProgramManager::DEFAULT_PROGRAM_TYPE, CameraManager::getInstance()->getDefaultPerspectiveCamera(), deltaTime, false, RENDER_OPAQUE, true); GeneralEntityManager::getInstance()->render(); GeneralEntity::setCurrentValues(ProgramManager::DEFAULT_PROGRAM_TYPE, CameraManager::getInstance()->getDefaultPerspectiveCamera(), deltaTime, true, RENDER_TRANSPARENT, true); glFrontFace(GL_CW); GeneralEntityManager::getInstance()->render(); glFrontFace(GL_CCW); GeneralEntityManager::getInstance()->render(); // FPS FpsPrinter::printer.print(deltaTime); return GLUS_TRUE; }
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; }