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