source: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp @ 139

Revision 139, 9.1 KB checked in by mattausch, 19 years ago (diff)

fixed bug with tight octree boxes
added more flexible renderqueue (can delete per flag)
reordered functions in visibility terrain scene manager

Line 
1#include <OgreCamera.h>
2#include <OgreLogManager.h>
3#include <OgreSubEntity.h>
4#include <OgreEntity.h>
5#include <OgreMovableObject.h>
6#include "OgreSolidBoundingBox.h"
7#include "OgrePlatformHierarchyInterface.h"
8#include "OgrePlatformOcclusionQuery.h"
9
10namespace Ogre {
11
12//-----------------------------------------------------------------------
13PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm, RenderSystem *rsys):
14mSceneManager(sm), mRenderSystem(rsys), mSolidBoundingBox(NULL), mCamera(NULL),
15mCullCamera(NULL), mOnlyShadowCasters(false), mLeavePassesInQueue(0),
16mIsBoundingBoxQuery(false)
17{
18}
19//-----------------------------------------------------------------------
20void PlatformHierarchyInterface::CreateNodeVizMaterials()
21{
22        // material for frustum culled nodes
23        MaterialPtr mat = MaterialManager::getSingleton().getByName("FrustumCulledNodesMaterial");
24       
25        if (mat.isNull())
26        {
27                mat = MaterialManager::getSingleton().create("FrustumCulledNodesMaterial",
28                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
29                mat->createTechnique()->createPass();
30               
31                mat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
32                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
33                //mat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
34                mat->load();
35        }
36
37        // material for query culled nodes
38        mat = MaterialManager::getSingleton().getByName("QueryCulledNodesMaterial");
39
40        if (mat.isNull())
41        {
42                mat = MaterialManager::getSingleton().create("QueryCulledNodesMaterial",
43                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
44                mat->createTechnique()->createPass();
45               
46                mat->getTechnique(0)->getPass(0)->setAmbient(0, 0, 1);
47                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
48                mat->load();
49        }
50
51        // material for scene nodes
52        /*mat = MaterialManager::getSingleton().getByName("SceneNodesMaterial");
53
54        if (mat.isNull())
55        {
56                mat = MaterialManager::getSingleton().create("SceneNodesMaterial",
57                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
58                mat->createTechnique()->createPass();
59               
60                mat->getTechnique(0)->getPass(0)->setAmbient(1, 1, 0);
61                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
62                mat->load();
63        }*/
64}
65//-----------------------------------------------------------------------
66PlatformHierarchyInterface::~PlatformHierarchyInterface()
67{
68        DeleteQueries();
69
70        if (mSolidBoundingBox)
71                delete mSolidBoundingBox;
72}
73//-----------------------------------------------------------------------
74void PlatformHierarchyInterface::DeleteQueries()
75{
76        for (int i=0; i < (int)mOcclusionQueries.size(); ++i)
77                delete mOcclusionQueries[i];
78
79        mOcclusionQueries.clear();
80}
81//-----------------------------------------------------------------------
82void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
83{
84        static RenderOperation ro;
85
86        SolidBoundingBox *solidBox = GetSolidBoundingBox();
87       
88        mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
89        mSceneManager->useRenderableViewProjMode(solidBox);
90       
91        // set no depth write, no color, no lighting material
92        mSceneManager->setPass(solidBox->getTechnique()->getPass(0));
93        //SetOcclusionPass();
94
95        solidBox->SetupBoundingBoxVertices(*box);
96                               
97        solidBox->getRenderOperation(ro);
98        ro.srcRenderable = solidBox;
99        mRenderSystem->_render(ro);
100
101        // Render two halfes of the bounding box (using triangle fans)
102        /*for(int halfIdx = 0; halfIdx < 2; ++halfIdx)
103        {
104                solidBox->SetupBoundingBoxVertices(*box, halfIdx == 1);
105                               
106                solidBox->getRenderOperation(ro);
107                ro.srcRenderable = solidBox;
108                mRenderSystem->_render(ro);
109        }*/
110}
111//-----------------------------------------------------------------------
112void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
113{
114        mCamera = cam;
115}
116//-----------------------------------------------------------------------
117void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
118{
119        mCullCamera = cullCam;
120}
121//-----------------------------------------------------------------------
122GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
123{
124        if (mCurrentTestIdx == mOcclusionQueries.size())
125        {
126                mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
127        }
128       
129        return mOcclusionQueries[mCurrentTestIdx ++];
130}
131//-----------------------------------------------------------------------
132void PlatformHierarchyInterface::InitFrame(GtpVisibility::HierarchyNode *root,
133                                                                                   Camera *cam, Camera *cullCam, int leavePassesInQueue)
134{
135        GtpVisibility::HierarchyInterface::InitFrame(root);
136       
137        mPreviousNode = NULL;
138        mLeavePassesInQueue = leavePassesInQueue;
139
140        mCamera = cam;
141        mCullCamera = cullCam ? cullCam : cam;
142
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::IssueOcclusionQuery(
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 (mUseOptimization && (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::IssueOcclusionQuery(GtpVisibility::Mesh *mesh)
203{
204        // get next available test id
205        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
206
207        //-- the actual query test
208        query->BeginQuery();
209       
210        RenderGeometry(mesh);
211
212        query->EndQuery();
213
214        return query;
215}
216//-----------------------------------------------------------------------
217/*void PlatformHierarchyInterface::SetOcclusionPass()
218{
219    // disable vertex and fragment program
220        mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
221        mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
222       
223        // disable lighting
224        mRenderSystem->setLightingEnabled(false);
225
226    // Disable remaining texture units
227    mRenderSystem->_disableTextureUnitsFrom(0);
228
229    //--Set up non-texture related material settings
230   
231        // Depth buffer settings
232        mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
233        // Set colour write mode off
234        mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
235}*/
236//-----------------------------------------------------------------------
237SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
238{
239        if (!mSolidBoundingBox)
240                mSolidBoundingBox = new SolidBoundingBox;
241
242        return mSolidBoundingBox;
243}
244//-----------------------------------------------------------------------
245void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
246{
247        mOnlyShadowCasters = onlyShadowCasters;
248}
249//-----------------------------------------------------------------------
250bool PlatformHierarchyInterface::GetOnlyShadowCasters()
251{
252        return mOnlyShadowCasters;
253}
254//-----------------------------------------------------------------------
255bool PlatformHierarchyInterface::GetUseOptimization()
256{
257        return mUseOptimization;
258}
259//-----------------------------------------------------------------------
260bool PlatformHierarchyInterface::IsBoundingBoxQuery()
261{
262        return mIsBoundingBoxQuery;
263}
264//-----------------------------------------------------------------------
265void PlatformHierarchyInterface::RenderGeometry(GtpVisibility::Mesh *geom)
266{
267        mSceneManager->_renderMovableObject(geom, mLeavePassesInQueue);
268}
269//-----------------------------------------------------------------------
270SceneManager *PlatformHierarchyInterface::GetSceneManager()
271{
272        return mSceneManager;
273}
274//-----------------------------------------------------------------------
275RenderSystem *PlatformHierarchyInterface::GetRenderSystem()
276{
277        return mRenderSystem;
278}
279} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.