source: trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneManager.cpp @ 21

Revision 21, 5.0 KB checked in by gametools, 20 years ago (diff)
Line 
1#include "OgreOcclusionCullingSceneManager.h"
2#include "OgreMath.h"
3#include "OgreIteratorWrappers.h"
4#include "OgreRenderSystem.h"
5#include "OgreCamera.h"
6#include "OgreHardwareOcclusionQuery.h"
7
8#include <windows.h>
9
10namespace Ogre {
11       
12        //-----------------------------------------------------------------------
13        OcclusionCullingSceneManager::OcclusionCullingSceneManager():
14        mFrameID(1), mDistanceQueue(NULL), mVisibilityThreshold(0), mCurrentTestIdx(0),
15        mIsQueryMode(false)
16        {               
17        }
18
19        void OcclusionCullingSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
20        {
21                // empty because we have to find in _renderVisibleObjects
22        }
23
24        //-----------------------------------------------------------------------
25        void OcclusionCullingSceneManager::_renderVisibleObjects(void)
26        {
27                mDistanceQueue = new PriorityQueue(myless<SceneNode *>(mCameraInProgress));
28                mDistanceQueue->push(mSceneRoot);
29                //renderZPass();
30                renderCullFrustum();
31                delete mDistanceQueue;
32                //mDestRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS);
33                //SceneManager::_renderVisibleObjects();
34                //mDestRenderSystem->_setDepthBufferParams();
35        //mDistanceQueue.push(mSceneRoot);
36                //Preprocess();
37               
38                //renderCullFrustum();
39
40                //mFrameID ++;
41                //ResetQueries();
42        }
43
44        //-----------------------------------------------------------------------
45        void OcclusionCullingSceneManager::renderZPass()
46        {
47                traverseNode(mSceneRoot);
48        }
49
50        //-----------------------------------------------------------------------
51        void OcclusionCullingSceneManager::renderCullFrustum()
52        {
53                while(!mDistanceQueue->empty())
54                {
55                        SceneNode *node = mDistanceQueue->top();
56                        mDistanceQueue->pop();
57       
58                        // interesting for the visualization, so rest and set
59                        //node->SetVisible(false);
60                               
61                        if(mCameraInProgress->isVisible(node->_getWorldAABB()))
62                        {
63                                // update node's visited flag => needed for rendering
64                                // so set it also here
65                                //node->SetLastVisited(mFrameID);
66                                //node->SetVisible(true);
67                                traverseNode(node);
68                        }
69                        //else
70                        //MessageBox( NULL, "myplugin registered", "this is my plugin", MB_OK | MB_ICONERROR | MB_TASKMODAL);
71                }
72        }
73       
74       
75        //-----------------------------------------------------------------------
76        void OcclusionCullingSceneManager::renderStopAndWait()
77        {
78                while(!mDistanceQueue->empty())
79                {
80                        SceneNode *node = mDistanceQueue->top();
81                        mDistanceQueue->pop();
82               
83                        // interesting for the visualization, so rest and set
84                        //node->SetVisible(false);
85
86                        if(mCameraInProgress->isVisible(node->_getWorldAABB()))
87                        {
88                                issueOcclusionQuery(node, false);
89                               
90                                unsigned int visiblePixels;
91                                // wait if result not available
92                                mOcclusionQueries[mCurrentTestIdx++]->pullOcclusionQuery(&visiblePixels);
93                               
94                                // node visible
95                                if(visiblePixels > mVisibilityThreshold)
96                                {
97                                        traverseNode(node);
98                                }
99                        }                                       
100                }
101        }
102
103        void OcclusionCullingSceneManager::issueOcclusionQuery(SceneNode *node, bool wasVisible)
104        {
105                // get next available test id
106                mOcclusionQueries[mCurrentTestIdx]->beginOcclusionQuery();
107       
108                // change state so the bounding box gets not actually rendered on the screen
109                switchMode(false);
110                node->_addBoundingBoxToQueue(getRenderQueue());
111                SceneManager::_renderVisibleObjects();
112                getRenderQueue()->clear();
113                switchMode(true);
114
115                mOcclusionQueries[mCurrentTestIdx++]->endOcclusionQuery();
116        }
117
118
119        //-----------------------------------------------------------------------
120        void OcclusionCullingSceneManager::switchMode(bool switch2QueryMode)
121        {       
122                // boolean used to avoid unnecessary state changes
123                if(switch2QueryMode != mIsQueryMode)
124                {
125                        mDestRenderSystem->_setColourBufferWriteEnabled(switch2QueryMode,
126                                switch2QueryMode, switch2QueryMode, switch2QueryMode);
127                        mDestRenderSystem->_setDepthBufferWriteEnabled(switch2QueryMode);
128                        mDestRenderSystem->setLightingEnabled(switch2QueryMode);
129                        mIsQueryMode = switch2QueryMode;
130                }
131        }
132       
133
134        //-----------------------------------------------------------------------
135        void OcclusionCullingSceneManager::traverseNode(SceneNode *node)
136        {
137                if(node->numChildren() == 0) // reached leaf
138                {
139                        renderSceneNode(node);
140                }
141                else // internal node: add children to priority queue for further processing
142                {
143                        Node::ChildNodeIterator it = node->getChildIterator();
144                        char str[100]; sprintf(str, "number of children: %d", node->numChildren());
145               
146                        while (it.hasMoreElements())
147                        {
148                                SceneNode* sceneChild = static_cast<SceneNode*>(it.getNext());
149                                //mDistanceQueue->addRenderable(sceneChild);
150                mDistanceQueue->push(sceneChild);
151                        }
152                 }
153
154                 /*
155                 static RenderOperation ro;
156                 node->getRenderOperation(ro);
157                 ro.srcRenderable = node;
158                 mDestRenderSystem->_render(ro);
159                 Pass pass();
160                 renderSingleObject(node, &pass, true);
161                 */
162        }
163
164        void OcclusionCullingSceneManager::renderSceneNode(SceneNode *node)
165        {
166                node->_findVisibleObjects(mCameraInProgress, getRenderQueue(), false,
167                                                                  mDisplayNodes, false);
168                SceneManager::_renderVisibleObjects();
169                getRenderQueue()->clear();
170        }
171}       
Note: See TracBrowser for help on using the repository browser.