source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/PvsCollectionRenderer.cpp @ 3259

Revision 3259, 3.2 KB checked in by mattausch, 15 years ago (diff)
Line 
1#include "PvsCollectionRenderer.h"
2#include "ViewCellsTree.h"
3
4
5using namespace std;
6
7
8namespace CHCDemoEngine
9{
10
11
12PvsCollectionRenderer::PvsCollectionRenderer():
13RenderTraverser(), mViewCell(NULL)
14{
15}
16
17
18PvsCollectionRenderer::~PvsCollectionRenderer()
19{
20}
21
22
23void PvsCollectionRenderer::Traverse()
24{
25        mPIS.clear();
26
27        ////////////
28        //-- part1: do frustum culling + pvs
29
30        while (!mDistanceQueue.empty())
31        {
32                BvhNode *node = mDistanceQueue.top();
33                mDistanceQueue.pop();
34       
35                if (mBvh->IsWithinViewFrustum(node))
36                {
37                        // for leaves: check if the geometry is already in PVS
38                        if (!node->IsVirtualLeaf()  || IsNodeGeometryVisible(node, -1))
39                        {
40                                TraverseNode(node);
41                        }
42                        else
43                        {
44                                mPIS.push_back(node);
45                        }
46                }
47                else
48                {
49                        ++ mStats.mNumFrustumCulledNodes;
50                }
51        }
52
53        int pisSize = (int)mPIS.size();
54
55
56        ////////////
57        //-- part2: test the potentially invisible nodes
58
59        static QueryQueue queries;
60
61        BvhNodeContainer::const_iterator it, it_end = mPIS.end();
62
63        OcclusionQuery *query = mQueryHandler.RequestQuery();
64        query->Reset();
65
66        int querySize = 50;
67
68        for (it = mPIS.begin(); it != it_end; ++ it)
69        {
70                query->AddNode(*it);
71
72                if (query->GetSize() == querySize)
73                {
74                        // issue multiquery
75                        IssueOcclusionQuery(*query);
76                        queries.push(query);
77
78                        // get next query
79                        query = mQueryHandler.RequestQuery();
80                        query->Reset();
81                }
82        }
83
84        // query the rest of the nodes
85        if (!query->IsEmpty())
86        {
87                IssueOcclusionQuery(*query);
88                queries.push(query);
89        }
90
91       
92        static BvhNodeContainer probablyVisibleNodes;
93        probablyVisibleNodes.clear();
94       
95        int qsize = (int)queries.size();
96        int fqueries = 0;
97
98        // get query results: collect probably visible nodes
99        while (!queries.empty())
100        {
101                OcclusionQuery *q = queries.front();
102                queries.pop();
103       
104                const int visiblePixels = q->GetQueryResult();
105
106                if (visiblePixels > 0)
107                {
108                        ++ fqueries;
109
110                        BvhNodeContainer::const_iterator bit, bit_end = q->GetNodes().end();
111
112                        for (bit= q->GetNodes().begin(); bit != bit_end; ++ bit)
113                        {
114                                // query failed => render and add to probably visible nodes
115                                RenderNode(*bit);
116                                probablyVisibleNodes.push_back(*bit);
117                        }
118                }
119        }
120
121        //cout << "probably visible: " << fqueries << " of " << qsize << " (" << (int)probablyVisibleNodes.size() << " of " << pisSize << " nodes)" << endl;
122
123
124
125        //////////////
126        //-- part4: test probably visible nodes individually, update pvs of current view cell
127
128        if (!mViewCell) return;
129       
130        // query all nodes individually
131        BvhNodeContainer::const_iterator bit, bit_end = probablyVisibleNodes.end();
132
133        for (bit = probablyVisibleNodes.begin(); bit != bit_end; ++ bit)
134        {
135                BvhNode *n = *bit;
136
137                // node intersects near plane =>
138                // add immediately because testing the bb could fail
139                if (mBvh->IntersectsNearPlane(n))
140                {
141                        mViewCell->mPvs.AddEntry(mBvh, n);
142                }
143                else
144                {
145                        queries.push(IssueOcclusionQuery(n));
146                }
147        }
148
149        // get query results: if visible, add node to pvs
150        while (!queries.empty())
151        {
152                OcclusionQuery *q = queries.front();
153                queries.pop();
154       
155                const int visiblePixels = q->GetQueryResult();
156               
157                if (visiblePixels > 0)
158                {
159                        mViewCell->mPvs.AddEntry(mBvh, q->GetFrontNode());
160                }
161        }
162}
163
164
165
166}
Note: See TracBrowser for help on using the repository browser.