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

Revision 2184, 10.3 KB checked in by mattausch, 17 years ago (diff)

debug version: please don't check out

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