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

Revision 1621, 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        OGRE_DELETE(mSolidBoundingBox);
78}
79//-----------------------------------------------------------------------
80void PlatformHierarchyInterface::ResetQueries()
81{
82        OcclusionQueryContainer::iterator it, it_end = mOcclusionQueries.end();
83        for (it = mOcclusionQueries.begin(); it != it_end; ++ it)
84        {
85                PlatformOcclusionQuery *query = *it;
86                OGRE_DELETE(query);
87        }
88
89        mCurrentTestIdx = 0;
90    mOcclusionQueries.clear();
91}
92//-----------------------------------------------------------------------
93void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
94{
95        static RenderOperation ro;
96
97        SolidBoundingBox *solidBox = GetSolidBoundingBox();
98       
99        mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
100        mSceneManager->useRenderableViewProjModeWrapper(solidBox);
101        // HACK! (mySetPass should be setPass)
102        // set no depth write, no color, no lighting material
103        mSceneManager->setPassWrapper(solidBox->getTechnique()->getPass(0));
104        //mSceneManager->_setPass(solidBox->getTechnique()->getPass(0));
105        //SetOcclusionPass();
106
107        solidBox->SetupBoundingBoxVertices(*box);
108                               
109        solidBox->getRenderOperation(ro);
110        ro.srcRenderable = solidBox;
111        mRenderSystem->_render(ro);
112}
113//-----------------------------------------------------------------------
114void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
115{
116        mCamera = cam;
117}
118//-----------------------------------------------------------------------
119void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
120{
121        mCullCamera = cullCam;
122}
123//-----------------------------------------------------------------------
124GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
125{
126        // create new query if there is no query left
127        if (mCurrentTestIdx == (int)mOcclusionQueries.size())
128        {
129                mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
130
131                /*std::stringstream d;
132                d << "resizing queries: " << (int)mOcclusionQueries.size() << std::endl;
133                LogManager::getSingleton().logMessage(d.str());*/
134        }
135       
136        return mOcclusionQueries[mCurrentTestIdx ++];
137}
138//-----------------------------------------------------------------------
139void PlatformHierarchyInterface::InitTraversal(Camera *cam, Camera *cullCam,
140                                                                                           int leavePassesInQueue)
141{
142        GtpVisibility::HierarchyInterface::InitTraversal();
143       
144        mSavedNode = NULL;
145        mLeavePassesInQueue = leavePassesInQueue;
146
147        mCamera = cam;
148        // set culling camera for visualization
149        mCullCamera = cullCam ? cullCam : cam;
150
151        mCameraPosition = mCullCamera->getDerivedPosition();
152        // create materials for node visualization
153        CreateNodeVizMaterials();
154}
155//-----------------------------------------------------------------------
156void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
157{
158        mSceneManager = sm;
159}
160//-----------------------------------------------------------------------
161void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
162{
163        mRenderSystem = rsys;
164}
165//-----------------------------------------------------------------------
166bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
167                                                                                                         bool &intersects)
168{
169#ifdef GTP_VISIBILITY_MODIFIED_OGRE
170        return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
171#else
172        return true;
173#endif
174}
175//-----------------------------------------------------------------------
176GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueNodeOcclusionQuery(
177        GtpVisibility::HierarchyNode *node, const bool wasVisible)
178{
179        // get next available test id
180        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
181
182        //-- the actual query test
183        query->BeginQuery();
184       
185        // if node is leaf and was visible => will be rendered anyway.
186        // In this case we can also test with the real geometry.
187        // If camera for culling is different from camera for rendering or only solids
188        // will be rendereded => cannot optimize
189        if (mTestGeometryForVisibleLeaves && (mCamera == mCullCamera) && wasVisible && IsLeaf(node))
190        {
191                RenderNode(node);
192        }
193        else
194        {
195                // this information is used e.g., by the scene graph, because the bounding box
196                // must be treated differently to the scene geometry during rendering
197                mIsBoundingBoxQuery = true;
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        ////////
231        //-- the actual query test
232
233        query->BeginQuery();
234        RenderGeometry(mesh);
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.