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 | node->SetVisible(true);
|
---|
31 | mBvh->MakeParentsVisible(node);
|
---|
32 | TraverseNode(node);
|
---|
33 | }
|
---|
34 | else
|
---|
35 | {
|
---|
36 | ++ mStats.mNumQueryCulledNodes;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | //-- PART 2: hierarchical traversal
|
---|
41 | if (!mDistanceQueue.empty())
|
---|
42 | {
|
---|
43 | BvhNode *node = mDistanceQueue.top();
|
---|
44 | mDistanceQueue.pop();
|
---|
45 |
|
---|
46 | if (mBvh->IsWithinViewFrustum(node))
|
---|
47 | {
|
---|
48 | // for near plane intersecting bounding box possible
|
---|
49 | // wrong results => skip occlusion query
|
---|
50 | if (IntersectsNearPlane(node))
|
---|
51 | {
|
---|
52 | // update node's visited flag
|
---|
53 | node->SetLastVisitedFrame(mFrameId);
|
---|
54 | node->SetVisible(true);
|
---|
55 | mBvh->MakeParentsVisible(node);
|
---|
56 |
|
---|
57 | TraverseNode(node);
|
---|
58 | }
|
---|
59 | else
|
---|
60 | {
|
---|
61 | // identify previously visible nodes
|
---|
62 | const bool wasVisible = node->IsVisible() && (node->GetLastVisitedFrame() == mFrameId - 1);
|
---|
63 |
|
---|
64 | // identify nodes that we cannot skip queries for
|
---|
65 | const bool leafOrWasInvisible = (!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 | } |
---|