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

Revision 726, 9.6 KB checked in by mattausch, 18 years ago (diff)

improved performance of TerrainSceneManager?
revisit octreescenemanager

Line 
1#include <OgreCamera.h>
2#include <OgreLogManager.h>
3#include <OgreSubEntity.h>
4#include <OgreEntity.h>
5#include <OgreMovableObject.h>
6#include "OgreSolidBoundingBox.h"
7#include "OgrePlatformHierarchyInterface.h"
8#include "OgrePlatformOcclusionQuery.h"
9
10namespace Ogre {
11
12//-----------------------------------------------------------------------
13PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm,
14                                                                                                           RenderSystem *rsys):
15mSceneManager(sm),
16mRenderSystem(rsys),
17mSolidBoundingBox(NULL),
18mCamera(NULL),
19mCullCamera(NULL),
20mOnlyShadowCasters(false),
21mLeavePassesInQueue(0),
22mIsBoundingBoxQuery(false),
23mCameraPosition(Vector3(0, 0, 0))
24{
25}
26//-----------------------------------------------------------------------
27void PlatformHierarchyInterface::CreateNodeVizMaterials()
28{
29        // material for frustum culled nodes
30        MaterialPtr mat = MaterialManager::getSingleton().getByName("FrustumCulledNodesMaterial");
31       
32        if (mat.isNull())
33        {
34                mat = MaterialManager::getSingleton().create("FrustumCulledNodesMaterial",
35                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
36                mat->createTechnique()->createPass();
37               
38                mat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
39                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
40                //mat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
41                mat->load();
42        }
43
44        // material for query culled nodes
45        mat = MaterialManager::getSingleton().getByName("QueryCulledNodesMaterial");
46
47        if (mat.isNull())
48        {
49                mat = MaterialManager::getSingleton().create("QueryCulledNodesMaterial",
50                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
51                mat->createTechnique()->createPass();
52               
53                mat->getTechnique(0)->getPass(0)->setAmbient(0, 0, 1);
54                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
55                mat->load();
56        }
57
58        // material for scene nodes
59        /*mat = MaterialManager::getSingleton().getByName("SceneNodesMaterial");
60
61        if (mat.isNull())
62        {
63                mat = MaterialManager::getSingleton().create("SceneNodesMaterial",
64                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
65                mat->createTechnique()->createPass();
66               
67                mat->getTechnique(0)->getPass(0)->setAmbient(1, 1, 0);
68                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
69                mat->load();
70        }*/
71}
72//-----------------------------------------------------------------------
73PlatformHierarchyInterface::~PlatformHierarchyInterface()
74{
75        DeleteQueries();
76
77        OGRE_DELETE(mSolidBoundingBox);
78}
79//-----------------------------------------------------------------------
80void PlatformHierarchyInterface::DeleteQueries()
81{
82        for (int i = 0; i < (int)mOcclusionQueries.size(); ++ i)
83                OGRE_DELETE(mOcclusionQueries[i]);
84
85        mCurrentTestIdx = 0;
86    mOcclusionQueries.clear();
87}
88//-----------------------------------------------------------------------
89void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
90{
91        static RenderOperation ro;
92
93        SolidBoundingBox *solidBox = GetSolidBoundingBox();
94       
95        mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
96        mSceneManager->useRenderableViewProjModeWrapper(solidBox);
97       
98        // set no depth write, no color, no lighting material
99        mSceneManager->setPassWrapper(solidBox->getTechnique()->getPass(0)); // HACK! (mySetPass should be setPass)
100        //SetOcclusionPass();
101
102        solidBox->SetupBoundingBoxVertices(*box);
103                               
104        solidBox->getRenderOperation(ro);
105        ro.srcRenderable = solidBox;
106        mRenderSystem->_render(ro);
107}
108//-----------------------------------------------------------------------
109void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
110{
111        mCamera = cam;
112}
113//-----------------------------------------------------------------------
114void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
115{
116        mCullCamera = cullCam;
117}
118//-----------------------------------------------------------------------
119GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
120{
121        if (mCurrentTestIdx == mOcclusionQueries.size())
122        {
123                mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
124        }
125       
126        return mOcclusionQueries[mCurrentTestIdx ++];
127}
128//-----------------------------------------------------------------------
129void PlatformHierarchyInterface::InitTraversal(Camera *cam, Camera *cullCam,
130                                                                                           int leavePassesInQueue)
131{
132        GtpVisibility::HierarchyInterface::InitTraversal();
133       
134        mSavedNode = NULL;
135        mLeavePassesInQueue = leavePassesInQueue;
136
137        mCamera = cam;
138        mCullCamera = cullCam ? cullCam : cam;
139
140        mCameraPosition = mCullCamera->getDerivedPosition();
141        // create materials for node visualization
142        CreateNodeVizMaterials();
143}
144//-----------------------------------------------------------------------
145void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
146{
147        mSceneManager = sm;
148}
149//-----------------------------------------------------------------------
150void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
151{
152        mRenderSystem = rsys;
153}
154//-----------------------------------------------------------------------
155bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
156                                                                                                         bool &intersects)
157{
158#ifdef GTP_VISIBILITY_MODIFIED_OGRE
159        return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
160#else
161        return true;
162#endif
163}
164//-----------------------------------------------------------------------
165GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
166        GtpVisibility::HierarchyNode *node, const bool wasVisible)
167{
168        // get next available test id
169        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
170
171        //-- the actual query test
172        query->BeginQuery();
173       
174        // if node is leaf and was visible => will be rendered anyway.
175        // In this case we can also test with the real geometry.
176        // If camera for culling is different from camera for rendering or only solids
177        // will be rendereded => cannot optimize
178        if (mTestGeometryForVisibleLeaves && (mCamera == mCullCamera) && wasVisible && IsLeaf(node))
179        {
180                //LogManager::getSingleton().logMessage("render node\n");
181                RenderNode(node);
182        }
183        else
184        {
185                // this information is used e.g., by the scene graph, because the bounding box
186                // must be treated differently to the scene geometry during rendering
187                mIsBoundingBoxQuery = true;
188
189                //LogManager::getSingleton().logMessage("render box\n");
190                RenderBoundingBox(GetBoundingBox(node));
191
192                mIsBoundingBoxQuery = false;
193        }
194
195        query->EndQuery();
196       
197        return query;
198}
199//-----------------------------------------------------------------------
200GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssuePatchOcclusionQuery(
201        GtpVisibility::Patch *patch)
202{
203        // get next available test id
204        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
205
206        //-- the actual query test
207        query->BeginQuery();
208       
209        RenderPatch(patch);
210
211        query->EndQuery();
212
213        return query;
214}
215//-----------------------------------------------------------------------
216GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMeshOcclusionQuery(
217        GtpVisibility::Mesh *mesh)
218{
219        // get next available test id
220        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
221
222        //-- the actual query test
223        query->BeginQuery();
224       
225        RenderGeometry(mesh);
226
227        query->EndQuery();
228
229        return query;
230}
231//-----------------------------------------------------------------------
232/*void PlatformHierarchyInterface::SetOcclusionPass()
233{
234    // disable vertex and fragment program
235        mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
236        mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
237       
238        // disable lighting
239        mRenderSystem->setLightingEnabled(false);
240
241    // Disable remaining texture units
242    mRenderSystem->_disableTextureUnitsFrom(0);
243
244    //--Set up non-texture related material settings
245   
246        // Depth buffer settings
247        mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
248        // Set colour write mode off
249        mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
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::GetTestGeometryForVisibleLeaves()
271{
272        return mTestGeometryForVisibleLeaves;
273}
274//-----------------------------------------------------------------------
275bool PlatformHierarchyInterface::IsBoundingBoxQuery()
276{
277        return mIsBoundingBoxQuery;
278}
279//-----------------------------------------------------------------------
280void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
281{
282        mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
283}
284//-----------------------------------------------------------------------
285void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
286{
287        mSceneManager->_renderSingleRenderable(patch);
288}
289//-----------------------------------------------------------------------
290SceneManager *PlatformHierarchyInterface::GetSceneManager()
291{
292        return mSceneManager;
293}
294//-----------------------------------------------------------------------
295RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
296{
297        return mRenderSystem;
298}
299
300} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.