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

Revision 925, 9.9 KB checked in by mattausch, 18 years ago (diff)

update for ogre 1.2
OcclusionCullingSceneManager? is the only scenemanager in the solution now

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
78        OGRE_DELETE(mSolidBoundingBox);
79}
80//-----------------------------------------------------------------------
81void PlatformHierarchyInterface::ResetQueries()
82{
83        for (int i = 0; i < (int)mOcclusionQueries.size(); ++ i) 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        //mSceneManager->_setPass(solidBox->getTechnique()->getPass(0)); // HACK! (mySetPass should be setPass)
101        //SetOcclusionPass();
102
103        solidBox->SetupBoundingBoxVertices(*box);
104                               
105        solidBox->getRenderOperation(ro);
106        ro.srcRenderable = solidBox;
107        mRenderSystem->_render(ro);
108}
109//-----------------------------------------------------------------------
110void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
111{
112        mCamera = cam;
113}
114//-----------------------------------------------------------------------
115void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
116{
117        mCullCamera = cullCam;
118}
119//-----------------------------------------------------------------------
120GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
121{
122        // create new query if there is no query left
123        if (mCurrentTestIdx >= (int)mOcclusionQueries.size())
124        {
125                mCurrentTestIdx = (int)mOcclusionQueries.size();
126                mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
127        }
128       
129        return mOcclusionQueries[mCurrentTestIdx ++];
130}
131//-----------------------------------------------------------------------
132void PlatformHierarchyInterface::InitTraversal(Camera *cam, Camera *cullCam,
133                                                                                           int leavePassesInQueue)
134{
135        GtpVisibility::HierarchyInterface::InitTraversal();
136       
137        mSavedNode = NULL;
138        mLeavePassesInQueue = leavePassesInQueue;
139
140        mCamera = cam;
141        // set culling camera for visualization
142        mCullCamera = cullCam ? cullCam : cam;
143
144        mCameraPosition = mCullCamera->getDerivedPosition();
145        // create materials for node visualization
146        CreateNodeVizMaterials();
147}
148//-----------------------------------------------------------------------
149void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
150{
151        mSceneManager = sm;
152}
153//-----------------------------------------------------------------------
154void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
155{
156        mRenderSystem = rsys;
157}
158//-----------------------------------------------------------------------
159bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
160                                                                                                         bool &intersects)
161{
162#ifdef GTP_VISIBILITY_MODIFIED_OGRE
163        return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
164#else
165        return true;
166#endif
167}
168//-----------------------------------------------------------------------
169GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
170        GtpVisibility::HierarchyNode *node, const bool wasVisible)
171{
172        // get next available test id
173        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
174
175        //-- the actual query test
176        query->BeginQuery();
177       
178        // if node is leaf and was visible => will be rendered anyway.
179        // In this case we can also test with the real geometry.
180        // If camera for culling is different from camera for rendering or only solids
181        // will be rendereded => cannot optimize
182        if (mTestGeometryForVisibleLeaves && (mCamera == mCullCamera) && wasVisible && IsLeaf(node))
183        {
184                //LogManager::getSingleton().logMessage("render node\n");
185                RenderNode(node);
186        }
187        else
188        {
189                // this information is used e.g., by the scene graph, because the bounding box
190                // must be treated differently to the scene geometry during rendering
191                mIsBoundingBoxQuery = true;
192
193                //LogManager::getSingleton().logMessage("render box\n");
194                RenderBoundingBox(GetBoundingBox(node));
195
196                mIsBoundingBoxQuery = false;
197        }
198
199        query->EndQuery();
200       
201        return query;
202}
203//-----------------------------------------------------------------------
204GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssuePatchOcclusionQuery(
205        GtpVisibility::Patch *patch)
206{
207        // get next available test id
208        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
209
210        //-- the actual query test
211        query->BeginQuery();
212       
213        RenderPatch(patch);
214
215        query->EndQuery();
216
217        return query;
218}
219//-----------------------------------------------------------------------
220GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueMeshOcclusionQuery(
221        GtpVisibility::Mesh *mesh)
222{
223        // get next available test id
224        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
225
226        //-- the actual query test
227        query->BeginQuery();
228       
229        RenderGeometry(mesh);
230
231        query->EndQuery();
232
233        return query;
234}
235//-----------------------------------------------------------------------
236/*void PlatformHierarchyInterface::SetOcclusionPass()
237{
238    // disable vertex and fragment program
239        mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
240        mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
241       
242        // disable lighting
243        mRenderSystem->setLightingEnabled(false);
244
245    // Disable remaining texture units
246    mRenderSystem->_disableTextureUnitsFrom(0);
247
248    //--Set up non-texture related material settings
249   
250        // Depth buffer settings
251        mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
252        // Set colour write mode off
253        mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
254}*/
255//-----------------------------------------------------------------------
256SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
257{
258        if (!mSolidBoundingBox)
259                mSolidBoundingBox = new SolidBoundingBox;
260
261        return mSolidBoundingBox;
262}
263//-----------------------------------------------------------------------
264void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
265{
266        mOnlyShadowCasters = onlyShadowCasters;
267}
268//-----------------------------------------------------------------------
269bool PlatformHierarchyInterface::GetOnlyShadowCasters()
270{
271        return mOnlyShadowCasters;
272}
273//-----------------------------------------------------------------------
274bool PlatformHierarchyInterface::GetTestGeometryForVisibleLeaves()
275{
276        return mTestGeometryForVisibleLeaves;
277}
278//-----------------------------------------------------------------------
279bool PlatformHierarchyInterface::IsBoundingBoxQuery()
280{
281        return mIsBoundingBoxQuery;
282}
283//-----------------------------------------------------------------------
284void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
285{
286        mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
287}
288//-----------------------------------------------------------------------
289void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
290{
291        mSceneManager->_renderSingleRenderable(patch);
292}
293//-----------------------------------------------------------------------
294SceneManager *PlatformHierarchyInterface::GetSceneManager()
295{
296        return mSceneManager;
297}
298//-----------------------------------------------------------------------
299RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
300{
301        return mRenderSystem;
302}
303
304} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.