Changeset 276
- Timestamp:
- 10/11/06 17:13:30 (2 years ago)
- Files:
-
- branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGOcclusionCullingTreeBuilder.cpp (modified) (6 diffs)
- branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGOcclusionCullingTreeBuilder.h (modified) (2 diffs)
- branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGRenderTreeNode.cpp (modified) (1 diff)
- branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGRenderTreeNode.h (modified) (2 diffs)
- branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGRenderTreeNode.inl (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGOcclusionCullingTreeBuilder.cpp
r273 r276 39 39 #include <stdlib.h> 40 40 #include <stdio.h> 41 #include <math.h> 41 42 42 43 #include "OSGConfig.h" … … 93 94 94 95 OcclusionCullingTreeBuilder::OcclusionCullingTreeBuilder(void) 95 : uNumNodes (0) 96 : uNumNodes (0), 97 _isOccSetup(false), 98 _currSample(0) 96 99 { 97 100 _extOcclusionQuery = Window::registerExtension("GL_ARB_occlusion_query"); … … 134 137 _uiActiveMatrix = 0; 135 138 Real32 screen_covered_percentage = 0.f; 136 UInt32 current_node = 1; 137 GLuint queries[uNumNodes]; 138 //std::cout << "Submitting " << uNumNodes << " queries" << std::endl; 139 140 GenQueryT genquer = (GenQueryT)win->getFunction(_funcGenQueriesARB); 141 genquer(uNumNodes, queries); 139 if(!_isOccSetup) 140 { 141 _testSamples = new GLuint[uNumNodes]; 142 //std::cout << "Submitting " << uNumNodes << " queries" << std::endl; 143 144 GenQueryT genquer = (GenQueryT)win->getFunction(_funcGenQueriesARB); 145 genquer(uNumNodes, _testSamples); 146 } 142 147 143 148 //glGenQueriesARB(uNumNodes, queries); … … 145 150 146 151 //DRAW / TEST / RE-DRAW ON BUFFER FULL 147 drawNode(_pRoot, denv, part, screen_covered_percentage, current_node, queries); 152 testNode(_pRoot, denv, part, screen_covered_percentage); 153 uNumNodes=0; 154 _uiActiveMatrix = 0; 155 drawTestResults(_pRoot, denv, part); 148 156 //RESULTS / RE-DRAW 149 157 150 158 //std::cout << "Calc Pixels" << std::endl; 151 159 152 GetQueryObjectuivT getquiv = 153 (GetQueryObjectuivT)win->getFunction(_funcGetQueryObjectuivARB); 160 154 161 155 162 StatCollector *sc = denv.getRTAction()->getStatistics(); 156 sc->getElem(statNOccTests )->set( uNumNodes);157 sc->getElem(statNOccInvisible)->set( 0); // Just create it163 sc->getElem(statNOccTests )->set(_currSample); 164 sc->getElem(statNOccInvisible)->set(uNumNodes); // Just create it 158 165 159 166 //std::cout << std::endl; 160 167 //std::cout << "Real pixels " << std::endl; 161 for(UInt32 i = 1; i<uNumNodes; i++) 162 { 163 GLuint available = 0; 164 getquiv(queries[i], GL_QUERY_RESULT_AVAILABLE_ARB, &available); 165 /* 166 if (!available) 167 { 168 std::cout << std::endl << "Waiting on " << i << "th query"; 169 } 170 */ 171 while (!available) 172 { 173 //std::cout << "."; 174 getquiv(queries[i], GL_QUERY_RESULT_AVAILABLE_ARB, &available); 175 //glGetQueryObjectivARB(queries[i], GL_QUERY_RESULT_AVAILABLE_ARB, &available); 176 /* 177 if(available) 178 { 179 std::cout << std::endl; 180 } 181 */ 182 } 183 184 GLuint sampleCount = 0; 185 getquiv(queries[i], GL_QUERY_RESULT_ARB, &sampleCount); 186 //glGetQueryObjectuivARB(queries[i], GL_QUERY_RESULT_ARB, &sampleCount); 187 //std::cout << i << ":" << sampleCount << " "; 188 189 if(sampleCount == 0) 190 sc->getElem(statNOccInvisible)->inc(); 191 } 192 //std::cout << std::endl; 168 //std::cout << " Next" << std::endl; 193 169 194 170 // screen_covered_percentage = 1.0; 195 171 // drawNode(_pRoot, denv, part, screen_covered_percentage); 196 172 uNumNodes=0; 197 } 198 199 void OcclusionCullingTreeBuilder::drawNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part, Real32 &cscr, UInt32 &cur_node, GLuint queries[]) 173 _currSample = 0; 174 175 } 176 void OcclusionCullingTreeBuilder::testNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part, Real32 &scr_percent) 200 177 { 201 178 while (pNode != NULL) 202 179 { 203 180 //MATRIX SETUP 181 UInt32 uiNextMatrix = pNode->getMatrixStore().first; 182 183 if(uiNextMatrix != 0 && uiNextMatrix != _uiActiveMatrix) 184 { 185 glLoadMatrixf(pNode->getMatrixStore().second.getValues()); 186 187 _uiActiveMatrix = uiNextMatrix; 188 189 _currMatrix.second = pNode->getMatrixStore().second; 190 191 updateTopMatrix(denv); 192 193 denv.setObjectToWorld(_accMatrix); 194 195 ++part->_uiNumMatrixChanges; 196 } 197 204 198 Viewport* vp = denv.getViewport(); 205 199 Int32 pix_width = vp->getPixelWidth(); … … 208 202 Matrix worldToCam; 209 203 vp->getCamera()->getWorldToScreen(worldToCam, *vp); 210 211 212 UInt32 uiNextMatrix = pNode->getMatrixStore().first;213 214 if(uiNextMatrix != 0 && uiNextMatrix != _uiActiveMatrix)215 {216 glLoadMatrixf(pNode->getMatrixStore().second.getValues());217 218 _uiActiveMatrix = uiNextMatrix;219 220 _currMatrix.second = pNode->getMatrixStore().second;221 222 updateTopMatrix(denv);223 224 denv.setObjectToWorld(_accMatrix);225 226 ++part->_uiNumMatrixChanges;227 228 // Negative scaled matrices in conjunction with double sided229 // lighting230 // gives wrong render results cause the lighting itselfs gets231 // inverted. This corrects this behavior.232 233 // DRAW SPECIFIC234 if(part->_bCorrectTwoSidedLighting)235 {236 const Matrix &m = _currMatrix.second;237 238 // test for a "flipped" matrix239 // glFrontFace can give conflicts with the polygon chunk ...240 241 if(m[0].cross(m[1]).dot(m[2]) < 0.0)242 {243 glFrontFace(GL_CW);244 }245 else246 {247 glFrontFace(GL_CCW);248 }249 }250 }251 204 DynamicVolume volume = pNode->getVol(); 252 205 Pnt3r min,max; … … 299 252 Real32 pix; 300 253 pix = (max[0] - min[0]) * (max[1] - min[1]) * ((pix_width/2.0f)*(pix_height/2.0f)); 254 255 Real32 cbb = pix / (pix_width * pix_height); 301 256 302 257 //std::cout << cur_node << ":" << pix << " "; 303 258 //std::cout << pNode->getScalar() << std::endl; 304 259 305 //STATE ACTIVATION 306 State *pNewState = pNode->getState(); 307 StateOverride *pNewStateOverride = pNode->getStateOverride(); 308 309 denv.activateState(pNewState, pNewStateOverride); 310 311 //DRAW DRAW DRAW 312 Window* win = denv.getWindow(); 313 314 //glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queries[cur_node]); 315 if(pNode->hasFunctor() == true) 316 { 317 BeginQueryT beginq = (BeginQueryT) win->getFunction(_funcBeginQueryARB); 318 beginq(GL_SAMPLES_PASSED_ARB, queries[cur_node]); 319 if(0 == 1) 320 { 321 //glPushMatrix(); 322 //glLoadIdentity(); 323 //glMatrixMode(GL_PROJECTION); 324 //GLfloat m[16]; 325 //glGetFloatv(GL_PROJECTION_MATRIX, m); 326 //glLoadIdentity(); 327 //glOrtho(-1,1,-1,1,-1,1); 328 329 330 volume.getBounds(min, max); 331 static GLdouble n[6][3] = { 332 {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, 333 {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} 334 }; 335 static GLint faces[6][4] = { 336 { 0, 1, 2, 3}, { 3, 2, 6, 7}, { 7, 6, 5, 4}, 337 { 4, 5, 1, 0}, { 5, 6, 2, 1}, { 7, 4, 0, 3} 338 }; 339 340 GLdouble v[8][3]; 341 v[0][0] = v[1][0] = v[2][0] = v[3][0] = min[0]; 342 v[4][0] = v[5][0] = v[6][0] = v[7][0] = max[0]; 343 v[0][1] = v[1][1] = v[4][1] = v[5][1] = min[1]; 344 v[2][1] = v[3][1] = v[6][1] = v[7][1] = max[1]; 345 v[0][2] = v[3][2] = v[4][2] = v[7][2] = min[2]; 346 v[1][2] = v[2][2] = v[5][2] = v[6][2] = max[2]; 347 glColor3f(0.3,0.4,0.6); 348 for(UInt32 i = 0; i<6; i++) 349 { 350 glBegin(GL_QUADS); 351 glNormal3dv(&n[i][0]); 352 glVertex3dv(&v[faces[i][0]][0]); 353 glNormal3dv(&n[i][0]); 354 glVertex3dv(&v[faces[i][1]][0]); 355 glNormal3dv(&n[i][0]); 356 glVertex3dv(&v[faces[i][2]][0]); 357 glNormal3dv(&n[i][0]); 358 glVertex3dv(&v[faces[i][3]][0]); 359 glEnd(); 360 } 361 //glLoadMatrixf(m); 362 //glMatrixMode(GL_MODELVIEW); 363 //glPopMatrix(); 364 } 365 else if ( 9 == 9 ) 366 { 367 drawVolume(volume); 368 pNode->getFunctor()(&denv); 369 } 370 371 EndQueryT endq = (EndQueryT) win->getFunction(_funcEndQueryARB); 372 endq(GL_SAMPLES_PASSED_ARB); 373 //glEndQueryARB(GL_SAMPLES_PASSED_ARB); 374 cur_node++; 375 } 376 377 260 //Make decision 261 if(pNode->hasFunctor() == false) 262 { 263 //renderNode 264 drawNode(pNode, denv, part); 265 } 266 else 267 { 268 //make decision 269 //if(0 > 1) 270 if(cbb > scr_percent || cbb > 0.01) 271 { 272 drawNode(pNode, denv, part); 273 scr_percent+=cbb; 274 } 275 else 276 { 277 Real32 pcov; 278 pcov = sqrt(scr_percent) - sqrt(cbb); 279 pcov = pcov * pcov; 280 //std::cout << "cbb:" << cbb << " scr_percent:" << scr_percent <<" pcov:" << pcov << std::endl; 281 if(pcov < scr_percent) 282 { 283 drawTestNode(pNode, denv, part); 284 } 285 else 286 { 287 drawNode(pNode, denv, part); 288 } 289 scr_percent+=cbb; 290 } 291 292 } 378 293 379 294 //DRAW CHILDREN OR GO TO TOP AND DO IT AGAIN 380 295 if(pNode->getFirstChild() != NULL) 381 296 { 382 drawNode(pNode->getFirstChild(), denv, part, cscr, cur_node, queries); 297 testNode(pNode->getFirstChild(), denv, part, scr_percent); 298 } 299 300 pNode = pNode->getBrother(); 301 } 302 } 303 304 void OcclusionCullingTreeBuilder::drawTestNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part) 305 { 306 //DRAW DRAW DRAW 307 Window* win = denv.getWindow(); 308 pNode->setIsRendered(false); 309 310 //glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queries[cur_node]); 311 //glPushMatrix(); 312 //glLoadIdentity(); 313 //glMatrixMode(GL_PROJECTION); 314 //GLfloat m[16]; 315 //glGetFloatv(GL_PROJECTION_MATRIX, m); 316 //glLoadIdentity(); 317 //glOrtho(-1,1,-1,1,-1,1); 318 319 DynamicVolume volume = pNode->getVol(); 320 Pnt3r min,max; 321 volume.getBounds(min, max); 322 static GLdouble n[6][3] = { 323 {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, 324 {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} 325 }; 326 static GLint faces[6][4] = { 327 { 0, 1, 2, 3}, { 3, 2, 6, 7}, { 7, 6, 5, 4}, 328 { 4, 5, 1, 0}, { 5, 6, 2, 1}, { 7, 4, 0, 3} 329 }; 330 331 GLdouble v[8][3]; 332 v[0][0] = v[1][0] = v[2][0] = v[3][0] = min[0]; 333 v[4][0] = v[5][0] = v[6][0] = v[7][0] = max[0]; 334 v[0][1] = v[1][1] = v[4][1] = v[5][1] = min[1]; 335 v[2][1] = v[3][1] = v[6][1] = v[7][1] = max[1]; 336 v[0][2] = v[3][2] = v[4][2] = v[7][2] = min[2]; 337 v[1][2] = v[2][2] = v[5][2] = v[6][2] = max[2]; 338 339 //glColor3f(0.3,0.4,0.6); 340 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 341 glDepthMask(GL_FALSE); 342 343 BeginQueryT beginq = (BeginQueryT) win->getFunction(_funcBeginQueryARB); 344 _currSample++; 345 pNode->setResultNum(_currSample); 346 beginq(GL_SAMPLES_PASSED_ARB, _testSamples[_currSample]); 347 for(UInt32 i = 0; i<6; i++) 348 { 349 glBegin(GL_QUADS); 350 glNormal3dv(&n[i][0]); 351 glVertex3dv(&v[faces[i][0]][0]); 352 glNormal3dv(&n[i][0]); 353 glVertex3dv(&v[faces[i][1]][0]); 354 glNormal3dv(&n[i][0]); 355 glVertex3dv(&v[faces[i][2]][0]); 356 glNormal3dv(&n[i][0]); 357 glVertex3dv(&v[faces[i][3]][0]); 358 glEnd(); 359 } 360 //glLoadMatrixf(m); 361 //glMatrixMode(GL_MODELVIEW); 362 //glPopMatrix(); 363 364 EndQueryT endq = (EndQueryT) win->getFunction(_funcEndQueryARB); 365 endq(GL_SAMPLES_PASSED_ARB); 366 //glEndQueryARB(GL_SAMPLES_PASSED_ARB); 367 368 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 369 glDepthMask(GL_TRUE); 370 } 371 372 void OcclusionCullingTreeBuilder::drawNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part) 373 { 374 375 //STATE ACTIVATION 376 State *pNewState = pNode->getState(); 377 StateOverride *pNewStateOverride = pNode->getStateOverride(); 378 379 denv.activateState(pNewState, pNewStateOverride); 380 pNode->setIsRendered(true); 381 382 //DRAW DRAW DRAW 383 384 if(pNode->hasFunctor() == true) 385 { 386 if(part->_bCorrectTwoSidedLighting) 387 { 388 const Matrix &m = _currMatrix.second; 389 390 // test for a "flipped" matrix 391 // glFrontFace can give conflicts with the polygon chunk ... 392 393 if(m[0].cross(m[1]).dot(m[2]) < 0.0) 394 { 395 glFrontFace(GL_CW); 396 } 397 else 398 { 399 glFrontFace(GL_CCW); 400 } 401 } 402 403 pNode->getFunctor()(&denv); 404 } 405 } 406 407 void OcclusionCullingTreeBuilder::drawTestResults(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part) 408 { 409 while (pNode != NULL) 410 { 411 //MATRIX SETUP 412 UInt32 uiNextMatrix = pNode->getMatrixStore().first; 413 414 if(uiNextMatrix != 0 && uiNextMatrix != _uiActiveMatrix) 415 { 416 glLoadMatrixf(pNode->getMatrixStore().second.getValues()); 417 418 _uiActiveMatrix = uiNextMatrix; 419 420 _currMatrix.second = pNode->getMatrixStore().second; 421 422 updateTopMatrix(denv); 423 424 denv.setObjectToWorld(_accMatrix); 425 426 ++part->_uiNumMatrixChanges; 427 } 428 429 //DRAW DRAW DRAW 430 if(pNode->hasFunctor() == true && !pNode->getIsRendered()) 431 { 432 Window* win = denv.getWindow(); 433 GetQueryObjectuivT getquiv = 434 (GetQueryObjectuivT)win->getFunction(_funcGetQueryObjectuivARB); 435 GLuint available = 0; 436 getquiv(_testSamples[pNode->getResultNum()], GL_QUERY_RESULT_AVAILABLE_ARB, &available); 437 /* 438 if (!available) 439 { 440 std::cout << std::endl << "Waiting on " << i << "th query"; 441 } 442 */ 443 while (!available) 444 { 445 //std::cout << "."; 446 getquiv(_testSamples[pNode->getResultNum()], GL_QUERY_RESULT_AVAILABLE_ARB, &available); 447 //glGetQueryObjectivARB(queries[i], GL_QUERY_RESULT_AVAILABLE_ARB, &available); 448 /* 449 if(available) 450 { 451 std::cout << std::endl; 452 } 453 */ 454 } 455 456 GLuint sampleCount = 0; 457 getquiv(_testSamples[pNode->getResultNum()], GL_QUERY_RESULT_ARB, &sampleCount); 458 //glGetQueryObjectuivARB(queries[i], GL_QUERY_RESULT_ARB, &sampleCount); 459 //std::cout << i << ":" << sampleCount << " "; 460 if(sampleCount > 0) 461 { 462 // Negative scaled matrices in conjunction with double sided 463 // lighting 464 // gives wrong render results cause the lighting itselfs gets 465 // inverted. This corrects this behavior. 466 if(part->_bCorrectTwoSidedLighting) 467 { 468 const Matrix &m = _currMatrix.second; 469 470 // test for a "flipped" matrix 471 // glFrontFace can give conflicts with the polygon chunk ... 472 473 if(m[0].cross(m[1]).dot(m[2]) < 0.0) 474 { 475 glFrontFace(GL_CW); 476 } 477 else 478 { 479 glFrontFace(GL_CCW); 480 } 481 } 482 //STATE ACTIVATION 483 State *pNewState = pNode->getState(); 484 StateOverride *pNewStateOverride = pNode->getStateOverride(); 485 486 denv.activateState(pNewState, pNewStateOverride); 487 pNode->getFunctor()(&denv); 488 } 489 else 490 { 491 uNumNodes++; 492 } 493 } 494 495 496 497 //DRAW CHILDREN OR GO TO TOP AND DO IT AGAIN 498 if(pNode->getFirstChild() != NULL) 499 { 500 drawTestResults(pNode->getFirstChild(), denv, part); 383 501 } 384 502 branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGOcclusionCullingTreeBuilder.h
r268 r276 98 98 //----------------------------------------------------------------------- 99 99 virtual void draw(DrawEnv &denv, RenderPartition *part); 100 void drawNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part, Real32 &cscr, UInt32 &cur_node, GLuint queries[]); 100 101 void testNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part, Real32 &scr_percent); 102 void drawNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part); 103 void drawTestNode(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part); 104 void drawTestResults(RenderTreeNode *pNode, DrawEnv &denv, RenderPartition *part); 101 105 102 106 //----------------------------------------------------------------------- … … 181 185 static StatElemDesc<StatIntElem> statNOccTests; 182 186 static StatElemDesc<StatIntElem> statNOccInvisible; 187 188 GLuint* _testSamples; 189 bool _isOccSetup; 190 UInt32 _currSample; //Last current sample test number 183 191 184 192 branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGRenderTreeNode.cpp
r271 r276 66 66 _oMatrixStore ( ), 67 67 _rScalarVal (0.f ), 68 _isRendered (false), 69 _resultNum (0), 68 70 _lightsState (0 ) 69 71 { branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGRenderTreeNode.h
r271 r276 104 104 DynamicVolume getVol (void ); 105 105 106 void setIsRendered (bool bRendered ); 107 bool getIsRendered (void ); 108 109 void setResultNum (UInt32 uiResNum ); 110 UInt32 getResultNum (void ); 111 106 112 void setLightsState (UInt64 state ); 107 113 UInt64 getLightsState (void ); … … 134 140 135 141 DynamicVolume _dVol; 142 143 bool _isRendered; 144 145 UInt32 _resultNum; 136 146 137 147 UInt64 _lightsState; branches/Dirk_RenderTraversalWork/Source/System/Action/RenderTraversal/OSGRenderTreeNode.inl
r271 r276 205 205 return _dVol; 206 206 } 207 208 inline 209 void RenderTreeNode::setResultNum(UInt32 uiResNum) 210 { 211 _resultNum = uiResNum; 212 } 213 214 inline 215 UInt32 RenderTreeNode::getResultNum(void) 216 { 217 return _resultNum; 218 } 219 220 inline 221 void RenderTreeNode::setIsRendered(bool bRendered) 222 { 223 _isRendered = bRendered; 224 } 225 226 inline 227 bool RenderTreeNode::getIsRendered(void) 228 { 229 return _isRendered; 230 } 231 207 232 208 233 inline
