#include "OgreVisibilityTerrainSceneManager.h" #include "OgreVisibilityOptionsManager.h" #include #include #include #include #include #include #include #include namespace Ogre { //----------------------------------------------------------------------- VisibilityTerrainSceneManager::VisibilityTerrainSceneManager( GtpVisibility::VisibilityManager *visManager): mVisibilityManager(visManager), mRenderDepthPass(false), mShowVisualization(false), mRenderNodesForViz(false), mRenderNodesContentForViz(false), mVisualizeCulledNodes(false), mLeavePassesInQueue(0), mDelayRenderTransparents(true), mUseDepthPass(false), mRenderItemBuffer(true), mCurrentEntityId(0), mEnableDepthWrite(true), mSkipTransparents(false), mSavedShadowTechnique(SHADOWTYPE_NONE) { mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem); mQueryManager = new PlatformQueryManager(mHierarchyInterface, mCurrentViewport); mVisibilityManager->SetQueryManager(mQueryManager); //mDisplayNodes = true; //mShowBoundingBoxes = true; // TODO: set maxdepth to reasonable value mMaxDepth = 50; } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::InitDepthPass() { MaterialPtr depthMat = MaterialManager::getSingleton().getByName("Visibility/DepthPass"); if (depthMat.isNull()) { // Init depthMat = MaterialManager::getSingleton().create( "Visibility/DepthPass", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); mDepthPass = depthMat->getTechnique(0)->getPass(0); mDepthPass->setColourWriteEnabled(false); mDepthPass->setDepthWriteEnabled(true); mDepthPass->setLightingEnabled(false); } else { mDepthPass = depthMat->getTechnique(0)->getPass(0); } } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::InitItemBufferPass() { MaterialPtr itemBufferMat = MaterialManager::getSingleton(). getByName("Visibility/ItemBufferPass"); if (itemBufferMat.isNull()) { // Init itemBufferMat = MaterialManager::getSingleton().create("Visibility/ItemBufferPass", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0); mItemBufferPass->setColourWriteEnabled(true); mItemBufferPass->setDepthWriteEnabled(true); mItemBufferPass->setLightingEnabled(true); } else { mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0); } mItemBufferPass->setAmbient(1, 1, 0); } //----------------------------------------------------------------------- VisibilityTerrainSceneManager::~VisibilityTerrainSceneManager() { if (mHierarchyInterface) { delete mHierarchyInterface; mHierarchyInterface = NULL; } if (mQueryManager) { delete mQueryManager; mQueryManager = NULL; } } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::PrepareVisualization(Camera *cam) { // add player camera for visualization purpose try { Camera *c; if ((c = getCamera("PlayerCam")) != NULL) { getRenderQueue()->addRenderable(c); } } catch (...) { // ignore } for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) { getRenderQueue()->addRenderable(*it); } if (mRenderNodesForViz || mRenderNodesContentForViz) { // change node material so it is better suited for visualization MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial"); nodeMat->setAmbient(1, 1, 0); nodeMat->setLightingEnabled(true); nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates(); for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it) { if (mRenderNodesForViz) { OctreeNode *node = *it; if ((node->numAttachedObjects() > 0) && (node->numChildren() == 0) && node->getAttachedObject(0)->getMovableType() == "Entity") getRenderQueue()->addRenderable(node); // addbounding boxes instead of node itself //(*it)->_addBoundingBoxToQueue(getRenderQueue()); } if (mRenderNodesContentForViz) { (*it)->_addToRenderQueue(cam, getRenderQueue(), false); } } } } //----------------------------------------------------------------------- Pass *VisibilityTerrainSceneManager::setPass(Pass* pass) { // TODO: setting vertex program is not efficient //Pass *usedPass = ((mRenderDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass); // set depth fill pass only if depth write enabled Pass *usedPass = (mRenderDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? mDepthPass : pass); IlluminationRenderStage savedStage = mIlluminationStage; // set illumination stage to NONE so no shadow material is used // for depth pass or for occlusion query if (mRenderDepthPass || mHierarchyInterface->IsBoundingBoxQuery()) { mIlluminationStage = IRS_NONE; } if (mRenderDepthPass) { // --- set vertex program of current pass so z-buffer is updated correctly if (pass->hasVertexProgram()) { mDepthPass->setVertexProgram(pass->getVertexProgramName()); if (mDepthPass->hasVertexProgram()) { const GpuProgramPtr& prg = mDepthPass->getVertexProgram(); // Load this program if not done already if (!prg->isLoaded()) prg->load(); // Copy params mDepthPass->setVertexProgramParameters(pass->getVertexProgramParameters()); } } else if (mDepthPass->hasVertexProgram()) { mDepthPass->setVertexProgram(""); } } bool IsDepthWrite = usedPass->getDepthWriteEnabled(); // global option which enables / disables depth writes if (!mEnableDepthWrite) { usedPass->setDepthWriteEnabled(false); } //else if (mIsItemBufferPass) {usedPass = mItemBufferPass;} Pass *result = SceneManager::setPass(usedPass); // reset depth write if (!mEnableDepthWrite) { usedPass->setDepthWriteEnabled(IsDepthWrite); } // reset illumination stage mIlluminationStage = savedStage; return result; } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters) { // needs full ambient lighting for item colors to be exact //if (mRenderItemBuffer) {setAmbientLight(ColourValue(1,1,1,1));} //-- show visible scene nodes and octree bounding boxes from last frame if (mShowVisualization) { PrepareVisualization(cam); } else { // for hierarchical culling, we interleave identification // and rendering of objects in _renderVisibibleObjects // only for the shadow pass we use standard rendering if (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && mIlluminationStage == IRS_RENDER_TO_TEXTURE) { TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters); } // only shadow casters will be rendered in shadow texture pass //mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters); } //TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters); mVisible.clear(); mBoxes.clear(); } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::_renderVisibleObjects() { // visualization or shadow pass: apply standard rendering if (mShowVisualization || (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && mIlluminationStage == IRS_RENDER_TO_TEXTURE)) { IlluminationRenderStage savedStage = mIlluminationStage; if (mShowVisualization) // disable illumination stage so we have no shadows in visualization mIlluminationStage = IRS_NONE; // standard rendering for shadow maps because of performance TerrainSceneManager::_renderVisibleObjects(); mIlluminationStage = savedStage; } else // the hierarchical culling algorithm { //-- hierarchical culling // the objects of different layers (e.g., background, scene, // overlay) must be identified and rendered one after another //-- render all early skies clearSpecialCaseRenderQueues(); addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND); addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY); setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE); TerrainSceneManager::_renderVisibleObjects(); #ifdef GTP_VISIBILITY_MODIFIED_OGRE _deleteRenderedQueueGroups(); #endif //-- prepare queue for visible objects (i.e., all but overlay and skies late) clearSpecialCaseRenderQueues(); addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_LATE); addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY); setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE); //-- set all necessary parameters for hierarchical visibility culling and rendering InitVisibilityCulling(mCameraInProgress); /** * the hierarchical culling algorithm * if using depth pass: will just find objects and update depth buffer * for "delayed" rendering: will render some passes afterwards, e.g. transparents **/ mVisibilityManager->ApplyVisibilityCulling(); // delete remaining renderables from queue (all not in mLeavePassesInQueue) #ifdef GTP_VISIBILITY_MODIFIED_OGRE _deleteRenderedQueueGroups(mLeavePassesInQueue); #endif //-- reset parameters mRenderDepthPass = false; mSkipTransparents = false; mLeavePassesInQueue = 0; mShadowTechnique = mSavedShadowTechnique; // add visible nodes found by the visibility culling algorithm if (mUseDepthPass) { for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it) { (*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false); } } //-- now we can render all remaining queue objects // for depth pass, transparents, overlay clearSpecialCaseRenderQueues(); TerrainSceneManager::_renderVisibleObjects(); } // set the new render level index afterwards => new level in the next frame int levelIdx = (TerrainRenderable::getCurrentRenderLevelIndex() + 1) % 10; TerrainRenderable::setCurrentRenderLevelIndex(levelIdx); getRenderQueue()->clear(); //WriteLog(); // write out stats } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam) { mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface); mHierarchyInterface->SetRenderSystem(mDestRenderSystem); #ifdef GTP_VISIBILITY_MODIFIED_OGRE mHierarchyInterface->SetNumOctreeNodes(mNumOctreeNodes); #endif TerrainSceneManager::_updateSceneGraph(cam); } //----------------------------------------------------------------------- bool VisibilityTerrainSceneManager::setOption(const String & key, const void * val) { if (key == "UseDepthPass") { mUseDepthPass = (*static_cast(val)); return true; } if (key == "PrepareVisualization") { mShowVisualization = (*static_cast(val)); return true; } if (key == "RenderNodesForViz") { mRenderNodesForViz = (*static_cast(val)); return true; } if (key == "RenderNodesContentForViz") { mRenderNodesContentForViz = (*static_cast(val)); return true; } if (key == "SkyBoxEnabled") { mSkyBoxEnabled = (*static_cast(val)); return true; } if (key == "SkyPlaneEnabled") { mSkyPlaneEnabled = (*static_cast(val)); return true; } if (key == "SkyDomeEnabled") { mSkyDomeEnabled = (*static_cast(val)); return true; } if (key == "VisualizeCulledNodes") { mVisualizeCulledNodes = (*static_cast(val)); return true; } if (key == "DelayRenderTransparents") { mDelayRenderTransparents = (*static_cast(val)); return true; } // notifiy that frame has ended so terrain render level can be reset for correct // terrain rendering if (key == "TerrainLevelIdx") { TerrainRenderable::setCurrentRenderLevelIndex((*static_cast(val))); return true; } if (key == "DepthWrite") { mEnableDepthWrite = (*static_cast(val)); return true; } return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface). setOption(key, val) || TerrainSceneManager::setOption(key, val); } //----------------------------------------------------------------------- bool VisibilityTerrainSceneManager::getOption(const String & key, void *val) { if (key == "NumHierarchyNodes") { * static_cast(val) = (unsigned int)mNumOctreeNodes; return true; } return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface). getOption(key, val) && TerrainSceneManager::getOption(key, val); } //----------------------------------------------------------------------- bool VisibilityTerrainSceneManager::getOptionValues(const String & key, StringVector &refValueList) { return TerrainSceneManager::getOptionValues( key, refValueList); } //----------------------------------------------------------------------- bool VisibilityTerrainSceneManager::getOptionKeys(StringVector & refKeys) { return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface). getOptionKeys(refKeys) || TerrainSceneManager::getOptionKeys(refKeys); } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::setVisibilityManager(GtpVisibility::VisibilityManager *visManager) { mVisibilityManager = visManager; } //----------------------------------------------------------------------- GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::getVisibilityManager( void ) { return mVisibilityManager; } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::WriteLog() { std::stringstream d; d << "Depth pass: " << StringConverter::toString(mUseDepthPass) << ", " << "Delay transparents: " << StringConverter::toString(mDelayRenderTransparents) << ", " << "Use optimization: " << StringConverter::toString(mHierarchyInterface->GetUseOptimization()) << ", " << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", " << "Hierarchy nodes: " << mNumOctreeNodes << ", " << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", " << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", " << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", " << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", " << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << "\n"; LogManager::getSingleton().logMessage(d.str()); } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::renderObjects( const RenderPriorityGroup::TransparentRenderablePassList& objs, bool doLightIteration, const LightList* manualLightList) { // for correct rendering, transparents must be rendered after hierarchical culling if (!mSkipTransparents) { OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList); } } //----------------------------------------------------------------------- bool VisibilityTerrainSceneManager::validatePassForRendering(Pass* pass) { // skip all but first pass if we are doing the depth pass if ((mRenderDepthPass || mRenderItemBuffer) && pass->getIndex() > 0) { return false; } return SceneManager::validatePassForRendering(pass); } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::renderQueueGroupObjects(RenderQueueGroup* pGroup) { if (!mRenderItemBuffer) { TerrainSceneManager::renderQueueGroupObjects(pGroup); return; } //--- item buffer // Iterate through priorities RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator(); while (groupIt.hasMoreElements()) { RenderItemBuffer(groupIt.getNext()); } } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::RenderItemBuffer(RenderPriorityGroup* pGroup) { // Do solids RenderPriorityGroup::SolidRenderablePassMap solidObjs = pGroup->_getSolidPasses(); // ----- SOLIDS LOOP ----- RenderPriorityGroup::SolidRenderablePassMap::const_iterator ipass, ipassend; ipassend = solidObjs.end(); for (ipass = solidObjs.begin(); ipass != ipassend; ++ipass) { // Fast bypass if this group is now empty if (ipass->second->empty()) continue; // Render only first pass if (ipass->first->getIndex() > 0) continue; RenderPriorityGroup::RenderableList* rendList = ipass->second; RenderPriorityGroup::RenderableList::const_iterator irend, irendend; irendend = rendList->end(); for (irend = rendList->begin(); irend != irendend; ++irend) { std::stringstream d; d << "itembuffer, pass name: " << ipass->first->getParent()->getParent()->getName(); LogManager::getSingleton().logMessage(d.str()); RenderSingleObjectForItemBuffer(*irend, ipass->first); } } // ----- TRANSPARENT LOOP: must be handled differently // although we don't really care about transparents for the item buffer // TODO: HOW TO HANDLE OCCLUDED OBJECTS ???? RenderPriorityGroup::TransparentRenderablePassList transpObjs = pGroup->_getTransparentPasses(); RenderPriorityGroup::TransparentRenderablePassList::const_iterator itrans, itransend; itransend = transpObjs.end(); for (itrans = transpObjs.begin(); itrans != itransend; ++itrans) { // like for solids, render only first pass if (itrans->pass->getIndex() == 0) { RenderSingleObjectForItemBuffer(itrans->renderable, itrans->pass); } } } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::RenderSingleObjectForItemBuffer(Renderable *rend, Pass *pass) { static LightList nullLightList; Real col = (Real)rend->getId() / (Real)mCurrentEntityId; mItemBufferPass->setAmbient(ColourValue(0, col, 0)); //mItemBufferPass->setDiffuse(ColourValue(0, col, 0)); //mItemBufferPass->setSpecular(ColourValue(0, col, 0)); // set vertex program of current pass if (pass->hasVertexProgram()) { mItemBufferPass->setVertexProgram(pass->getVertexProgramName()); if (mItemBufferPass->hasVertexProgram()) { const GpuProgramPtr& prg = mItemBufferPass->getVertexProgram(); // Load this program if not done already if (!prg->isLoaded()) prg->load(); // Copy params mItemBufferPass->setVertexProgramParameters(pass->getVertexProgramParameters()); } } else if (mItemBufferPass->hasVertexProgram()) { mItemBufferPass->setVertexProgram(""); } Pass *usedPass = setPass(mItemBufferPass); //Pass *usedPass = setPass(pass); std::stringstream d; d << "item buffer id: " << rend->getId() << ", col: " << col; LogManager::getSingleton().logMessage(d.str()); // Render a single object, this will set up auto params if required renderSingleObject(rend, usedPass, false, &nullLightList); } //----------------------------------------------------------------------- GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::GetVisibilityManager() { return mVisibilityManager; } //----------------------------------------------------------------------- Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName, const String& meshName) { Entity *ent = SceneManager::createEntity(entityName, meshName); for (int i = 0; i < (int)ent->getNumSubEntities(); ++i) { ent->getSubEntity(i)->setId(mCurrentEntityId ++); } return ent; } //----------------------------------------------------------------------- void VisibilityTerrainSceneManager::InitVisibilityCulling(Camera *cam) { InitDepthPass(); // create material for depth pass InitItemBufferPass(); // create material for item buffer pass // save shadow technique. It will be reset after hierarchical culling mSavedShadowTechnique = mShadowTechnique; // set passes which should be stored in render queue // for rendering after hierarchical culling mLeavePassesInQueue = 0; if (mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE) { // render standard solids without shadows during hierarchical culling pass mShadowTechnique = SHADOWTYPE_NONE; if (!mUseDepthPass) { // TODO: remove this because should be processed in first pass mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW; mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DECAL; mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DIFFUSE_SPECULAR; mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES; } } if (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE) { // render standard solids without shadows during hierarchical culling pass mShadowTechnique = SHADOWTYPE_NONE; if (!mUseDepthPass) { mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW; mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES; } } if (mDelayRenderTransparents && (!mUseDepthPass)) { mLeavePassesInQueue |= RenderPriorityGroup::TRANSPARENT_PASSES; } //std::stringstream d; d << "leave passes in queue: " << mLeavePassesInQueue;LogManager::getSingleton().logMessage(d.str()); // possible two cameras (one for culling, one for rendering) mHierarchyInterface->InitFrame(mOctree, cam, mCullCamera ? getCamera("CullCamera") : NULL, mLeavePassesInQueue); // reset culling manager stats mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes); // set depth pass flag before rendering mRenderDepthPass = mUseDepthPass; // set flag for skipping transparents in the hierarchical culling pass mSkipTransparents = mUseDepthPass || (mLeavePassesInQueue & RenderPriorityGroup::TRANSPARENT_PASSES); } //----------------------------------------------------------------------- /*void VisibilityTerrainSceneManager::renderBasicQueueGroupObjects(RenderQueueGroup* pGroup) { // Basic render loop // Iterate through priorities RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator(); while (groupIt.hasMoreElements()) { RenderPriorityGroup* pPriorityGrp = groupIt.getNext(); // Sort the queue first pPriorityGrp->sort(mCameraInProgress); //TODO: render other splid passes for shadows // Do solids renderObjects(pPriorityGrp->_getSolidPassesNoShadows(), true); // do solid passes no shadows if addititive stencil shadows if (mSavedShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE) renderObjects(pPriorityGrp->_getSolidPassesNoShadows(), true); // Do transparents renderObjects(pPriorityGrp->_getTransparentPasses(), true); }// for each priority } */ } // namespace Ogre