1 | #include "CHCTraverser.h"
|
---|
2 | #include "SceneEntity.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | namespace CHCDemo
|
---|
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 |
|
---|
30 | mBvh->MakeParentsVisible(node);
|
---|
31 | TraverseNode(node);
|
---|
32 | }
|
---|
33 | else
|
---|
34 | {
|
---|
35 | ++ mStats.mNumQueryCulledNodes;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | //-- PART 2: hierarchical traversal
|
---|
40 | if (!mDistanceQueue.empty())
|
---|
41 | {
|
---|
42 | BvhNode *node = mDistanceQueue.top();
|
---|
43 | mDistanceQueue.pop();
|
---|
44 |
|
---|
45 | if (mBvh->IsWithinViewFrustum(node))
|
---|
46 | {
|
---|
47 | // for near plane intersecting bounding box possible
|
---|
48 | // wrong results => skip occlusion query
|
---|
49 | if (IntersectsNearPlane(node))
|
---|
50 | {
|
---|
51 | // update node's visited flag
|
---|
52 | node->SetLastVisitedFrame(mFrameId);
|
---|
53 | node->SetVisible(true);
|
---|
54 | mBvh->MakeParentsVisible(node);
|
---|
55 |
|
---|
56 | TraverseNode(node);
|
---|
57 | }
|
---|
58 | else
|
---|
59 | {
|
---|
60 | // identify previously visible nodes
|
---|
61 | const bool wasVisible = node->IsVisible() && (node->GetLastVisitedFrame() == mFrameId - 1);
|
---|
62 |
|
---|
63 | // identify nodes that we cannot skip queries for
|
---|
64 | const bool leafOrWasInvisible =
|
---|
65 | (node->GetLastVisitedFrame() != mFrameId) && (!wasVisible || node->IsVirtualLeaf());
|
---|
66 |
|
---|
67 | // reset node's visibility classification
|
---|
68 | node->SetVisible(false);
|
---|
69 |
|
---|
70 | // update node's visited flag
|
---|
71 | node->SetLastVisitedFrame(mFrameId);
|
---|
72 |
|
---|
73 | // skip testing previously visible interior nodes
|
---|
74 | if (leafOrWasInvisible)
|
---|
75 | {
|
---|
76 | OcclusionQuery *query = IssueOcclusionQuery(node, wasVisible);
|
---|
77 | mQueryQueue.push(query);
|
---|
78 | }
|
---|
79 |
|
---|
80 | // always traverse a node if it was visible
|
---|
81 | if (wasVisible)
|
---|
82 | TraverseNode(node);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | // for stats
|
---|
88 | ++ mStats.mNumFrustumCulledNodes;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | } |
---|