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