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