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