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

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