1 | #include "CHCTraverser.h"
|
---|
2 | #include "SceneEntity.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | namespace CHCDemoEngine
|
---|
6 | {
|
---|
7 |
|
---|
8 | CHCTraverser::CHCTraverser()
|
---|
9 | {}
|
---|
10 |
|
---|
11 |
|
---|
12 | void CHCTraverser::Traverse()
|
---|
13 | {
|
---|
14 | //-- PART 1: process finished occlusion queries
|
---|
15 | while (!mDistanceQueue.empty() || !mQueryQueue.empty())
|
---|
16 | {
|
---|
17 | while (!mQueryQueue.empty() &&
|
---|
18 | (mQueryQueue.front()->ResultAvailable() || mDistanceQueue.empty()))
|
---|
19 | {
|
---|
20 | OcclusionQuery *query = mQueryQueue.front();
|
---|
21 | mQueryQueue.pop();
|
---|
22 |
|
---|
23 | // wait until result available
|
---|
24 | int visiblePixels = query->GetQueryResult();
|
---|
25 |
|
---|
26 | if (visiblePixels > mVisibilityThreshold)
|
---|
27 | {
|
---|
28 | BvhNode *node = query->GetFrontNode();
|
---|
29 | node->SetAssumedVisibleFrameId(mFrameId + mAssumedVisibleFrames);
|
---|
30 |
|
---|
31 | node->SetVisible(true);
|
---|
32 | mBvh->MakeParentsVisible(node);
|
---|
33 | TraverseNode(node);
|
---|
34 | }
|
---|
35 | else
|
---|
36 | {
|
---|
37 |
|
---|
38 | ++ mStats.mNumQueryCulledNodes;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | //-- PART 2: hierarchical traversal
|
---|
43 | if (!mDistanceQueue.empty())
|
---|
44 | {
|
---|
45 | BvhNode *node = mDistanceQueue.top();
|
---|
46 | mDistanceQueue.pop();
|
---|
47 |
|
---|
48 | if (mBvh->IsWithinViewFrustum(node))
|
---|
49 | {
|
---|
50 | // for near plane intersecting bounding box possible
|
---|
51 | // wrong results => skip occlusion query
|
---|
52 | if (IntersectsNearPlane(node))
|
---|
53 | {
|
---|
54 | // update node's visited flag
|
---|
55 | node->SetLastVisitedFrame(mFrameId);
|
---|
56 | node->SetVisible(true);
|
---|
57 | mBvh->MakeParentsVisible(node);
|
---|
58 |
|
---|
59 | TraverseNode(node);
|
---|
60 | }
|
---|
61 | else
|
---|
62 | {
|
---|
63 | // identify previously visible nodes
|
---|
64 | const bool wasVisible = node->IsVisible() && (node->GetLastVisitedFrame() == mFrameId - 1);
|
---|
65 |
|
---|
66 | // identify nodes that we cannot skip queries for
|
---|
67 | const bool testFeasible = (!wasVisible ||
|
---|
68 | (node->IsVirtualLeaf() && (node->GetAssumedVisibleFrameId() <= mFrameId)));
|
---|
69 |
|
---|
70 | // update node's visited flag
|
---|
71 | node->SetLastVisitedFrame(mFrameId);
|
---|
72 |
|
---|
73 | // skip testing previously visible interior nodes
|
---|
74 | if (testFeasible)
|
---|
75 | {
|
---|
76 | OcclusionQuery *query = IssueOcclusionQuery(node, wasVisible);
|
---|
77 | mQueryQueue.push(query);
|
---|
78 | }
|
---|
79 | else
|
---|
80 | {
|
---|
81 | if (node->IsVirtualLeaf())
|
---|
82 | {
|
---|
83 | node->SetVisible(true);
|
---|
84 | mBvh->MakeParentsVisible(node);
|
---|
85 | }
|
---|
86 | else // reset visibility classification
|
---|
87 | {
|
---|
88 | node->SetVisible(false);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | // always traverse a node if it was visible
|
---|
94 | if (wasVisible)
|
---|
95 | TraverseNode(node);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | // for stats
|
---|
101 | ++ mStats.mNumFrustumCulledNodes;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | } |
---|