1 | #include <OgreCamera.h>
|
---|
2 | #include <OgreLogManager.h>
|
---|
3 | #include <OgreSubEntity.h>
|
---|
4 | #include <OgreEntity.h>
|
---|
5 | #include <OgreMovableObject.h>
|
---|
6 | #include <OgreMaterialManager.h>
|
---|
7 | #include "OgreSolidBoundingBox.h"
|
---|
8 | #include "OgrePlatformHierarchyInterface.h"
|
---|
9 | #include "OgrePlatformOcclusionQuery.h"
|
---|
10 |
|
---|
11 | namespace Ogre {
|
---|
12 |
|
---|
13 | //-----------------------------------------------------------------------
|
---|
14 | PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm,
|
---|
15 | RenderSystem *rsys):
|
---|
16 | mSceneManager(sm),
|
---|
17 | mRenderSystem(rsys),
|
---|
18 | mSolidBoundingBox(NULL),
|
---|
19 | mCamera(NULL),
|
---|
20 | mCullCamera(NULL),
|
---|
21 | mOnlyShadowCasters(false),
|
---|
22 | mLeavePassesInQueue(0),
|
---|
23 | mIsBoundingBoxQuery(false),
|
---|
24 | mCameraPosition(Vector3(0, 0, 0))
|
---|
25 | {
|
---|
26 | }
|
---|
27 | //-----------------------------------------------------------------------
|
---|
28 | void PlatformHierarchyInterface::CreateNodeVizMaterials()
|
---|
29 | {
|
---|
30 | // material for frustum culled nodes
|
---|
31 | MaterialPtr mat = MaterialManager::getSingleton().getByName("FrustumCulledNodesMaterial");
|
---|
32 |
|
---|
33 | if (mat.isNull())
|
---|
34 | {
|
---|
35 | mat = MaterialManager::getSingleton().create("FrustumCulledNodesMaterial",
|
---|
36 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
37 | mat->createTechnique()->createPass();
|
---|
38 |
|
---|
39 | mat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
|
---|
40 | mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
|
---|
41 | //mat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
|
---|
42 | mat->load();
|
---|
43 | }
|
---|
44 |
|
---|
45 | // material for query culled nodes
|
---|
46 | mat = MaterialManager::getSingleton().getByName("QueryCulledNodesMaterial");
|
---|
47 |
|
---|
48 | if (mat.isNull())
|
---|
49 | {
|
---|
50 | mat = MaterialManager::getSingleton().create("QueryCulledNodesMaterial",
|
---|
51 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
52 | mat->createTechnique()->createPass();
|
---|
53 |
|
---|
54 | mat->getTechnique(0)->getPass(0)->setAmbient(0, 0, 1);
|
---|
55 | mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
|
---|
56 | mat->load();
|
---|
57 | }
|
---|
58 |
|
---|
59 | // material for scene nodes
|
---|
60 | /*mat = MaterialManager::getSingleton().getByName("SceneNodesMaterial");
|
---|
61 |
|
---|
62 | if (mat.isNull())
|
---|
63 | {
|
---|
64 | mat = MaterialManager::getSingleton().create("SceneNodesMaterial",
|
---|
65 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
66 | mat->createTechnique()->createPass();
|
---|
67 |
|
---|
68 | mat->getTechnique(0)->getPass(0)->setAmbient(1, 1, 0);
|
---|
69 | mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
|
---|
70 | mat->load();
|
---|
71 | }*/
|
---|
72 | }
|
---|
73 | //-----------------------------------------------------------------------
|
---|
74 | PlatformHierarchyInterface::~PlatformHierarchyInterface()
|
---|
75 | {
|
---|
76 | ResetQueries();
|
---|
77 | OGRE_DELETE(mSolidBoundingBox);
|
---|
78 | }
|
---|
79 | //-----------------------------------------------------------------------
|
---|
80 | void PlatformHierarchyInterface::ResetQueries()
|
---|
81 | {
|
---|
82 | OcclusionQueryContainer::iterator it, it_end = mOcclusionQueries.end();
|
---|
83 | for (it = mOcclusionQueries.begin(); it != it_end; ++ it)
|
---|
84 | {
|
---|
85 | PlatformOcclusionQuery *query = *it;
|
---|
86 | OGRE_DELETE(query);
|
---|
87 | }
|
---|
88 |
|
---|
89 | mCurrentTestIdx = 0;
|
---|
90 | mOcclusionQueries.clear();
|
---|
91 | }
|
---|
92 | //-----------------------------------------------------------------------
|
---|
93 | void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
|
---|
94 | {
|
---|
95 | static RenderOperation ro;
|
---|
96 |
|
---|
97 | SolidBoundingBox *solidBox = GetSolidBoundingBox();
|
---|
98 |
|
---|
99 | mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
|
---|
100 | //mSceneManager->useRenderableViewProjModeWrapper(solidBox);
|
---|
101 |
|
---|
102 | // set no depth write, no color, no lighting material
|
---|
103 | if (!mTestMode)
|
---|
104 | {
|
---|
105 | mTestMode = true;
|
---|
106 | mSceneManager->setPassWrapper(solidBox->getTechnique()->getPass(0));
|
---|
107 | }
|
---|
108 |
|
---|
109 | solidBox->SetupBoundingBoxVertices(*box);
|
---|
110 |
|
---|
111 | solidBox->getRenderOperation(ro);
|
---|
112 | ro.srcRenderable = solidBox;
|
---|
113 |
|
---|
114 | mRenderSystem->_render(ro);
|
---|
115 | }
|
---|
116 | //-----------------------------------------------------------------------
|
---|
117 | void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
|
---|
118 | {
|
---|
119 | mCamera = cam;
|
---|
120 | }
|
---|
121 | //-----------------------------------------------------------------------
|
---|
122 | void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
|
---|
123 | {
|
---|
124 | mCullCamera = cullCam;
|
---|
125 | }
|
---|
126 | //-----------------------------------------------------------------------
|
---|
127 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
|
---|
128 | {
|
---|
129 | // create new query if there is no query left
|
---|
130 | if (mCurrentTestIdx == (int)mOcclusionQueries.size())
|
---|
131 | {
|
---|
132 | mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
|
---|
133 | }
|
---|
134 |
|
---|
135 | return mOcclusionQueries[mCurrentTestIdx ++];
|
---|
136 | }
|
---|
137 | //-----------------------------------------------------------------------
|
---|
138 | void PlatformHierarchyInterface::InitTraversal(Camera *cam,
|
---|
139 | Camera *cullCam,
|
---|
140 | int leavePassesInQueue)
|
---|
141 | {
|
---|
142 | GtpVisibility::HierarchyInterface::InitTraversal();
|
---|
143 |
|
---|
144 | mOldNode = NULL;
|
---|
145 | mLeavePassesInQueue = leavePassesInQueue;
|
---|
146 |
|
---|
147 | mCamera = cam;
|
---|
148 | // set culling camera for visualization
|
---|
149 | mCullCamera = cullCam ? cullCam : cam;
|
---|
150 |
|
---|
151 | mCameraPosition = mCullCamera->getDerivedPosition();
|
---|
152 | // create materials for node visualization
|
---|
153 | CreateNodeVizMaterials();
|
---|
154 |
|
---|
155 | mTestMode = false;
|
---|
156 | }
|
---|
157 | //-----------------------------------------------------------------------
|
---|
158 | void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
|
---|
159 | {
|
---|
160 | mSceneManager = sm;
|
---|
161 | }
|
---|
162 | //-----------------------------------------------------------------------
|
---|
163 | void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
|
---|
164 | {
|
---|
165 | mRenderSystem = rsys;
|
---|
166 | }
|
---|
167 | //-----------------------------------------------------------------------
|
---|
168 | bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
|
---|
169 | bool &intersects)
|
---|
170 | {
|
---|
171 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
172 | return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
|
---|
173 | #else
|
---|
174 | return true;
|
---|
175 | #endif
|
---|
176 | }
|
---|
177 | //-----------------------------------------------------------------------
|
---|
178 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
|
---|
179 | GtpVisibility::HierarchyNode *node,
|
---|
180 | const bool testGeometry)
|
---|
181 | {
|
---|
182 | // get next available test id
|
---|
183 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
184 |
|
---|
185 | //-- the actual query test
|
---|
186 | query->BeginQuery();
|
---|
187 |
|
---|
188 | if (testGeometry && IsLeaf(node))
|
---|
189 | {
|
---|
190 | // if node is a leaf and was visible => will be rendered anyway.
|
---|
191 | // In this case we can also test with the real geometry.
|
---|
192 | RenderNode(node);
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | if (mTestGeometryBounds && IsLeaf(node))
|
---|
197 | {
|
---|
198 | mIsBoundingBoxQuery = true;
|
---|
199 | // we can also test the tighter bounds of the geometry instead of the hierarchy nodes.
|
---|
200 | RenderGeometryBounds(node);
|
---|
201 | mIsBoundingBoxQuery = false;
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 | // this information is used e.g., by the scene graph, because the bounding box
|
---|
206 | // must be treated differently to the scene geometry during rendering
|
---|
207 | mIsBoundingBoxQuery = true;
|
---|
208 | RenderBoundingBox(GetBoundingBox(node));
|
---|
209 | mIsBoundingBoxQuery = false;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | query->EndQuery();
|
---|
214 |
|
---|
215 | return query;
|
---|
216 | }
|
---|
217 | //-----------------------------------------------------------------------
|
---|
218 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssuePatchOcclusionQuery(
|
---|
219 | GtpVisibility::Patch *patch)
|
---|
220 | {
|
---|
221 | // get next available test id
|
---|
222 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
223 |
|
---|
224 | //-- the actual query test
|
---|
225 | query->BeginQuery();
|
---|
226 |
|
---|
227 | RenderPatch(patch);
|
---|
228 |
|
---|
229 | query->EndQuery();
|
---|
230 |
|
---|
231 | return query;
|
---|
232 | }
|
---|
233 | //-----------------------------------------------------------------------
|
---|
234 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMeshOcclusionQuery(
|
---|
235 | GtpVisibility::Mesh *mesh)
|
---|
236 | {
|
---|
237 | // get next available test id
|
---|
238 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
239 |
|
---|
240 | ////////
|
---|
241 | //-- the actual query test
|
---|
242 |
|
---|
243 | query->BeginQuery();
|
---|
244 |
|
---|
245 | RenderGeometry(mesh);
|
---|
246 |
|
---|
247 | query->EndQuery();
|
---|
248 |
|
---|
249 | return query;
|
---|
250 | }
|
---|
251 | //-----------------------------------------------------------------------
|
---|
252 | SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
|
---|
253 | {
|
---|
254 | if (!mSolidBoundingBox)
|
---|
255 | mSolidBoundingBox = new SolidBoundingBox;
|
---|
256 |
|
---|
257 | return mSolidBoundingBox;
|
---|
258 | }
|
---|
259 | //-----------------------------------------------------------------------
|
---|
260 | void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
|
---|
261 | {
|
---|
262 | mOnlyShadowCasters = onlyShadowCasters;
|
---|
263 | }
|
---|
264 | //-----------------------------------------------------------------------
|
---|
265 | bool PlatformHierarchyInterface::GetOnlyShadowCasters()
|
---|
266 | {
|
---|
267 | return mOnlyShadowCasters;
|
---|
268 | }
|
---|
269 | //-----------------------------------------------------------------------
|
---|
270 | bool PlatformHierarchyInterface::IsBoundingBoxQuery()
|
---|
271 | {
|
---|
272 | return mIsBoundingBoxQuery;
|
---|
273 | }
|
---|
274 | //-----------------------------------------------------------------------
|
---|
275 | void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
|
---|
276 | {
|
---|
277 | mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
|
---|
278 | }
|
---|
279 | //-----------------------------------------------------------------------
|
---|
280 | void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
|
---|
281 | {
|
---|
282 | mSceneManager->_renderSingleRenderable(patch);
|
---|
283 | }
|
---|
284 | //-----------------------------------------------------------------------
|
---|
285 | SceneManager *PlatformHierarchyInterface::GetSceneManager()
|
---|
286 | {
|
---|
287 | return mSceneManager;
|
---|
288 | }
|
---|
289 | //-----------------------------------------------------------------------
|
---|
290 | RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
|
---|
291 | {
|
---|
292 | return mRenderSystem;
|
---|
293 | }
|
---|
294 | //-----------------------------------------------------------------------
|
---|
295 | void PlatformHierarchyInterface::SetTestGeometryBounds(bool testGeometryBounds)
|
---|
296 | {
|
---|
297 | mTestGeometryBounds = testGeometryBounds;
|
---|
298 | }
|
---|
299 | //-----------------------------------------------------------------------
|
---|
300 | int PlatformHierarchyInterface::GetTestGeometryBounds()
|
---|
301 | {
|
---|
302 | return mTestGeometryBounds;
|
---|
303 | }
|
---|
304 | //-----------------------------------------------------------------------
|
---|
305 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMultiQuery(
|
---|
306 | const GtpVisibility::HierarchyNodeContainer &nodes)
|
---|
307 | {
|
---|
308 | // get next available test id
|
---|
309 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
310 | GtpVisibility::HierarchyNodeContainer::const_iterator nit, nit_end = nodes.end();
|
---|
311 |
|
---|
312 | //-- the actual query test
|
---|
313 | query->BeginQuery();
|
---|
314 | mIsBoundingBoxQuery = true;
|
---|
315 |
|
---|
316 | for (nit = nodes.begin(); nit != nodes.end(); ++ nit)
|
---|
317 | {
|
---|
318 | // this information is used e.g., by the scene graph, because the bounding box
|
---|
319 | // must be treated differently to the scene geometry during rendering
|
---|
320 | RenderBoundingBox(GetBoundingBox(*nit));
|
---|
321 | }
|
---|
322 |
|
---|
323 | mIsBoundingBoxQuery = false;
|
---|
324 | query->EndQuery();
|
---|
325 |
|
---|
326 | return query;
|
---|
327 | }
|
---|
328 |
|
---|
329 | } // namespace Ogre
|
---|