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

Revision 120, 7.7 KB checked in by mattausch, 19 years ago (diff)
Line 
1#include <OgreCamera.h>
2#include <OgreLogManager.h>
3#include "OgreSolidBoundingBox.h"
4#include "OgrePlatformHierarchyInterface.h"
5#include "OgrePlatformOcclusionQuery.h"
6
7namespace Ogre {
8
9//-----------------------------------------------------------------------
10PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm, RenderSystem *rsys):
11mSceneManager(sm), mRenderSystem(rsys), mSolidBoundingBox(NULL),
12mCamera(NULL), mCullCamera(NULL), mOnlyShadowCasters(false), mLeaveTransparentsInQueue(false)
13{
14}
15//-----------------------------------------------------------------------
16void PlatformHierarchyInterface::CreateNodeVizMaterials()
17{
18        // material for frustum culled nodes
19        MaterialPtr mat = MaterialManager::getSingleton().getByName("FrustumCulledNodesMaterial");
20       
21        if (mat.isNull())
22        {
23                mat = MaterialManager::getSingleton().create("FrustumCulledNodesMaterial",
24                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
25                mat->createTechnique()->createPass();
26               
27                mat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
28                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
29                //mat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
30                mat->load();
31        }
32
33        // material for query culled nodes
34        mat = MaterialManager::getSingleton().getByName("QueryCulledNodesMaterial");
35
36        if (mat.isNull())
37        {
38                mat = MaterialManager::getSingleton().create("QueryCulledNodesMaterial",
39                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
40                mat->createTechnique()->createPass();
41               
42                mat->getTechnique(0)->getPass(0)->setAmbient(0, 0, 1);
43                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
44                mat->load();
45        }
46
47        // material for scene nodes
48        /*mat = MaterialManager::getSingleton().getByName("SceneNodesMaterial");
49
50        if (mat.isNull())
51        {
52                mat = MaterialManager::getSingleton().create("SceneNodesMaterial",
53                        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
54                mat->createTechnique()->createPass();
55               
56                mat->getTechnique(0)->getPass(0)->setAmbient(1, 1, 0);
57                mat->getTechnique(0)->getPass(0)->setLightingEnabled(true);
58                mat->load();
59        }*/
60}
61//-----------------------------------------------------------------------
62PlatformHierarchyInterface::~PlatformHierarchyInterface()
63{
64        DeleteQueries();
65
66        if (mSolidBoundingBox)
67                delete mSolidBoundingBox;
68}
69//-----------------------------------------------------------------------
70void PlatformHierarchyInterface::DeleteQueries()
71{
72        for (int i=0; i < (int)mOcclusionQueries.size(); ++i)
73                delete mOcclusionQueries[i];
74
75        mOcclusionQueries.clear();
76}
77//-----------------------------------------------------------------------
78void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
79{
80        static RenderOperation ro;
81
82        SolidBoundingBox *solidBox = GetSolidBoundingBox();
83       
84        mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
85        mSceneManager->useRenderableViewProjMode(solidBox);
86       
87        // set no depth write, no color, no lighting material
88        //mSceneManager->setPass(solidBox->getTechnique()->getPass(0));
89        SetOcclusionPass();
90
91        solidBox->SetupBoundingBoxVertices(*box);
92                               
93        solidBox->getRenderOperation(ro);
94        ro.srcRenderable = solidBox;
95        mRenderSystem->_render(ro);
96
97        // Render two halfes of the bounding box (using triangle fans)
98        /*for(int halfIdx = 0; halfIdx < 2; ++halfIdx)
99        {
100                solidBox->SetupBoundingBoxVertices(*box, halfIdx == 1);
101                               
102                solidBox->getRenderOperation(ro);
103                ro.srcRenderable = solidBox;
104                mRenderSystem->_render(ro);
105        }*/
106}
107//-----------------------------------------------------------------------
108void PlatformHierarchyInterface::SetCamera(Ogre::Camera *cam)
109{
110        mCamera = cam;
111}
112//-----------------------------------------------------------------------
113void PlatformHierarchyInterface::SetCullCamera(Ogre::Camera *cullCam)
114{
115        mCullCamera = cullCam;
116}
117//-----------------------------------------------------------------------
118GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::GetNextOcclusionQuery()
119{
120        if (mCurrentTestIdx == mOcclusionQueries.size())
121        {
122                mOcclusionQueries.push_back(new PlatformOcclusionQuery(mRenderSystem));
123        }
124       
125        return mOcclusionQueries[mCurrentTestIdx ++];
126}
127//-----------------------------------------------------------------------
128void PlatformHierarchyInterface::InitFrame(GtpVisibility::HierarchyNode *root,
129                                                                                   Camera *cam, Camera *cullCam, bool leaveTransparentsInQueue)
130{
131        GtpVisibility::HierarchyInterface::InitFrame(root);
132        mPreviousNode = NULL;
133        mLeaveTransparentsInQueue = leaveTransparentsInQueue;
134        SetCamera(cam);
135
136        if (cullCam)
137        {
138                SetCullCamera(cullCam);
139        }
140        else
141        {
142                SetCullCamera(cam);
143        }
144
145        CreateNodeVizMaterials();
146}
147//-----------------------------------------------------------------------
148void PlatformHierarchyInterface::SetSceneManager(SceneManager *sm)
149{
150        mSceneManager = sm;
151}
152//-----------------------------------------------------------------------
153void PlatformHierarchyInterface::SetRenderSystem(RenderSystem *rsys)
154{
155        mRenderSystem = rsys;
156}
157//-----------------------------------------------------------------------
158bool PlatformHierarchyInterface::CheckFrustumVisible(GtpVisibility::HierarchyNode *node,
159                                                                                                         bool &intersects)
160{
161#ifdef GTP_VISIBILITY_MODIFIED_OGRE
162        return mCullCamera->isVisible(*GetBoundingBox(node), intersects);
163#else
164        return true;
165#endif
166}
167//-----------------------------------------------------------------------
168GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueOcclusionQuery(
169        GtpVisibility::HierarchyNode *node, const bool wasVisible)
170{
171        // get next available test id
172        GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
173
174        //-- the actual query test
175        query->BeginQuery();
176                       
177        // if node is leaf and was visible => will be rendered anyway.
178        // In this case we can also test with the real geometry.
179        // If camera for culling is different from camera for rendering or only solids
180        // will be rendereded => cannot optimize
181        if (mUseOptimization && (mCamera == mCullCamera) && wasVisible && IsLeaf(node) )
182        {
183                //LogManager::getSingleton().logMessage("render node\n");
184                RenderNode(node);
185        }
186        else
187        {
188                //LogManager::getSingleton().logMessage("render box\n");
189                RenderBoundingBox(GetBoundingBox(node));
190        }
191
192        query->EndQuery();
193       
194        return query;
195}
196//-----------------------------------------------------------------------
197void PlatformHierarchyInterface::SetOcclusionPass()
198{
199    // disable vertex and fragment program
200        mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
201        mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
202       
203        // disable lighting
204        mRenderSystem->setLightingEnabled(false);
205
206    // Disable remaining texture units
207    mRenderSystem->_disableTextureUnitsFrom(0);
208
209    //--Set up non-texture related material settings
210   
211        // Depth buffer settings
212        mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
213        // Set colour write mode off
214        mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
215}
216//-----------------------------------------------------------------------
217SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
218{
219        if (!mSolidBoundingBox)
220                mSolidBoundingBox = new SolidBoundingBox;
221
222        return mSolidBoundingBox;
223}
224//-----------------------------------------------------------------------
225void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
226{
227        mOnlyShadowCasters = onlyShadowCasters;
228        //mIsShadowPass = mOnlyShadowCasters ;
229}
230//-----------------------------------------------------------------------
231bool PlatformHierarchyInterface::GetOnlyShadowCasters()
232{
233        return mOnlyShadowCasters;
234}
235//-----------------------------------------------------------------------
236bool PlatformHierarchyInterface::GetUseOptimization()
237{
238        return mUseOptimization;
239}
240} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.