source: branches/VUT/0.3/Ogre/src/OgrePlatformHierarchyInterface.cpp @ 187

Revision 187, 9.5 KB checked in by mattausch, 19 years ago (diff)

added animationbug fix (deleting while animation)fixed visibilityQueriesadditive shadow volumes fixed for octree
hack to fully empty queue after traversal
added demo for vienna model

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