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

Revision 2278, 10.2 KB checked in by mattausch, 17 years ago (diff)

worked on randomupdatemanager

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