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