source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp @ 3102

Revision 3102, 5.6 KB checked in by mattausch, 16 years ago (diff)

orthocam not working yet

Line 
1#include "RenderTraverser.h"
2#include "glInterface.h"
3#include "Camera.h"
4#include "SceneEntity.h"
5#include "RenderState.h"
6#include "Geometry.h"
7#include "Timer/PerfTimer.h"
8
9
10using namespace std;
11
12
13namespace CHCDemoEngine
14{
15
16       
17void TraversalStatistics::Reset()
18{
19        mNumTraversedNodes = 0;
20        mNumQueryCulledNodes = 0;
21        mNumFrustumCulledNodes = 0;
22       
23        mNumRenderedGeometry = 0;
24        mNumRenderedTriangles = 0;
25        mNumRenderedNodes = 0;
26
27        mNumIssuedQueries = 0;
28        mNumStateChanges = 0;
29        mNumBatches = 0;
30
31        mWaitTime = 0;
32        mQueryTime = 0;
33        mRestTime = 0;
34}
35
36
37
38/******************************************************/
39/*           RenderTraverser implementation           */
40/******************************************************/
41
42
43RenderTraverser::RenderTraverser(): 
44mVisibilityThreshold(0),
45mBvh(NULL),
46mUseOptimization(false),
47mFrameId(-1),
48mUseRenderQueue(false),
49mAssumedVisibleFrames(10),
50mMaxBatchSize(50),
51mUseTightBounds(false),
52mShowBounds(false),
53mRenderQueue(NULL),
54mMaxVisibleDistance(.0f),
55mRenderDynamicObjects(true)
56{
57}
58
59
60RenderTraverser::~RenderTraverser()
61{
62        mQueryHandler.DestroyQueries();
63}
64
65
66void RenderTraverser::EnqueueNode(BvhNode *node)
67{
68        mBvh->UpdateDistance(node);
69        mDistanceQueue.push(node);
70}
71
72
73void RenderTraverser::TraverseNode(BvhNode *node)
74{
75        ++ mStats.mNumTraversedNodes;
76
77        if (node->IsVirtualLeaf())
78        {
79                RenderNode(node);
80
81                if (mShowBounds)
82                        mBvh->RenderBoundsForViz(node, mRenderState, mUseTightBounds);
83        }
84        else
85        {
86                BvhInterior *interior = static_cast<BvhInterior *>(node);
87
88                EnqueueNode(interior->GetFront());
89                EnqueueNode(interior->GetBack());
90        }
91}
92
93
94void RenderTraverser::RenderNode(BvhNode *node)
95{
96        // test if node was already rendered in this frame
97        if (node->GetLastRenderedFrame() != mFrameId)
98        {
99                if (mRenderState->SetMode(RenderState::RENDER))
100                        ++ mStats.mNumStateChanges;
101               
102                node->SetLastRenderedFrame(mFrameId);
103
104                int geometrySize;
105                SceneEntity **entities = mBvh->GetGeometry(node, geometrySize);
106
107                mStats.mNumRenderedGeometry += geometrySize;
108                ++ mStats.mNumRenderedNodes;
109               
110                for (int i = 0; i < geometrySize; ++ i)
111                {
112                        SceneEntity *ent = entities[i];
113               
114                        mStats.mNumRenderedTriangles += ent->CountNumTriangles();
115
116                        if (mUseRenderQueue)
117                                mRenderQueue->Enqueue(ent);
118                        else
119                                ent->Render(mRenderState);
120
121                        // store the visible entities for rendering in the second pass
122                        if (mUseDepthPass) mVisibleObjects.push_back(ent);
123                }
124
125                // store the max distance in the scene for later use
126                float maxDist = mBvh->CalcMaxDistance(node);
127
128                if (maxDist > mMaxVisibleDistance)
129                        mMaxVisibleDistance = maxDist;
130        }
131}
132
133
134void RenderTraverser::SetHierarchy(Bvh *bvh)
135{
136        mBvh = bvh;
137}
138
139
140void RenderTraverser::SetRenderState(RenderState *state)
141{
142        mRenderState = state;
143}
144
145
146void RenderTraverser::RenderScene()
147{
148        mVisibleObjects.clear();
149        mMaxVisibleDistance = .0f;
150
151        ++ mFrameId;
152
153        mBvh->InitFrame(mCamera, mRenderState);
154
155        mStats.Reset();
156        mQueryHandler.ResetQueries();
157
158        // add root node to queue
159        if (mRenderDynamicObjects)
160                EnqueueNode(mBvh->GetRoot());
161        else
162                EnqueueNode(mBvh->GetStaticRoot());
163
164       
165
166        ///////////
167        //-- the actual rendering algorithm
168
169        Traverse();
170
171        // render the contents of the render queue
172        if (mUseRenderQueue) ApplyRenderQueue();
173       
174        // reset the render state
175        mRenderState->Reset();
176}
177
178
179void RenderTraverser::SetUseRenderQueue(bool useRenderQueue)
180{
181        mUseRenderQueue = useRenderQueue;
182        //std::cout << "using render queue: " << mUseRenderQueue << std::endl;
183}
184
185
186void RenderTraverser::SetUseDepthPass(bool b)
187{
188        mUseDepthPass = b;
189}
190
191
192void RenderTraverser::SetVisibilityThreshold(int threshold)
193{
194        mVisibilityThreshold = threshold;
195}
196
197
198void RenderTraverser::SetUseOptimization(bool useOptimization)
199{
200        mUseOptimization = useOptimization;
201        //cout << "using optimization: " << mUseOptimization << endl;
202}
203
204
205void RenderTraverser::SetAssumedVisibleFrames(int assumedVisibleFrames)
206{
207        mAssumedVisibleFrames = assumedVisibleFrames;
208}
209
210
211void RenderTraverser::SetMaxBatchSize(int batchSize)
212{
213        mMaxBatchSize = batchSize;
214}
215
216
217void RenderTraverser::SetUseMultiQueries(bool useMultiQueries)
218{
219        mUseMultiQueries = useMultiQueries;
220        //cout << "using multiqueries: " << mUseMultiQueries << endl;
221}
222
223
224void RenderTraverser::SetShowBounds(bool showBounds)
225{
226        mShowBounds = showBounds;
227}
228
229
230void RenderTraverser::SetUseTightBounds(bool useTightBounds)
231{
232        mUseTightBounds = useTightBounds;
233        //cout << "using tight bounds: " << useTightBounds << endl;
234}
235
236
237OcclusionQuery *RenderTraverser::IssueOcclusionQuery(BvhNode *node)
238{
239        OcclusionQuery *query = mQueryHandler.RequestQuery();
240        query->AddNode(node);
241
242        IssueOcclusionQuery(*query);
243
244        return query;
245}
246
247
248void RenderTraverser::IssueOcclusionQuery(const OcclusionQuery &query)
249{
250        ++ mStats.mNumIssuedQueries;
251
252        // render pending objects before changing to query mode
253        if (mUseRenderQueue && (mRenderState->GetMode() == RenderState::RENDER))
254        {
255                ApplyRenderQueue();
256        }
257
258        query.BeginQuery();
259
260        // change to query mode and render box
261        if (mRenderState->SetMode(RenderState::QUERY))
262        {
263                ++ mStats.mNumStateChanges;
264        }
265
266        mBvh->RenderBounds(query.GetNodes(), mRenderState, mUseTightBounds);
267       
268        query.EndQuery();
269}
270
271
272void RenderTraverser::ApplyRenderQueue()
273{
274        if (mRenderState->SetMode(RenderState::RENDER))
275                ++ mStats.mNumStateChanges;
276                 
277        if (mRenderQueue->GetSize() > 0)
278        {
279                ++ mStats.mNumBatches;
280                mRenderQueue->Apply();
281        }
282}
283
284
285void RenderTraverser::SetRenderDynamicObjects(bool dynamic)
286{
287        mRenderDynamicObjects = dynamic;
288}
289
290}
Note: See TracBrowser for help on using the repository browser.