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

Revision 2556, 9.5 KB checked in by mattausch, 17 years ago (diff)

implemented part of chc++

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        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//-----------------------------------------------------------------------
117void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
118{
119        mCamera = cam;
120}
121//-----------------------------------------------------------------------
122void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
123{
124        mCullCamera = cullCam;
125}
126//-----------------------------------------------------------------------
127GtpVisibility::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//-----------------------------------------------------------------------
138void 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//-----------------------------------------------------------------------
158void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
159{
160        mSceneManager = sm;
161}
162//-----------------------------------------------------------------------
163void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
164{
165        mRenderSystem = rsys;
166}
167//-----------------------------------------------------------------------
168bool 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//-----------------------------------------------------------------------
178GtpVisibility::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//-----------------------------------------------------------------------
218GtpVisibility::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//-----------------------------------------------------------------------
234GtpVisibility::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//-----------------------------------------------------------------------
252SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
253{
254        if (!mSolidBoundingBox)
255                mSolidBoundingBox = new SolidBoundingBox;
256
257        return mSolidBoundingBox;
258}
259//-----------------------------------------------------------------------
260void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
261{
262        mOnlyShadowCasters = onlyShadowCasters;
263}
264//-----------------------------------------------------------------------
265bool PlatformHierarchyInterface::GetOnlyShadowCasters()
266{
267        return mOnlyShadowCasters;
268}
269//-----------------------------------------------------------------------
270bool PlatformHierarchyInterface::IsBoundingBoxQuery()
271{
272        return mIsBoundingBoxQuery;
273}
274//-----------------------------------------------------------------------
275void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
276{
277        mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
278}
279//-----------------------------------------------------------------------
280void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
281{
282        mSceneManager->_renderSingleRenderable(patch);
283}
284//-----------------------------------------------------------------------
285SceneManager *PlatformHierarchyInterface::GetSceneManager()
286{
287        return mSceneManager;
288}
289//-----------------------------------------------------------------------
290RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
291{
292        return mRenderSystem;
293}
294//-----------------------------------------------------------------------
295void PlatformHierarchyInterface::SetTestGeometryBounds(bool testGeometryBounds)
296{
297        mTestGeometryBounds = testGeometryBounds;
298}
299//-----------------------------------------------------------------------
300int PlatformHierarchyInterface::GetTestGeometryBounds()
301{
302        return mTestGeometryBounds;
303}
304
305} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.