1 | #include "CoherentHierarchicalCullingManager.h" |
---|
2 | #include <OgreLogManager.h> |
---|
3 | |
---|
4 | namespace GtpVisibility { |
---|
5 | |
---|
6 | //----------------------------------------------------------------------- |
---|
7 | CoherentHierarchicalCullingManager::CoherentHierarchicalCullingManager() |
---|
8 | { |
---|
9 | SetAssumedVisibility(0); |
---|
10 | // initialise random generator in case we use assumed visibility |
---|
11 | srand(time(NULL)); |
---|
12 | } |
---|
13 | //----------------------------------------------------------------------- |
---|
14 | CoherentHierarchicalCullingManager::CoherentHierarchicalCullingManager(const unsigned int |
---|
15 | assumedVisibility) |
---|
16 | { |
---|
17 | SetAssumedVisibility(assumedVisibility); |
---|
18 | // initialise random generator in case we use assumed visibility |
---|
19 | srand(time(NULL)); |
---|
20 | } |
---|
21 | //----------------------------------------------------------------------- |
---|
22 | void CoherentHierarchicalCullingManager::RenderScene() |
---|
23 | { |
---|
24 | QueryQueue queryQueue; |
---|
25 | unsigned int visiblePixels = 0; |
---|
26 | bool isAvailable = false; |
---|
27 | //Ogre::LogManager::getSingleton().logMessage("Coherent Hierarchical Culling"); |
---|
28 | |
---|
29 | //-- PART 1: process finished occlusion queries |
---|
30 | while (!mHierarchyInterface->GetQueue()->empty() || !queryQueue.empty()) |
---|
31 | { |
---|
32 | // only wait for result if queue is empty |
---|
33 | while (!queryQueue.empty() && |
---|
34 | queryQueue.front().second->GetQueryResult(visiblePixels, |
---|
35 | mHierarchyInterface->GetQueue()->empty())) |
---|
36 | { |
---|
37 | HierarchyNode *node = queryQueue.front().first; |
---|
38 | |
---|
39 | queryQueue.pop(); |
---|
40 | |
---|
41 | if (visiblePixels > mVisibilityThreshold) |
---|
42 | { |
---|
43 | // in case geometry is in omterior node: ensure that we only traverse once |
---|
44 | if (!mHierarchyInterface->IsNodeVisible(node)) |
---|
45 | { |
---|
46 | mHierarchyInterface->TraverseNode(node); |
---|
47 | } |
---|
48 | |
---|
49 | mHierarchyInterface->PullUpVisibility(node); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | mHierarchyInterface->SetNodeVisible(node, false); |
---|
54 | |
---|
55 | ++ mNumQueryCulledNodes; |
---|
56 | |
---|
57 | if (mVisualizeCulledNodes) |
---|
58 | { |
---|
59 | mHierarchyInterface->VisualizeCulledNode(node, QUERY_CULLED); |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | //-- PART 2: hierarchical traversal |
---|
65 | if (!mHierarchyInterface->GetQueue()->empty()) |
---|
66 | { |
---|
67 | HierarchyNode *node = mHierarchyInterface->GetQueue()->top(); |
---|
68 | mHierarchyInterface->GetQueue()->pop(); |
---|
69 | |
---|
70 | bool intersects = false; |
---|
71 | |
---|
72 | if (!mHierarchyInterface->CheckFrustumVisible(node, intersects)) |
---|
73 | { |
---|
74 | ++ mNumFrustumCulledNodes; |
---|
75 | |
---|
76 | if (mVisualizeCulledNodes) |
---|
77 | { |
---|
78 | mHierarchyInterface->VisualizeCulledNode(node, FRUSTUM_CULLED); |
---|
79 | } |
---|
80 | } |
---|
81 | // -- if node intersects near plane, skip query because wrong results possible |
---|
82 | else if (intersects) |
---|
83 | { |
---|
84 | SkipQuery(node); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | // identify previously visible nodes |
---|
89 | bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && |
---|
90 | (mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1); |
---|
91 | |
---|
92 | // if we assume node to be visible in this frame => skip query |
---|
93 | bool skipQuery = wasVisible && (mAssumedVisibility > 0) && |
---|
94 | DecideVisible(node) && mHierarchyInterface->HasGeometry(node); |
---|
95 | |
---|
96 | //if (mHierarchyInterface->LastVisited(node) >= mHierarchyInterface->GetFrameId()) |
---|
97 | // Ogre::LogManager::getSingleton().logMessage("error"); |
---|
98 | |
---|
99 | if (skipQuery) |
---|
100 | { |
---|
101 | SkipQuery(node); |
---|
102 | continue; |
---|
103 | } |
---|
104 | |
---|
105 | // identify nodes that we cannot skip queries for |
---|
106 | // geometry not only in leaves => test for renderable geometry |
---|
107 | bool issueQuery = !wasVisible || mHierarchyInterface->HasGeometry(node); |
---|
108 | |
---|
109 | // reset node's visibility classification |
---|
110 | // set visibe if geometry in node so we only traverse once |
---|
111 | mHierarchyInterface->SetNodeVisible(node, wasVisible && issueQuery); |
---|
112 | |
---|
113 | // update node's visited flag |
---|
114 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
115 | |
---|
116 | // skip testing previously visible nodes without geometry |
---|
117 | if (issueQuery) |
---|
118 | { |
---|
119 | ++ mNumQueriesIssued; |
---|
120 | |
---|
121 | queryQueue.push(QueryPair(node, mHierarchyInterface-> |
---|
122 | IssueNodeOcclusionQuery(node, wasVisible))); |
---|
123 | } |
---|
124 | |
---|
125 | // always traverse a node if it was visible |
---|
126 | if (wasVisible) |
---|
127 | { |
---|
128 | mHierarchyInterface->TraverseNode(node); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | //----------------------------------------------------------------------- |
---|
135 | void CoherentHierarchicalCullingManager::SetAssumedVisibility(const unsigned int assumedVisibility) |
---|
136 | { |
---|
137 | mAssumedVisibility = assumedVisibility; |
---|
138 | |
---|
139 | mThreshold = 0; |
---|
140 | |
---|
141 | if (mAssumedVisibility > 0) |
---|
142 | { |
---|
143 | mThreshold = RAND_MAX - RAND_MAX / mAssumedVisibility; |
---|
144 | } |
---|
145 | //std::stringstream d; d << "*** setting assumed vis: " << mAssumedVisibility; Ogre::LogManager::getSingleton().logMessage(d.str()); |
---|
146 | } |
---|
147 | //----------------------------------------------------------------------- |
---|
148 | inline bool CoherentHierarchicalCullingManager::DecideVisible(HierarchyNode *node) const |
---|
149 | { |
---|
150 | //bool result = rand() < mThreshold; std::stringstream d; d << "Assumed vis: " << mAssumedVisibility << ", result: " << result; |
---|
151 | //Ogre::LogManager::getSingleton().logMessage(d.str()); return result; |
---|
152 | |
---|
153 | return rand() < mThreshold; |
---|
154 | } |
---|
155 | //----------------------------------------------------------------------- |
---|
156 | inline void CoherentHierarchicalCullingManager::SkipQuery(HierarchyNode *node) const |
---|
157 | { |
---|
158 | // -- set node to be visible in this frame, then traverse it |
---|
159 | mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId()); |
---|
160 | |
---|
161 | mHierarchyInterface->PullUpVisibility(node); |
---|
162 | mHierarchyInterface->TraverseNode(node); |
---|
163 | } |
---|
164 | |
---|
165 | } // namespace GtpVisibility |
---|