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 | mSceneManager->setPassWrapper(solidBox->getTechnique()->getPass(0));
|
---|
104 | solidBox->SetupBoundingBoxVertices(*box);
|
---|
105 |
|
---|
106 | solidBox->getRenderOperation(ro);
|
---|
107 | ro.srcRenderable = solidBox;
|
---|
108 |
|
---|
109 | mRenderSystem->_render(ro);
|
---|
110 | }
|
---|
111 | //-----------------------------------------------------------------------
|
---|
112 | void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
|
---|
113 | {
|
---|
114 | mCamera = cam;
|
---|
115 | }
|
---|
116 | //-----------------------------------------------------------------------
|
---|
117 | void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
|
---|
118 | {
|
---|
119 | mCullCamera = cullCam;
|
---|
120 | }
|
---|
121 | //-----------------------------------------------------------------------
|
---|
122 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
|
---|
123 | {
|
---|
124 | // create new query if there is no query left
|
---|
125 | if (mCurrentTestIdx == (int)mOcclusionQueries.size())
|
---|
126 | {
|
---|
127 | mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
|
---|
128 | }
|
---|
129 |
|
---|
130 | return mOcclusionQueries[mCurrentTestIdx ++];
|
---|
131 | }
|
---|
132 | //-----------------------------------------------------------------------
|
---|
133 | void PlatformHierarchyInterface::InitTraversal(Camera *cam,
|
---|
134 | Camera *cullCam,
|
---|
135 | int leavePassesInQueue)
|
---|
136 | {
|
---|
137 | GtpVisibility::HierarchyInterface::InitTraversal();
|
---|
138 |
|
---|
139 | mOldNode = NULL;
|
---|
140 | mLeavePassesInQueue = leavePassesInQueue;
|
---|
141 |
|
---|
142 | mCamera = cam;
|
---|
143 | // set culling camera for visualization
|
---|
144 | mCullCamera = cullCam ? cullCam : cam;
|
---|
145 |
|
---|
146 | mCameraPosition = mCullCamera->getDerivedPosition();
|
---|
147 | // create materials for node visualization
|
---|
148 | CreateNodeVizMaterials();
|
---|
149 | }
|
---|
150 | //-----------------------------------------------------------------------
|
---|
151 | void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
|
---|
152 | {
|
---|
153 | mSceneManager = sm;
|
---|
154 | }
|
---|
155 | //-----------------------------------------------------------------------
|
---|
156 | void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
|
---|
157 | {
|
---|
158 | mRenderSystem = rsys;
|
---|
159 | }
|
---|
160 | //-----------------------------------------------------------------------
|
---|
161 | bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
|
---|
162 | bool &intersects)
|
---|
163 | {
|
---|
164 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
165 | return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
|
---|
166 | #else
|
---|
167 | return true;
|
---|
168 | #endif
|
---|
169 | }
|
---|
170 | //-----------------------------------------------------------------------
|
---|
171 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
|
---|
172 | GtpVisibility::HierarchyNode *node,
|
---|
173 | const bool testGeometry)
|
---|
174 | {
|
---|
175 | // get next available test id
|
---|
176 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
177 |
|
---|
178 | //-- the actual query test
|
---|
179 | query->BeginQuery();
|
---|
180 |
|
---|
181 | if (testGeometry && IsLeaf(node))
|
---|
182 | {
|
---|
183 | // if node is a leaf and was visible => will be rendered anyway.
|
---|
184 | // In this case we can also test with the real geometry.
|
---|
185 | RenderNode(node);
|
---|
186 | }
|
---|
187 | else
|
---|
188 | {
|
---|
189 | if (mTestGeometryBounds && IsLeaf(node))
|
---|
190 | {
|
---|
191 | mIsBoundingBoxQuery = true;
|
---|
192 | // we can also test the tighter bounds of the geometry instead of the hierarchy nodes.
|
---|
193 | RenderGeometryBounds(node);
|
---|
194 | mIsBoundingBoxQuery = false;
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | // this information is used e.g., by the scene graph, because the bounding box
|
---|
199 | // must be treated differently to the scene geometry during rendering
|
---|
200 | mIsBoundingBoxQuery = true;
|
---|
201 | RenderBoundingBox(GetBoundingBox(node));
|
---|
202 | mIsBoundingBoxQuery = false;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | query->EndQuery();
|
---|
207 |
|
---|
208 | return query;
|
---|
209 | }
|
---|
210 | //-----------------------------------------------------------------------
|
---|
211 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssuePatchOcclusionQuery(
|
---|
212 | GtpVisibility::Patch *patch)
|
---|
213 | {
|
---|
214 | // get next available test id
|
---|
215 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
216 |
|
---|
217 | //-- the actual query test
|
---|
218 | query->BeginQuery();
|
---|
219 |
|
---|
220 | RenderPatch(patch);
|
---|
221 |
|
---|
222 | query->EndQuery();
|
---|
223 |
|
---|
224 | return query;
|
---|
225 | }
|
---|
226 | //-----------------------------------------------------------------------
|
---|
227 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMeshOcclusionQuery(
|
---|
228 | GtpVisibility::Mesh *mesh)
|
---|
229 | {
|
---|
230 | // get next available test id
|
---|
231 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
232 |
|
---|
233 | ////////
|
---|
234 | //-- the actual query test
|
---|
235 | query->BeginQuery();
|
---|
236 |
|
---|
237 | RenderGeometry(mesh);
|
---|
238 |
|
---|
239 | query->EndQuery();
|
---|
240 |
|
---|
241 | return query;
|
---|
242 | }
|
---|
243 | //-----------------------------------------------------------------------
|
---|
244 | SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
|
---|
245 | {
|
---|
246 | if (!mSolidBoundingBox)
|
---|
247 | mSolidBoundingBox = new SolidBoundingBox;
|
---|
248 |
|
---|
249 | return mSolidBoundingBox;
|
---|
250 | }
|
---|
251 | //-----------------------------------------------------------------------
|
---|
252 | void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
|
---|
253 | {
|
---|
254 | mOnlyShadowCasters = onlyShadowCasters;
|
---|
255 | }
|
---|
256 | //-----------------------------------------------------------------------
|
---|
257 | bool PlatformHierarchyInterface::GetOnlyShadowCasters()
|
---|
258 | {
|
---|
259 | return mOnlyShadowCasters;
|
---|
260 | }
|
---|
261 | //-----------------------------------------------------------------------
|
---|
262 | bool PlatformHierarchyInterface::IsBoundingBoxQuery()
|
---|
263 | {
|
---|
264 | return mIsBoundingBoxQuery;
|
---|
265 | }
|
---|
266 | //-----------------------------------------------------------------------
|
---|
267 | void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
|
---|
268 | {
|
---|
269 | mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
|
---|
270 | }
|
---|
271 | //-----------------------------------------------------------------------
|
---|
272 | void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
|
---|
273 | {
|
---|
274 | mSceneManager->_renderSingleRenderable(patch);
|
---|
275 | }
|
---|
276 | //-----------------------------------------------------------------------
|
---|
277 | SceneManager *PlatformHierarchyInterface::GetSceneManager()
|
---|
278 | {
|
---|
279 | return mSceneManager;
|
---|
280 | }
|
---|
281 | //-----------------------------------------------------------------------
|
---|
282 | RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
|
---|
283 | {
|
---|
284 | return mRenderSystem;
|
---|
285 | }
|
---|
286 | //-----------------------------------------------------------------------
|
---|
287 | void PlatformHierarchyInterface::SetTestGeometryBounds(bool testGeometryBounds)
|
---|
288 | {
|
---|
289 | mTestGeometryBounds = testGeometryBounds;
|
---|
290 | }
|
---|
291 | //-----------------------------------------------------------------------
|
---|
292 | int PlatformHierarchyInterface::GetTestGeometryBounds()
|
---|
293 | {
|
---|
294 | return mTestGeometryBounds;
|
---|
295 | }
|
---|
296 |
|
---|
297 | } // namespace Ogre
|
---|