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