[59] | 1 | #include "OgreVisibilityTerrainSceneManager.h"
|
---|
[74] | 2 | #include "OgreVisibilityOptionsManager.h"
|
---|
[59] | 3 | #include <OgreMath.h>
|
---|
| 4 | #include <OgreIteratorWrappers.h>
|
---|
| 5 | #include <OgreRenderSystem.h>
|
---|
| 6 | #include <OgreCamera.h>
|
---|
[93] | 7 | #include <OgreLogManager.h>
|
---|
[100] | 8 | #include <OgreStringConverter.h>
|
---|
[59] | 9 |
|
---|
| 10 |
|
---|
| 11 | namespace Ogre {
|
---|
[87] | 12 |
|
---|
[59] | 13 | //-----------------------------------------------------------------------
|
---|
[86] | 14 | VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
|
---|
[115] | 15 | GtpVisibility::VisibilityManager *visManager):
|
---|
| 16 | mVisibilityManager(visManager),
|
---|
| 17 | mUseDepthPass(false),
|
---|
| 18 | mIsDepthPass(false),
|
---|
[103] | 19 | mShowVisualization(false),
|
---|
[112] | 20 | mRenderNodesForViz(false),
|
---|
[113] | 21 | mRenderNodesContentForViz(false),
|
---|
[114] | 22 | mVisualizeCulledNodes(false),
|
---|
[115] | 23 | mSkipTransparents(false),
|
---|
| 24 | mDelayRenderTransparents(true)
|
---|
[59] | 25 | {
|
---|
| 26 | mHierarchyInterface =
|
---|
| 27 | new OctreeHierarchyInterface(this, mDestRenderSystem);
|
---|
| 28 |
|
---|
[114] | 29 | //mDisplayNodes = true;
|
---|
[85] | 30 | //mShowBoundingBoxes = true;
|
---|
[103] | 31 |
|
---|
| 32 | // TODO: set maxdepth to reasonable value
|
---|
[96] | 33 | mMaxDepth = 50;
|
---|
[115] | 34 | // create material for depth pass
|
---|
| 35 | CreateDepthPass();
|
---|
[59] | 36 | }
|
---|
| 37 | //-----------------------------------------------------------------------
|
---|
[115] | 38 | void VisibilityTerrainSceneManager::CreateDepthPass()
|
---|
| 39 | {
|
---|
| 40 | MaterialPtr depthMat = MaterialManager::getSingleton().getByName("Visibility/DepthPass");
|
---|
| 41 |
|
---|
| 42 | if (depthMat.isNull())
|
---|
| 43 | {
|
---|
| 44 | // Init
|
---|
| 45 | depthMat = MaterialManager::getSingleton().create(
|
---|
| 46 | "Visibility/DepthPass",
|
---|
| 47 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
| 48 | mDepthPass = depthMat->getTechnique(0)->getPass(0);
|
---|
| 49 | mDepthPass->setColourWriteEnabled(false);
|
---|
| 50 | mDepthPass->setDepthWriteEnabled(true);
|
---|
| 51 | mDepthPass->setLightingEnabled(false);
|
---|
| 52 | }
|
---|
| 53 | else
|
---|
| 54 | {
|
---|
| 55 | mDepthPass = depthMat->getTechnique(0)->getPass(0);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | //-----------------------------------------------------------------------
|
---|
[59] | 59 | VisibilityTerrainSceneManager::~VisibilityTerrainSceneManager()
|
---|
| 60 | {
|
---|
| 61 | delete mHierarchyInterface;
|
---|
| 62 | }
|
---|
| 63 | //-----------------------------------------------------------------------
|
---|
[115] | 64 | Pass *VisibilityTerrainSceneManager::setPass(Pass* pass)
|
---|
[59] | 65 | {
|
---|
[115] | 66 | // setting vertex program is not efficient
|
---|
| 67 | //Pass *usedPass = ((mIsDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass);
|
---|
| 68 | Pass *usedPass = (mIsDepthPass ? mDepthPass : pass);
|
---|
[113] | 69 |
|
---|
[115] | 70 | if (mIsDepthPass)
|
---|
[103] | 71 | {
|
---|
[115] | 72 | // set vertex program of current pass
|
---|
| 73 | if (pass->hasVertexProgram())
|
---|
[103] | 74 | {
|
---|
[115] | 75 | mDepthPass->setVertexProgram(pass->getVertexProgramName());
|
---|
[103] | 76 |
|
---|
[115] | 77 | if (mDepthPass->hasVertexProgram())
|
---|
[103] | 78 | {
|
---|
[115] | 79 | const GpuProgramPtr& prg = mDepthPass->getVertexProgram();
|
---|
| 80 | // Load this program if not done already
|
---|
| 81 | if (!prg->isLoaded())
|
---|
| 82 | prg->load();
|
---|
| 83 | // Copy params
|
---|
| 84 | mDepthPass->setVertexProgramParameters(pass->getVertexProgramParameters());
|
---|
[103] | 85 | }
|
---|
| 86 | }
|
---|
[115] | 87 | else if (mDepthPass->hasVertexProgram())
|
---|
[103] | 88 | {
|
---|
[115] | 89 | mDepthPass->setVertexProgram("");
|
---|
[103] | 90 | }
|
---|
| 91 | }
|
---|
[115] | 92 |
|
---|
| 93 | TerrainSceneManager::setPass(usedPass);
|
---|
| 94 |
|
---|
| 95 | return usedPass;
|
---|
| 96 | }
|
---|
| 97 | //-----------------------------------------------------------------------
|
---|
| 98 | void VisibilityTerrainSceneManager::ShowVisualization(Camera *cam)
|
---|
| 99 | {
|
---|
| 100 | // add player camera for visualization purpose
|
---|
| 101 | try {
|
---|
| 102 | Camera *c;
|
---|
| 103 | if ((c = getCamera("PlayerCam")) != NULL)
|
---|
| 104 | {
|
---|
| 105 | getRenderQueue()->addRenderable(c);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | catch(...)
|
---|
| 109 | {
|
---|
| 110 | // ignore
|
---|
| 111 | }
|
---|
[118] | 112 | for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
|
---|
| 113 | {
|
---|
| 114 | getRenderQueue()->addRenderable(*it);
|
---|
| 115 | }
|
---|
[115] | 116 | if (mRenderNodesForViz || mRenderNodesContentForViz)
|
---|
| 117 | {
|
---|
[118] | 118 | // change node material so it is better suited for visualization
|
---|
| 119 | MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
|
---|
| 120 | nodeMat->setAmbient(1, 1, 0);
|
---|
| 121 | nodeMat->setLightingEnabled(true);
|
---|
| 122 | nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
|
---|
| 123 |
|
---|
[115] | 124 | for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
|
---|
| 125 | {
|
---|
[118] | 126 |
|
---|
[115] | 127 | if (mRenderNodesForViz)
|
---|
| 128 | {
|
---|
[118] | 129 | getRenderQueue()->addRenderable(*it);
|
---|
[115] | 130 | // addbounding boxes instead of node itself
|
---|
[118] | 131 | //(*it)->_addBoundingBoxToQueue(getRenderQueue());
|
---|
[115] | 132 | }
|
---|
| 133 | if (mRenderNodesContentForViz)
|
---|
| 134 | {
|
---|
| 135 | (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
[118] | 139 |
|
---|
[103] | 140 | }
|
---|
| 141 | //-----------------------------------------------------------------------
|
---|
[115] | 142 | void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
|
---|
[103] | 143 | {
|
---|
[115] | 144 | //-- show visible scene nodes and octree bounding boxes from last frame
|
---|
| 145 | if (mShowVisualization)
|
---|
| 146 | {
|
---|
| 147 | ShowVisualization(cam);
|
---|
| 148 | }
|
---|
| 149 | else
|
---|
[103] | 150 | {
|
---|
[115] | 151 | mVisible.clear();
|
---|
| 152 | mBoxes.clear();
|
---|
| 153 |
|
---|
| 154 | // if there is no depth pass =>
|
---|
| 155 | // we interleave identification and rendering of objects
|
---|
| 156 | // in _renderVisibibleObjects
|
---|
[103] | 157 |
|
---|
[115] | 158 | // only shadow casters will be rendered in shadow texture pass
|
---|
| 159 | mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
|
---|
[103] | 160 | }
|
---|
[115] | 161 | }
|
---|
| 162 | //-----------------------------------------------------------------------
|
---|
| 163 | void VisibilityTerrainSceneManager::_renderVisibleObjects()
|
---|
| 164 | {
|
---|
| 165 | // visualization: apply standard rendering
|
---|
| 166 | if (mShowVisualization)
|
---|
[87] | 167 | {
|
---|
[101] | 168 | TerrainSceneManager::_renderVisibleObjects();
|
---|
[87] | 169 | return;
|
---|
[115] | 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
[87] | 173 | //-- hierarchical culling
|
---|
| 174 | // the objects of different layers (e.g., background, scene,
|
---|
| 175 | // overlay) must be identified and rendered one after another
|
---|
| 176 |
|
---|
[115] | 177 | bool leaveTransparentsInQueue = mDelayRenderTransparents && !mUseDepthPass;
|
---|
| 178 |
|
---|
| 179 | // possible two cameras (one for culling, one for rendering)
|
---|
| 180 | mHierarchyInterface->InitFrame(mOctree, mCameraInProgress,
|
---|
| 181 | mCullCamera ? getCamera("CullCamera") : NULL,
|
---|
| 182 | leaveTransparentsInQueue);
|
---|
| 183 |
|
---|
| 184 | // call initframe to reset culling manager stats
|
---|
| 185 | mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
|
---|
| 186 |
|
---|
| 187 | mSkipTransparents = false;
|
---|
| 188 |
|
---|
| 189 | //-- render background, in case there is one
|
---|
[59] | 190 | clearSpecialCaseRenderQueues();
|
---|
| 191 | addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
|
---|
| 192 | addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
|
---|
[115] | 193 | setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
|
---|
[59] | 194 |
|
---|
| 195 | SceneManager::_renderVisibleObjects();
|
---|
[100] | 196 |
|
---|
[115] | 197 |
|
---|
[59] | 198 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
[115] | 199 | _deleteRenderedQueueGroups(false);
|
---|
[59] | 200 | #endif
|
---|
| 201 |
|
---|
[115] | 202 | //-- render visible objects (i.e., all but overlay and skies late)
|
---|
[59] | 203 | clearSpecialCaseRenderQueues();
|
---|
[115] | 204 | addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_LATE);
|
---|
[59] | 205 | addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
|
---|
| 206 | setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
|
---|
| 207 |
|
---|
[115] | 208 | // transparents are skipped from hierarchical rendering
|
---|
| 209 | // => they need sorting, thus we render them afterwards
|
---|
| 210 | mSkipTransparents = mDelayRenderTransparents;
|
---|
[100] | 211 |
|
---|
[115] | 212 | // set state for depth pass
|
---|
| 213 | mIsDepthPass = mUseDepthPass;
|
---|
| 214 |
|
---|
[100] | 215 | /**
|
---|
[115] | 216 | * the hierarchical culling algorithm
|
---|
| 217 | * for depth pass: will just find objects and update depth buffer
|
---|
| 218 | * for delayed rendering: will render all but transparents
|
---|
[100] | 219 | **/
|
---|
[115] | 220 |
|
---|
[74] | 221 | mVisibilityManager->ApplyVisibilityCulling();
|
---|
[59] | 222 |
|
---|
[100] | 223 |
|
---|
[115] | 224 | // for depth pass: add visible nodes found with the visibility culling
|
---|
| 225 | if (mUseDepthPass)
|
---|
| 226 | {
|
---|
| 227 | for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
|
---|
| 228 | {
|
---|
| 229 | (*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
|
---|
| 230 | }
|
---|
| 231 | mIsDepthPass = false;
|
---|
| 232 | }
|
---|
| 233 | mSkipTransparents = false;
|
---|
[59] | 234 |
|
---|
[115] | 235 | //-- now we can render all remaining queue objects
|
---|
| 236 | // for depth pass: all
|
---|
| 237 | // for delayed rendering: transparents, overlay
|
---|
[59] | 238 | clearSpecialCaseRenderQueues();
|
---|
[87] | 239 | SceneManager::_renderVisibleObjects();
|
---|
[99] | 240 |
|
---|
| 241 | //WriteLog(); // write out stats
|
---|
[59] | 242 | }
|
---|
| 243 | //-----------------------------------------------------------------------
|
---|
| 244 | void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam)
|
---|
| 245 | {
|
---|
| 246 | mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
|
---|
| 247 | mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
|
---|
[74] | 248 |
|
---|
[59] | 249 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 250 | mHierarchyInterface->SetNumOctreeNodes(mNumOctreeNodes);
|
---|
| 251 | #endif
|
---|
| 252 | TerrainSceneManager::_updateSceneGraph(cam);
|
---|
| 253 | }
|
---|
| 254 | //-----------------------------------------------------------------------
|
---|
| 255 | bool VisibilityTerrainSceneManager::setOption(const String & key, const void * val)
|
---|
| 256 | {
|
---|
[115] | 257 | if (key == "UseDepthPass")
|
---|
[87] | 258 | {
|
---|
[115] | 259 | mUseDepthPass = (*static_cast<const bool *>(val));
|
---|
[87] | 260 | return true;
|
---|
| 261 | }
|
---|
[99] | 262 | if (key == "ShowVisualization")
|
---|
| 263 | {
|
---|
| 264 | mShowVisualization = (*static_cast<const bool *>(val));
|
---|
| 265 | return true;
|
---|
| 266 | }
|
---|
[103] | 267 | if (key == "RenderNodesForViz")
|
---|
| 268 | {
|
---|
| 269 | mRenderNodesForViz = (*static_cast<const bool *>(val));
|
---|
| 270 | return true;
|
---|
| 271 | }
|
---|
[113] | 272 | if (key == "RenderNodesContentForViz")
|
---|
| 273 | {
|
---|
| 274 | mRenderNodesContentForViz = (*static_cast<const bool *>(val));
|
---|
| 275 | return true;
|
---|
| 276 | }
|
---|
[100] | 277 | if (key == "SkyBoxEnabled")
|
---|
| 278 | {
|
---|
| 279 | mSkyBoxEnabled = (*static_cast<const bool *>(val));
|
---|
| 280 | return true;
|
---|
| 281 | }
|
---|
| 282 | if (key == "SkyPlaneEnabled")
|
---|
| 283 | {
|
---|
| 284 | mSkyPlaneEnabled = (*static_cast<const bool *>(val));
|
---|
| 285 | return true;
|
---|
| 286 | }
|
---|
| 287 | if (key == "SkyDomeEnabled")
|
---|
| 288 | {
|
---|
| 289 | mSkyDomeEnabled = (*static_cast<const bool *>(val));
|
---|
| 290 | return true;
|
---|
| 291 | }
|
---|
[112] | 292 | if (key == "VisualizeCulledNodes")
|
---|
| 293 | {
|
---|
| 294 | mVisualizeCulledNodes = (*static_cast<const bool *>(val));
|
---|
| 295 | return true;
|
---|
| 296 | }
|
---|
[115] | 297 | if (key == "DelayRenderTransparents")
|
---|
| 298 | {
|
---|
| 299 | mDelayRenderTransparents = (*static_cast<const bool *>(val));
|
---|
| 300 | return true;
|
---|
| 301 | }
|
---|
[74] | 302 | return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
|
---|
| 303 | setOption(key, val) || TerrainSceneManager::setOption(key, val);
|
---|
[59] | 304 | }
|
---|
| 305 | //-----------------------------------------------------------------------
|
---|
| 306 | bool VisibilityTerrainSceneManager::getOption(const String & key, void *val)
|
---|
| 307 | {
|
---|
[74] | 308 | if (key == "NumHierarchyNodes")
|
---|
| 309 | {
|
---|
| 310 | * static_cast<unsigned int *>(val) = (unsigned int)mNumOctreeNodes;
|
---|
| 311 | return true;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
|
---|
| 315 | getOption(key, val) && TerrainSceneManager::getOption(key, val);
|
---|
[59] | 316 | }
|
---|
| 317 | //-----------------------------------------------------------------------
|
---|
[103] | 318 | bool VisibilityTerrainSceneManager::getOptionValues(const String & key, StringVector &refValueList)
|
---|
[59] | 319 | {
|
---|
[74] | 320 | return TerrainSceneManager::getOptionValues( key, refValueList);
|
---|
[59] | 321 | }
|
---|
| 322 | //-----------------------------------------------------------------------
|
---|
| 323 | bool VisibilityTerrainSceneManager::getOptionKeys(StringVector & refKeys)
|
---|
| 324 | {
|
---|
[74] | 325 | return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
|
---|
| 326 | getOptionKeys(refKeys) || TerrainSceneManager::getOptionKeys(refKeys);
|
---|
[59] | 327 | }
|
---|
| 328 | //-----------------------------------------------------------------------
|
---|
| 329 | void VisibilityTerrainSceneManager::setVisibilityManager(GtpVisibility::VisibilityManager *visManager)
|
---|
| 330 | {
|
---|
| 331 | mVisibilityManager = visManager;
|
---|
| 332 | }
|
---|
| 333 | //-----------------------------------------------------------------------
|
---|
| 334 | GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::getVisibilityManager( void )
|
---|
| 335 | {
|
---|
| 336 | return mVisibilityManager;
|
---|
| 337 | }
|
---|
[93] | 338 | //-----------------------------------------------------------------------
|
---|
| 339 | void VisibilityTerrainSceneManager::WriteLog()
|
---|
| 340 | {
|
---|
| 341 | std::stringstream d;
|
---|
| 342 |
|
---|
| 343 | d << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", "
|
---|
[101] | 344 | << "Hierarchy nodes: " << mNumOctreeNodes << ", "
|
---|
| 345 | << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
|
---|
[93] | 346 | << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
|
---|
| 347 | << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
|
---|
| 348 | << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
|
---|
| 349 | << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << "\n";
|
---|
| 350 |
|
---|
| 351 | LogManager::getSingleton().logMessage(d.str());
|
---|
| 352 | }
|
---|
[114] | 353 | //-----------------------------------------------------------------------
|
---|
| 354 | void VisibilityTerrainSceneManager::renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs,
|
---|
| 355 | bool doLightIteration, const LightList* manualLightList)
|
---|
| 356 | {
|
---|
[115] | 357 | if (!mSkipTransparents)
|
---|
[114] | 358 | {
|
---|
| 359 | OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
[103] | 362 |
|
---|
[59] | 363 | } // namespace Ogre
|
---|