source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgrePlatformHierarchyInterface.cpp @ 2561

Revision 2561, 10.3 KB checked in by mattausch, 17 years ago (diff)
RevLine 
[59]1#include <OgreCamera.h>
[94]2#include <OgreLogManager.h>
[130]3#include <OgreSubEntity.h>
4#include <OgreEntity.h>
5#include <OgreMovableObject.h>
[897]6#include <OgreMaterialManager.h>
[92]7#include "OgreSolidBoundingBox.h"
[59]8#include "OgrePlatformHierarchyInterface.h"
9#include "OgrePlatformOcclusionQuery.h"
10
11namespace Ogre {
12
13//-----------------------------------------------------------------------
[726]14PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm,
15                                                                                                           RenderSystem *rsys):
16mSceneManager(sm),
17mRenderSystem(rsys),
18mSolidBoundingBox(NULL),
19mCamera(NULL),
20mCullCamera(NULL),
21mOnlyShadowCasters(false),
22mLeavePassesInQueue(0),
23mIsBoundingBoxQuery(false),
24mCameraPosition(Vector3(0, 0, 0))
[85]25{
[59]26}
27//-----------------------------------------------------------------------
[115]28void PlatformHierarchyInterface::CreateNodeVizMaterials()
[112]29{
30        // material for frustum culled nodes
31        MaterialPtr mat = MaterialManager::getSingleton().getByName("FrustumCulledNodesMaterial");
[113]32       
[112]33        if (mat.isNull())
34        {
35                mat = MaterialManager::getSingleton().create("FrustumCulledNodesMaterial",
36                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
[113]37                mat->createTechnique()->createPass();
38               
[115]39                mat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
[113]40                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
41                //mat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
42                mat->load();
[112]43        }
44
45        // material for query culled nodes
[113]46        mat = MaterialManager::getSingleton().getByName("QueryCulledNodesMaterial");
[112]47
[113]48        if (mat.isNull())
[112]49        {
[113]50                mat = MaterialManager::getSingleton().create("QueryCulledNodesMaterial",
[112]51                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
[113]52                mat->createTechnique()->createPass();
53               
[115]54                mat->getTechnique(0)->getPass(0)->setAmbient(0, 0, 1);
[113]55                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
56                mat->load();
[112]57        }
[115]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        }*/
[112]72}
73//-----------------------------------------------------------------------
[59]74PlatformHierarchyInterface::~PlatformHierarchyInterface()
75{
[897]76        ResetQueries();
[187]77        OGRE_DELETE(mSolidBoundingBox);
[59]78}
79//-----------------------------------------------------------------------
[897]80void PlatformHierarchyInterface::ResetQueries()
[59]81{
[938]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        }
[59]88
[187]89        mCurrentTestIdx = 0;
[903]90    mOcclusionQueries.clear();
[59]91}
92//-----------------------------------------------------------------------
93void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
94{
[86]95        static RenderOperation ro;
96
[92]97        SolidBoundingBox *solidBox = GetSolidBoundingBox();
[86]98       
99        mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
[2555]100        //mSceneManager->useRenderableViewProjModeWrapper(solidBox);
[2455]101       
[115]102        // set no depth write, no color, no lighting material
[2555]103        if (!mTestMode)
104        {
105                mTestMode = true;
106                mSceneManager->setPassWrapper(solidBox->getTechnique()->getPass(0));
107        }
108
[92]109        solidBox->SetupBoundingBoxVertices(*box);
[2184]110
[92]111        solidBox->getRenderOperation(ro);
112        ro.srcRenderable = solidBox;
[2184]113
[92]114        mRenderSystem->_render(ro);
[59]115}
116//-----------------------------------------------------------------------
117void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
118{
119        mCamera = cam;
120}
121//-----------------------------------------------------------------------
[94]122void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
123{
124        mCullCamera = cullCam;
125}
126//-----------------------------------------------------------------------
[59]127GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
128{
[897]129        // create new query if there is no query left
[1595]130        if (mCurrentTestIdx == (int)mOcclusionQueries.size())
[59]131        {
132                mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
133        }
134       
135        return mOcclusionQueries[mCurrentTestIdx ++];
136}
137//-----------------------------------------------------------------------
[2306]138void PlatformHierarchyInterface::InitTraversal(Camera *cam,
139                                                                                           Camera *cullCam,
[158]140                                                                                           int leavePassesInQueue)
[59]141{
[155]142        GtpVisibility::HierarchyInterface::InitTraversal();
[121]143       
[2455]144        mOldNode = NULL;
[139]145        mLeavePassesInQueue = leavePassesInQueue;
[94]146
[121]147        mCamera = cam;
[925]148        // set culling camera for visualization
[121]149        mCullCamera = cullCam ? cullCam : cam;
[113]150
[726]151        mCameraPosition = mCullCamera->getDerivedPosition();
[121]152        // create materials for node visualization
[115]153        CreateNodeVizMaterials();
[2555]154
155        mTestMode = false;
[59]156}
157//-----------------------------------------------------------------------
158void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
159{
160        mSceneManager = sm;
161}
162//-----------------------------------------------------------------------
163void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
164{
165        mRenderSystem = rsys;
166}
167//-----------------------------------------------------------------------
[85]168bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
169                                                                                                         bool &intersects)
[59]170{
171#ifdef GTP_VISIBILITY_MODIFIED_OGRE
[94]172        return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
[59]173#else
174        return true;
175#endif
176}
177//-----------------------------------------------------------------------
[175]178GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
[2318]179                                                                              GtpVisibility::HierarchyNode *node,
[2455]180                                                                                                                                                  const bool testGeometry)
[2278]181{
[59]182        // get next available test id
183        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
184
185        //-- the actual query test
186        query->BeginQuery();
[122]187       
[2455]188        if (testGeometry && IsLeaf(node))
[135]189        {
[2455]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.
[135]192                RenderNode(node);
193        }
[2455]194        else
[89]195        {
[2455]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                }
[89]211        }
[59]212
213        query->EndQuery();
214       
215        return query;
216}
[130]217//-----------------------------------------------------------------------
[175]218GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssuePatchOcclusionQuery(
[174]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//-----------------------------------------------------------------------
[175]234GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMeshOcclusionQuery(
[2254]235                                                                                                                GtpVisibility::Mesh *mesh)
[130]236{
237        // get next available test id
238        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
[1486]239       
240        ////////
241        //-- the actual query test
[2556]242
[130]243        query->BeginQuery();
[2455]244
[130]245        RenderGeometry(mesh);
[2455]246       
[130]247        query->EndQuery();
248
249        return query;
250}
[135]251//-----------------------------------------------------------------------
[92]252SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
[85]253{
[100]254        if (!mSolidBoundingBox)
[92]255                mSolidBoundingBox = new SolidBoundingBox;
[59]256
[92]257        return mSolidBoundingBox;
[85]258}
[103]259//-----------------------------------------------------------------------
260void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
261{
262        mOnlyShadowCasters = onlyShadowCasters;
263}
[111]264//-----------------------------------------------------------------------
265bool PlatformHierarchyInterface::GetOnlyShadowCasters()
266{
267        return mOnlyShadowCasters;
268}
[120]269//-----------------------------------------------------------------------
[121]270bool PlatformHierarchyInterface::IsBoundingBoxQuery()
271{
272        return mIsBoundingBoxQuery;
273}
[130]274//-----------------------------------------------------------------------
275void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
276{
[139]277        mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
[130]278}
279//-----------------------------------------------------------------------
[174]280void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
281{
[175]282        mSceneManager->_renderSingleRenderable(patch);
[174]283}
284//-----------------------------------------------------------------------
[130]285SceneManager *PlatformHierarchyInterface::GetSceneManager()
286{
287        return mSceneManager;
288}
289//-----------------------------------------------------------------------
290RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
291{
292        return mRenderSystem;
293}
[2455]294//-----------------------------------------------------------------------
295void PlatformHierarchyInterface::SetTestGeometryBounds(bool testGeometryBounds)
296{
297        mTestGeometryBounds = testGeometryBounds;
298}
299//-----------------------------------------------------------------------
300int PlatformHierarchyInterface::GetTestGeometryBounds()
301{
302        return mTestGeometryBounds;
303}
[2558]304//-----------------------------------------------------------------------
305GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMultiQuery(
306                                                                                        const GtpVisibility::HierarchyNodeContainer &nodes)
307{
308        // get next available test id
309        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
[2561]310        GtpVisibility::HierarchyNodeContainer::const_iterator nit, nit_end = nodes.end();
[174]311
[2558]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
[135]329} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.