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

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