1 | #include "CHCTraverser.h"
|
---|
2 | #include "SceneEntity.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | namespace CHCDemoEngine
|
---|
6 | {
|
---|
7 |
|
---|
8 | CHCTraverser::CHCTraverser()
|
---|
9 | {}
|
---|
10 |
|
---|
11 |
|
---|
12 | OcclusionQuery *CHCTraverser::IssueOcclusionQueryWithGeometry(BvhNode *node)
|
---|
13 | {
|
---|
14 | // render out what is left in the qeue
|
---|
15 | if (mUseRenderQueue) ApplyRenderQueue();
|
---|
16 |
|
---|
17 | OcclusionQuery *query = mQueryHandler.RequestQuery();
|
---|
18 | query->AddNode(node);
|
---|
19 |
|
---|
20 | ++ mStats.mNumIssuedQueries;
|
---|
21 |
|
---|
22 | query->BeginQuery();
|
---|
23 |
|
---|
24 | RenderNode(node);
|
---|
25 | if (mUseRenderQueue) ApplyRenderQueue();
|
---|
26 |
|
---|
27 | query->EndQuery();
|
---|
28 |
|
---|
29 | return query;
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | void CHCTraverser::Traverse()
|
---|
34 | {
|
---|
35 | //-- PART 1: process finished occlusion queries
|
---|
36 | while (!mDistanceQueue.empty() || !mQueryQueue.empty())
|
---|
37 | {
|
---|
38 | while (!mQueryQueue.empty() &&
|
---|
39 | (mQueryQueue.front()->ResultAvailable() || mDistanceQueue.empty()))
|
---|
40 | {
|
---|
41 | OcclusionQuery *query = mQueryQueue.front();
|
---|
42 | mQueryQueue.pop();
|
---|
43 |
|
---|
44 | // wait until result available
|
---|
45 | int visiblePixels = query->GetQueryResult();
|
---|
46 |
|
---|
47 | if (visiblePixels > mVisibilityThreshold)
|
---|
48 | {
|
---|
49 | BvhNode *node = query->GetFrontNode();
|
---|
50 | node->SetAssumedVisibleFrameId(mFrameId + mAssumedVisibleFrames);
|
---|
51 |
|
---|
52 | node->SetVisible(true);
|
---|
53 | mBvh->MakeParentsVisible(node);
|
---|
54 | TraverseNode(node);
|
---|
55 | }
|
---|
56 | else
|
---|
57 | {
|
---|
58 |
|
---|
59 | ++ mStats.mNumQueryCulledNodes;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | //-- PART 2: hierarchical traversal
|
---|
64 | if (!mDistanceQueue.empty())
|
---|
65 | {
|
---|
66 | BvhNode *node = mDistanceQueue.top();
|
---|
67 | mDistanceQueue.pop();
|
---|
68 |
|
---|
69 | if (mBvh->IsWithinViewFrustum(node))
|
---|
70 | {
|
---|
71 | // for near plane intersecting bounding box possible
|
---|
72 | // wrong results => skip occlusion query
|
---|
73 | if (IntersectsNearPlane(node))
|
---|
74 | {
|
---|
75 | // update node's visited flag
|
---|
76 | node->SetLastVisitedFrame(mFrameId);
|
---|
77 | node->SetVisible(true);
|
---|
78 | mBvh->MakeParentsVisible(node);
|
---|
79 |
|
---|
80 | TraverseNode(node);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | // identify previously visible nodes
|
---|
85 | const bool wasVisible = node->IsVisible() && (node->GetLastVisitedFrame() == mFrameId - 1);
|
---|
86 |
|
---|
87 | // identify nodes that we cannot skip queries for
|
---|
88 | const bool queryFeasible = (!wasVisible ||
|
---|
89 | (node->IsVirtualLeaf() && (node->GetAssumedVisibleFrameId() <= mFrameId)));
|
---|
90 |
|
---|
91 | // update node's visited flag
|
---|
92 | node->SetLastVisitedFrame(mFrameId);
|
---|
93 |
|
---|
94 | // skip testing previously visible interior nodes
|
---|
95 | if (queryFeasible)
|
---|
96 | {
|
---|
97 | OcclusionQuery *query;
|
---|
98 |
|
---|
99 | // if this node is a previous visible leaf:
|
---|
100 | // leaves will be rendered anyway => we can directly query the real geometry
|
---|
101 | if (wasVisible && mUseOptimization)
|
---|
102 | query = IssueOcclusionQueryWithGeometry(node);
|
---|
103 | else
|
---|
104 | query = IssueOcclusionQuery(node);
|
---|
105 |
|
---|
106 | mQueryQueue.push(query);
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | if (node->IsVirtualLeaf())
|
---|
111 | {
|
---|
112 | node->SetVisible(true);
|
---|
113 | mBvh->MakeParentsVisible(node);
|
---|
114 | }
|
---|
115 | else // reset visibility classification
|
---|
116 | {
|
---|
117 | node->SetVisible(false);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | // always traverse a node if it was visible
|
---|
122 | if (wasVisible)
|
---|
123 | TraverseNode(node);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | // for stats
|
---|
129 | ++ mStats.mNumFrustumCulledNodes;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | } |
---|