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