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