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

Revision 897, 9.8 KB checked in by mattausch, 18 years ago (diff)

updated to ogre 1.2

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