1 | #include "OgreVisibilityTerrainSceneManager.h"
|
---|
2 | #include "OgreOctreeHierarchyInterface.h"
|
---|
3 | #include "OgreVisibilityOptionsManager.h"
|
---|
4 | #include <OgreMath.h>
|
---|
5 | #include <OgreIteratorWrappers.h>
|
---|
6 | #include <OgreRenderSystem.h>
|
---|
7 | #include <OgreCamera.h>
|
---|
8 | #include <OgreLogManager.h>
|
---|
9 | #include <OgreStringConverter.h>
|
---|
10 |
|
---|
11 | #include <windows.h>
|
---|
12 |
|
---|
13 | namespace Ogre {
|
---|
14 |
|
---|
15 | //-----------------------------------------------------------------------
|
---|
16 | VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
|
---|
17 | GtpVisibility::VisibilityManager *visManager)
|
---|
18 | : mVisibilityManager(visManager),
|
---|
19 | mUseVisibilityCulling(true),
|
---|
20 | mShowVisualization(false),
|
---|
21 | mRenderNodesForViz(false),
|
---|
22 | mRenderNodesContentForViz(false),
|
---|
23 | mVisualizeCulledNodes(false),
|
---|
24 | mRenderTransparentObjects(false)
|
---|
25 | {
|
---|
26 | mHierarchyInterface =
|
---|
27 | new OctreeHierarchyInterface(this, mDestRenderSystem);
|
---|
28 |
|
---|
29 | //mDisplayNodes = true;
|
---|
30 | //mShowBoundingBoxes = true;
|
---|
31 |
|
---|
32 | // TODO: set maxdepth to reasonable value
|
---|
33 | mMaxDepth = 50;
|
---|
34 | }
|
---|
35 | //-----------------------------------------------------------------------
|
---|
36 | VisibilityTerrainSceneManager::~VisibilityTerrainSceneManager()
|
---|
37 | {
|
---|
38 | delete mHierarchyInterface;
|
---|
39 | }
|
---|
40 | //-----------------------------------------------------------------------
|
---|
41 | void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
|
---|
42 | {
|
---|
43 | // only shadow casters will be rendered in shadow texture pass
|
---|
44 | mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
|
---|
45 |
|
---|
46 | // does nothing if hierarchical culling is used =>
|
---|
47 | // we interleave identification and rendering of objects
|
---|
48 | // in _renderVisibibleObjects
|
---|
49 | if (!mUseVisibilityCulling)
|
---|
50 | {
|
---|
51 | OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | //-- show visibile scene nodes and octree bounding boxes from last frame
|
---|
56 | if (mShowVisualization)
|
---|
57 | {
|
---|
58 | // add player camera for visualization purpose
|
---|
59 | try {
|
---|
60 | Camera *c;
|
---|
61 | if ((c = getCamera("PlayerCam")) != NULL)
|
---|
62 | {
|
---|
63 | getRenderQueue()->addRenderable(c);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | catch(...)
|
---|
67 | {
|
---|
68 | // ignore
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (mRenderNodesForViz || mRenderNodesContentForViz)
|
---|
72 | {
|
---|
73 | // change node material so it is better suited for visualization
|
---|
74 | /*MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
|
---|
75 | nodeMat->setAmbient(1, 1, 0);
|
---|
76 | nodeMat->setLightingEnabled(true);
|
---|
77 | nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();*/
|
---|
78 |
|
---|
79 | for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
|
---|
80 | {
|
---|
81 | if (mRenderNodesForViz)
|
---|
82 | {
|
---|
83 | getRenderQueue()->addRenderable(*it);
|
---|
84 | }
|
---|
85 | if (mRenderNodesContentForViz)
|
---|
86 | {
|
---|
87 | (*it)->_addToRenderQueue(cam, getRenderQueue(), false);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
|
---|
92 | {
|
---|
93 | getRenderQueue()->addRenderable(*it);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | mVisible.clear();
|
---|
98 | mBoxes.clear();
|
---|
99 | }
|
---|
100 | //-----------------------------------------------------------------------
|
---|
101 | void VisibilityTerrainSceneManager::_renderVisibleObjects()
|
---|
102 | {
|
---|
103 | if (!mShowVisualization)
|
---|
104 | {
|
---|
105 | // two cameras (one for culling, one for rendering)
|
---|
106 | mHierarchyInterface->InitFrame(mOctree, mCameraInProgress,
|
---|
107 | mCullCamera ? getCamera("CullCamera") : NULL);
|
---|
108 |
|
---|
109 | // call initframe to reset culling manager stats
|
---|
110 | mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
|
---|
111 | }
|
---|
112 |
|
---|
113 | // standard terrain scenemanager rendering without hierarchical culling
|
---|
114 | if (!mUseVisibilityCulling || mShowVisualization)
|
---|
115 | {
|
---|
116 | TerrainSceneManager::_renderVisibleObjects();
|
---|
117 |
|
---|
118 | return;
|
---|
119 | }
|
---|
120 |
|
---|
121 | //-- hierarchical culling
|
---|
122 | // the objects of different layers (e.g., background, scene,
|
---|
123 | // overlay) must be identified and rendered one after another
|
---|
124 |
|
---|
125 | //-- render background
|
---|
126 | clearSpecialCaseRenderQueues();
|
---|
127 | addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
|
---|
128 | addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
|
---|
129 |
|
---|
130 | setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
|
---|
131 | SceneManager::_renderVisibleObjects();
|
---|
132 |
|
---|
133 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
134 | _deleteRenderedQueueGroups();
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | //-- render visible objects (i.e., all but overlay)
|
---|
138 | clearSpecialCaseRenderQueues();
|
---|
139 | addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
|
---|
140 | setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * the hierarchical culling algorithm
|
---|
145 | **/
|
---|
146 | mVisibilityManager->ApplyVisibilityCulling();
|
---|
147 |
|
---|
148 |
|
---|
149 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
150 | _deleteRenderedQueueGroups();
|
---|
151 | #endif
|
---|
152 |
|
---|
153 | //-- render overlay
|
---|
154 | clearSpecialCaseRenderQueues();
|
---|
155 | SceneManager::_renderVisibleObjects();
|
---|
156 |
|
---|
157 | //WriteLog(); // write out stats
|
---|
158 | }
|
---|
159 | //-----------------------------------------------------------------------
|
---|
160 | void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam)
|
---|
161 | {
|
---|
162 | mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
|
---|
163 | mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
|
---|
164 |
|
---|
165 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
166 | mHierarchyInterface->SetNumOctreeNodes(mNumOctreeNodes);
|
---|
167 | #endif
|
---|
168 | TerrainSceneManager::_updateSceneGraph(cam);
|
---|
169 | }
|
---|
170 | //-----------------------------------------------------------------------
|
---|
171 | bool VisibilityTerrainSceneManager::setOption(const String & key, const void * val)
|
---|
172 | {
|
---|
173 | if (key == "UseVisibilityCulling")
|
---|
174 | {
|
---|
175 | mUseVisibilityCulling = (*static_cast<const bool *>(val));
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 | if (key == "ShowVisualization")
|
---|
179 | {
|
---|
180 | mShowVisualization = (*static_cast<const bool *>(val));
|
---|
181 | return true;
|
---|
182 | }
|
---|
183 | if (key == "RenderNodesForViz")
|
---|
184 | {
|
---|
185 | mRenderNodesForViz = (*static_cast<const bool *>(val));
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 | if (key == "RenderNodesContentForViz")
|
---|
189 | {
|
---|
190 | mRenderNodesContentForViz = (*static_cast<const bool *>(val));
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 | if (key == "SkyBoxEnabled")
|
---|
194 | {
|
---|
195 | mSkyBoxEnabled = (*static_cast<const bool *>(val));
|
---|
196 | return true;
|
---|
197 | }
|
---|
198 | if (key == "SkyPlaneEnabled")
|
---|
199 | {
|
---|
200 | mSkyPlaneEnabled = (*static_cast<const bool *>(val));
|
---|
201 | return true;
|
---|
202 | }
|
---|
203 | if (key == "SkyDomeEnabled")
|
---|
204 | {
|
---|
205 | mSkyDomeEnabled = (*static_cast<const bool *>(val));
|
---|
206 | return true;
|
---|
207 | }
|
---|
208 | if (key == "VisualizeCulledNodes")
|
---|
209 | {
|
---|
210 | mVisualizeCulledNodes = (*static_cast<const bool *>(val));
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 | return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
|
---|
214 | setOption(key, val) || TerrainSceneManager::setOption(key, val);
|
---|
215 | }
|
---|
216 | //-----------------------------------------------------------------------
|
---|
217 | bool VisibilityTerrainSceneManager::getOption(const String & key, void *val)
|
---|
218 | {
|
---|
219 | if (key == "NumHierarchyNodes")
|
---|
220 | {
|
---|
221 | * static_cast<unsigned int *>(val) = (unsigned int)mNumOctreeNodes;
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 |
|
---|
225 | return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
|
---|
226 | getOption(key, val) && TerrainSceneManager::getOption(key, val);
|
---|
227 | }
|
---|
228 | //-----------------------------------------------------------------------
|
---|
229 | bool VisibilityTerrainSceneManager::getOptionValues(const String & key, StringVector &refValueList)
|
---|
230 | {
|
---|
231 | return TerrainSceneManager::getOptionValues( key, refValueList);
|
---|
232 | }
|
---|
233 | //-----------------------------------------------------------------------
|
---|
234 | bool VisibilityTerrainSceneManager::getOptionKeys(StringVector & refKeys)
|
---|
235 | {
|
---|
236 | return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
|
---|
237 | getOptionKeys(refKeys) || TerrainSceneManager::getOptionKeys(refKeys);
|
---|
238 | }
|
---|
239 | //-----------------------------------------------------------------------
|
---|
240 | void VisibilityTerrainSceneManager::setVisibilityManager(GtpVisibility::VisibilityManager *visManager)
|
---|
241 | {
|
---|
242 | mVisibilityManager = visManager;
|
---|
243 | }
|
---|
244 | //-----------------------------------------------------------------------
|
---|
245 | GtpVisibility::VisibilityManager *VisibilityTerrainSceneManager::getVisibilityManager( void )
|
---|
246 | {
|
---|
247 | return mVisibilityManager;
|
---|
248 | }
|
---|
249 | //-----------------------------------------------------------------------
|
---|
250 | void VisibilityTerrainSceneManager::WriteLog()
|
---|
251 | {
|
---|
252 | std::stringstream d;
|
---|
253 |
|
---|
254 | d << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", "
|
---|
255 | << "Hierarchy nodes: " << mNumOctreeNodes << ", "
|
---|
256 | << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
|
---|
257 | << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
|
---|
258 | << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
|
---|
259 | << "Frustum culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumFrustumCulledNodes() << ", "
|
---|
260 | << "Queries issued: " << mVisibilityManager->GetCullingManager()->GetNumQueriesIssued() << "\n";
|
---|
261 |
|
---|
262 | LogManager::getSingleton().logMessage(d.str());
|
---|
263 | }
|
---|
264 | //-----------------------------------------------------------------------
|
---|
265 | void VisibilityTerrainSceneManager::renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs,
|
---|
266 | bool doLightIteration, const LightList* manualLightList)
|
---|
267 | {
|
---|
268 | if (mRenderTransparentObjects)
|
---|
269 | {
|
---|
270 | OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | } // namespace Ogre
|
---|