[3258] | 1 | #include "PvsCollectionRenderer.h"
|
---|
| 2 | #include "ViewCellsTree.h"
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | using namespace std;
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | namespace CHCDemoEngine
|
---|
| 9 | {
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | PvsCollectionRenderer::PvsCollectionRenderer():
|
---|
| 13 | RenderTraverser(), mViewCell(NULL)
|
---|
| 14 | {
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | PvsCollectionRenderer::~PvsCollectionRenderer()
|
---|
| 19 | {
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | void PvsCollectionRenderer::Traverse()
|
---|
| 24 | {
|
---|
[3259] | 25 | mPIS.clear();
|
---|
| 26 |
|
---|
[3258] | 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 |
|
---|
[3259] | 53 | int pisSize = (int)mPIS.size();
|
---|
| 54 |
|
---|
| 55 |
|
---|
[3258] | 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();
|
---|
[3259] | 64 | query->Reset();
|
---|
| 65 |
|
---|
[3258] | 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 |
|
---|
[3259] | 95 | int qsize = (int)queries.size();
|
---|
| 96 | int fqueries = 0;
|
---|
| 97 |
|
---|
[3258] | 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 | {
|
---|
[3259] | 108 | ++ fqueries;
|
---|
| 109 |
|
---|
[3258] | 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 |
|
---|
[3259] | 121 | //cout << "probably visible: " << fqueries << " of " << qsize << " (" << (int)probablyVisibleNodes.size() << " of " << pisSize << " nodes)" << endl;
|
---|
[3258] | 122 |
|
---|
[3259] | 123 |
|
---|
| 124 |
|
---|
[3258] | 125 | //////////////
|
---|
| 126 | //-- part4: test probably visible nodes individually, update pvs of current view cell
|
---|
| 127 |
|
---|
[3259] | 128 | if (!mViewCell) return;
|
---|
[3258] | 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;
|
---|
[3259] | 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 | }
|
---|
[3258] | 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();
|
---|
[3259] | 156 |
|
---|
[3258] | 157 | if (visiblePixels > 0)
|
---|
| 158 | {
|
---|
[3259] | 159 | mViewCell->mPvs.AddEntry(mBvh, q->GetFrontNode());
|
---|
[3258] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | }
|
---|