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 | // HACK! (mySetPass should be setPass)
|
---|
102 | // set no depth write, no color, no lighting material
|
---|
103 | mSceneManager->setPassWrapper(solidBox->getTechnique()->getPass(0));
|
---|
104 | //mSceneManager->_setPass(solidBox->getTechnique()->getPass(0));
|
---|
105 | //SetOcclusionPass();
|
---|
106 |
|
---|
107 | solidBox->SetupBoundingBoxVertices(*box);
|
---|
108 |
|
---|
109 | solidBox->getRenderOperation(ro);
|
---|
110 | ro.srcRenderable = solidBox;
|
---|
111 | //std::stringstream d;
|
---|
112 | //d << "vt2: " << ro.vertexData;
|
---|
113 | //LogManager::getSingleton().logMessage(d.str());
|
---|
114 |
|
---|
115 | mRenderSystem->_render(ro);
|
---|
116 | }
|
---|
117 | //-----------------------------------------------------------------------
|
---|
118 | void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
|
---|
119 | {
|
---|
120 | mCamera = cam;
|
---|
121 | }
|
---|
122 | //-----------------------------------------------------------------------
|
---|
123 | void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
|
---|
124 | {
|
---|
125 | mCullCamera = cullCam;
|
---|
126 | }
|
---|
127 | //-----------------------------------------------------------------------
|
---|
128 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
|
---|
129 | {
|
---|
130 | // create new query if there is no query left
|
---|
131 | if (mCurrentTestIdx == (int)mOcclusionQueries.size())
|
---|
132 | {
|
---|
133 | mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
|
---|
134 |
|
---|
135 | /*std::stringstream d;
|
---|
136 | d << "resizing queries: " << (int)mOcclusionQueries.size() << std::endl;
|
---|
137 | LogManager::getSingleton().logMessage(d.str());*/
|
---|
138 | }
|
---|
139 |
|
---|
140 | return mOcclusionQueries[mCurrentTestIdx ++];
|
---|
141 | }
|
---|
142 | //-----------------------------------------------------------------------
|
---|
143 | void PlatformHierarchyInterface::InitTraversal(Camera *cam,
|
---|
144 | Camera *cullCam,
|
---|
145 | int leavePassesInQueue)
|
---|
146 | {
|
---|
147 | GtpVisibility::HierarchyInterface::InitTraversal();
|
---|
148 |
|
---|
149 | mSavedNode = NULL;
|
---|
150 | mLeavePassesInQueue = leavePassesInQueue;
|
---|
151 |
|
---|
152 | mCamera = cam;
|
---|
153 | // set culling camera for visualization
|
---|
154 | mCullCamera = cullCam ? cullCam : cam;
|
---|
155 |
|
---|
156 | mCameraPosition = mCullCamera->getDerivedPosition();
|
---|
157 | // create materials for node visualization
|
---|
158 | CreateNodeVizMaterials();
|
---|
159 | }
|
---|
160 | //-----------------------------------------------------------------------
|
---|
161 | void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
|
---|
162 | {
|
---|
163 | mSceneManager = sm;
|
---|
164 | }
|
---|
165 | //-----------------------------------------------------------------------
|
---|
166 | void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
|
---|
167 | {
|
---|
168 | mRenderSystem = rsys;
|
---|
169 | }
|
---|
170 | //-----------------------------------------------------------------------
|
---|
171 | bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
|
---|
172 | bool &intersects)
|
---|
173 | {
|
---|
174 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
175 | return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
|
---|
176 | #else
|
---|
177 | return true;
|
---|
178 | #endif
|
---|
179 | }
|
---|
180 | //-----------------------------------------------------------------------
|
---|
181 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
|
---|
182 | GtpVisibility::HierarchyNode *node,
|
---|
183 | const bool wasVisible)
|
---|
184 | {
|
---|
185 | // get next available test id
|
---|
186 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
187 |
|
---|
188 | //-- the actual query test
|
---|
189 | query->BeginQuery();
|
---|
190 |
|
---|
191 | // if node is leaf and was visible => will be rendered anyway.
|
---|
192 | // In this case we can also test with the real geometry.
|
---|
193 | // If camera for culling is different from camera for rendering or only solids
|
---|
194 | // will be rendered => cannot optimize
|
---|
195 | if (mTestGeometryForVisibleLeaves && (mCamera == mCullCamera) && wasVisible && IsLeaf(node))
|
---|
196 | {
|
---|
197 | RenderNode(node);
|
---|
198 | }
|
---|
199 | else
|
---|
200 | {
|
---|
201 | // this information is used e.g., by the scene graph, because the bounding box
|
---|
202 | // must be treated differently to the scene geometry during rendering
|
---|
203 | mIsBoundingBoxQuery = true;
|
---|
204 | RenderBoundingBox(GetBoundingBox(node));
|
---|
205 |
|
---|
206 | mIsBoundingBoxQuery = false;
|
---|
207 | }
|
---|
208 |
|
---|
209 | query->EndQuery();
|
---|
210 |
|
---|
211 | return query;
|
---|
212 | }
|
---|
213 | //-----------------------------------------------------------------------
|
---|
214 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssuePatchOcclusionQuery(
|
---|
215 | GtpVisibility::Patch *patch)
|
---|
216 | {
|
---|
217 | // get next available test id
|
---|
218 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
219 |
|
---|
220 | //-- the actual query test
|
---|
221 | query->BeginQuery();
|
---|
222 |
|
---|
223 | RenderPatch(patch);
|
---|
224 |
|
---|
225 | query->EndQuery();
|
---|
226 |
|
---|
227 | return query;
|
---|
228 | }
|
---|
229 | //-----------------------------------------------------------------------
|
---|
230 | GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMeshOcclusionQuery(
|
---|
231 | GtpVisibility::Mesh *mesh)
|
---|
232 | {
|
---|
233 | // get next available test id
|
---|
234 | GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
|
---|
235 |
|
---|
236 | ////////
|
---|
237 | //-- the actual query test
|
---|
238 |
|
---|
239 | query->BeginQuery();
|
---|
240 | RenderGeometry(mesh);
|
---|
241 | query->EndQuery();
|
---|
242 |
|
---|
243 | return query;
|
---|
244 | }
|
---|
245 | //-----------------------------------------------------------------------
|
---|
246 | /*void PlatformHierarchyInterface::SetOcclusionPass()
|
---|
247 | {
|
---|
248 | // disable vertex and fragment program
|
---|
249 | mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
|
---|
250 | mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
|
---|
251 |
|
---|
252 | // disable lighting
|
---|
253 | mRenderSystem->setLightingEnabled(false);
|
---|
254 |
|
---|
255 | // Disable remaining texture units
|
---|
256 | mRenderSystem->_disableTextureUnitsFrom(0);
|
---|
257 |
|
---|
258 | //--Set up non-texture related material settings
|
---|
259 |
|
---|
260 | // Depth buffer settings
|
---|
261 | mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
|
---|
262 | // Set colour write mode off
|
---|
263 | mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
|
---|
264 | }*/
|
---|
265 | //-----------------------------------------------------------------------
|
---|
266 | SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
|
---|
267 | {
|
---|
268 | if (!mSolidBoundingBox)
|
---|
269 | {
|
---|
270 | mSolidBoundingBox = new SolidBoundingBox;
|
---|
271 | //LogManager::getSingleton().logMessage("solid box created");
|
---|
272 | }
|
---|
273 |
|
---|
274 | return mSolidBoundingBox;
|
---|
275 | }
|
---|
276 | //-----------------------------------------------------------------------
|
---|
277 | void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
|
---|
278 | {
|
---|
279 | mOnlyShadowCasters = onlyShadowCasters;
|
---|
280 | }
|
---|
281 | //-----------------------------------------------------------------------
|
---|
282 | bool PlatformHierarchyInterface::GetOnlyShadowCasters()
|
---|
283 | {
|
---|
284 | return mOnlyShadowCasters;
|
---|
285 | }
|
---|
286 | //-----------------------------------------------------------------------
|
---|
287 | bool PlatformHierarchyInterface::GetTestGeometryForVisibleLeaves()
|
---|
288 | {
|
---|
289 | return mTestGeometryForVisibleLeaves;
|
---|
290 | }
|
---|
291 | //-----------------------------------------------------------------------
|
---|
292 | bool PlatformHierarchyInterface::IsBoundingBoxQuery()
|
---|
293 | {
|
---|
294 | return mIsBoundingBoxQuery;
|
---|
295 | }
|
---|
296 | //-----------------------------------------------------------------------
|
---|
297 | void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
|
---|
298 | {
|
---|
299 | mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
|
---|
300 | }
|
---|
301 | //-----------------------------------------------------------------------
|
---|
302 | void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
|
---|
303 | {
|
---|
304 | mSceneManager->_renderSingleRenderable(patch);
|
---|
305 | }
|
---|
306 | //-----------------------------------------------------------------------
|
---|
307 | SceneManager *PlatformHierarchyInterface::GetSceneManager()
|
---|
308 | {
|
---|
309 | return mSceneManager;
|
---|
310 | }
|
---|
311 | //-----------------------------------------------------------------------
|
---|
312 | RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
|
---|
313 | {
|
---|
314 | return mRenderSystem;
|
---|
315 | }
|
---|
316 |
|
---|
317 | } // namespace Ogre
|
---|