Changeset 13041 for OpenSceneGraph/trunk/src/osgShadow/OccluderGeometry.cpp
- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgShadow/OccluderGeometry.cpp
r11478 r13041 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 5 * (at your option) any later version. The full license is in LICENSE file 6 6 * included with this distribution, and on the openscenegraph.org website. 7 * 7 * 8 8 * This library is distributed in the hope that it will be useful, 9 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 11 * OpenSceneGraph Public License for more details. 12 12 */ … … 34 34 osg::Drawable(oc,copyop) 35 35 { 36 36 37 37 } 38 38 … … 54 54 { 55 55 if (node.getStateSet()) pushState(node.getStateSet()); 56 56 57 57 traverse(node); 58 58 59 59 if (node.getStateSet()) popState(); 60 60 } 61 61 62 62 void apply(osg::Transform& transform) 63 63 { 64 64 if (transform.getStateSet()) pushState(transform.getStateSet()); 65 65 66 66 osg::Matrix matrix; 67 67 if (!_matrixStack.empty()) matrix = _matrixStack.back(); 68 68 69 69 transform.computeLocalToWorldMatrix(matrix,this); 70 70 71 71 pushMatrix(matrix); 72 72 73 73 traverse(transform); 74 74 75 75 popMatrix(); 76 76 77 77 if (transform.getStateSet()) popState(); 78 78 } 79 79 80 80 void apply(osg::Geode& geode) 81 81 { … … 87 87 88 88 if (drawable->getStateSet()) pushState(drawable->getStateSet()); 89 89 90 90 apply(geode.getDrawable(i)); 91 91 92 92 if (drawable->getStateSet()) popState(); 93 93 } 94 94 95 95 if (geode.getStateSet()) popState(); 96 96 } 97 97 98 98 void pushState(osg::StateSet* stateset) 99 99 { 100 100 osg::StateAttribute::GLModeValue prevBlendModeValue = _blendModeStack.empty() ? osg::StateAttribute::GLModeValue(osg::StateAttribute::INHERIT) : _blendModeStack.back(); 101 101 osg::StateAttribute::GLModeValue newBlendModeValue = stateset->getMode(GL_BLEND); 102 103 if (!(newBlendModeValue & osg::StateAttribute::PROTECTED) && 102 103 if (!(newBlendModeValue & osg::StateAttribute::PROTECTED) && 104 104 (prevBlendModeValue & osg::StateAttribute::OVERRIDE) ) 105 105 { 106 106 newBlendModeValue = prevBlendModeValue; 107 107 } 108 108 109 109 _blendModeStack.push_back(newBlendModeValue); 110 110 } 111 111 112 112 void popState() 113 113 { 114 114 _blendModeStack.pop_back(); 115 115 } 116 116 117 117 void pushMatrix(osg::Matrix& matrix) 118 118 { 119 119 _matrixStack.push_back(matrix); 120 120 } 121 121 122 122 void popMatrix() 123 123 { … … 133 133 return; 134 134 } 135 135 136 136 _oc->processGeometry(drawable, (_matrixStack.empty() ? 0 : &_matrixStack.back()), _ratio); 137 138 } 139 137 138 } 139 140 140 protected: 141 141 142 142 OccluderGeometry* _oc; 143 143 144 144 typedef std::vector<osg::Matrix> MatrixStack; 145 145 typedef std::vector<osg::StateAttribute::GLModeValue> ModeStack; 146 146 147 147 float _ratio; 148 148 MatrixStack _matrixStack; 149 149 ModeStack _blendModeStack; 150 150 151 151 }; 152 152 … … 156 156 157 157 osg::Timer_t startTick = osg::Timer::instance()->tick(); 158 158 159 159 CollectOccludersVisitor cov(this, matrix, sampleRatio); 160 160 subgraph->accept(cov); 161 161 162 162 setUpInternalStructures(); 163 163 … … 182 182 typedef std::vector<const osg::Vec3*> VertexPointers; 183 183 VertexPointers _vertexPointers; 184 184 185 185 OccluderGeometry::Vec3List _tempoaryTriangleVertices; 186 186 … … 204 204 _tempoaryTriangleVertices.push_back(v2); 205 205 _tempoaryTriangleVertices.push_back(v3); 206 206 207 207 } 208 208 else … … 215 215 216 216 } 217 217 218 218 void copyToLocalData() 219 219 { 220 220 if ((_vertexPointers.size()+_tempoaryTriangleVertices.size())<3) return; 221 222 221 222 223 223 const osg::Vec3* minVertex = _vertexPointers.empty() ? 0 : _vertexPointers.front(); 224 224 const osg::Vec3* maxVertex = _vertexPointers.empty() ? 0 : _vertexPointers.front(); 225 225 226 226 VertexPointers::iterator itr; 227 227 for(itr = _vertexPointers.begin(); … … 235 235 unsigned int base = _vertices->size(); 236 236 unsigned int numberNewVertices = _vertexPointers.empty() ? 0 : (maxVertex - minVertex) + 1; 237 237 238 238 // OSG_NOTICE<<"base = "<<base<<" numberNewVertices="<<numberNewVertices<<std::endl; 239 239 240 240 _vertices->resize(base + numberNewVertices + _tempoaryTriangleVertices.size()); 241 241 242 242 for(itr = _vertexPointers.begin(); 243 243 itr != _vertexPointers.end(); … … 267 267 } 268 268 } 269 269 270 270 } 271 271 … … 282 282 283 283 drawable->accept(tc); 284 284 285 285 tc.copyToLocalData(); 286 287 #if 0 286 287 #if 0 288 288 for(Vec3List::iterator vitr = _vertices.begin(); 289 289 vitr != _vertices.end(); … … 308 308 309 309 removeDuplicateVertices(); 310 310 311 311 osg::Timer_t t1 = osg::Timer::instance()->tick(); 312 312 … … 320 320 321 321 buildEdgeMaps(); 322 322 323 323 osg::Timer_t t4 = osg::Timer::instance()->tick(); 324 324 … … 332 332 333 333 dirtyBound(); 334 334 335 335 dirtyDisplayList(); 336 336 } … … 367 367 { 368 368 if (_vertices.empty()) return; 369 369 370 370 OSG_NOTICE<<"OccluderGeometry::removeDuplicates() before = "<<_vertices.size()<<std::endl; 371 371 … … 400 400 else 401 401 { 402 prev = curr; 402 prev = curr; 403 403 ++numUnique; 404 404 } 405 405 } 406 406 407 407 OSG_NOTICE<<"Num diplicates = "<<numDuplicates<<std::endl; 408 408 OSG_NOTICE<<"Num unique = "<<numUnique<<std::endl; … … 410 410 if (numDuplicates==0) return; 411 411 412 // now assign the unique Vec3 to the newVertices arrays 412 // now assign the unique Vec3 to the newVertices arrays 413 413 typedef std::vector<unsigned int> IndexMap; 414 414 IndexMap indexMap(indexVec3PtrPairs.size()); … … 429 429 for(; curr != indexVec3PtrPairs.end(); ++curr) 430 430 { 431 if (*prev==*curr) 431 if (*prev==*curr) 432 432 { 433 433 indexMap[curr->index] = index; … … 435 435 else 436 436 { 437 ++index; 438 437 ++index; 438 439 439 // add new unique vertex 440 440 indexMap[curr->index] = index; 441 441 newVertices.push_back(*(curr->vec)); 442 442 443 prev = curr; 443 prev = curr; 444 444 } 445 445 } … … 462 462 // OSG_NOTICE<<"OccluderGeometry::removeNullTriangles()"<<std::endl; 463 463 464 464 465 465 UIntList::iterator lastValidItr = _triangleIndices.begin(); 466 466 for(UIntList::iterator titr = _triangleIndices.begin(); … … 501 501 { 502 502 // OSG_NOTICE<<"OccluderGeometry::computeNormals()"<<std::endl; 503 503 504 504 unsigned int numTriangles = _triangleIndices.size() / 3; 505 505 unsigned int redundentIndices = _triangleIndices.size() - numTriangles * 3; … … 509 509 _triangleIndices.erase(_triangleIndices.begin() + numTriangles * 3, _triangleIndices.end()); 510 510 } 511 511 512 512 _triangleNormals.clear(); 513 513 _triangleNormals.reserve(numTriangles); 514 514 515 515 _normals.resize(_vertices.size()); 516 516 … … 529 529 530 530 if (!_normals.empty()) 531 { 531 { 532 532 _normals[p1] += normal; 533 533 _normals[p2] += normal; … … 535 535 } 536 536 } 537 537 538 538 for(Vec3List::iterator nitr = _normals.begin(); 539 539 nitr != _normals.end(); … … 543 543 } 544 544 545 545 546 546 } 547 547 … … 549 549 { 550 550 // OSG_NOTICE<<"OccluderGeometry::buildEdgeMaps()"<<std::endl; 551 551 552 552 typedef std::set<Edge> EdgeSet; 553 553 EdgeSet edgeSet; 554 554 555 555 unsigned int numTriangleErrors = 0; 556 556 unsigned int triNo=0; … … 562 562 GLuint p2 = *titr++; 563 563 GLuint p3 = *titr++; 564 564 565 565 { 566 566 Edge edge12(p1,p2); 567 567 EdgeSet::iterator itr = edgeSet.find(edge12); 568 if (itr == edgeSet.end()) 568 if (itr == edgeSet.end()) 569 569 { 570 570 if (!edge12.addTriangle(triNo)) ++numTriangleErrors; … … 577 577 } 578 578 579 { 579 { 580 580 Edge edge23(p2,p3); 581 581 EdgeSet::iterator itr = edgeSet.find(edge23); 582 if (itr == edgeSet.end()) 582 if (itr == edgeSet.end()) 583 583 { 584 584 if (!edge23.addTriangle(triNo)) ++numTriangleErrors; … … 590 590 } 591 591 } 592 592 593 593 { 594 594 Edge edge31(p3,p1); 595 595 EdgeSet::iterator itr = edgeSet.find(edge31); 596 if (itr == edgeSet.end()) 596 if (itr == edgeSet.end()) 597 597 { 598 598 if (!edge31.addTriangle(triNo)) ++numTriangleErrors; 599 599 600 600 edgeSet.insert(edge31); 601 601 } … … 609 609 _edges.clear(); 610 610 _edges.reserve(edgeSet.size()); 611 611 612 612 unsigned int numEdgesWithNoTriangles = 0; 613 613 unsigned int numEdgesWithOneTriangles = 0; 614 614 unsigned int numEdgesWithTwoTriangles = 0; 615 615 616 616 for(EdgeSet::iterator eitr = edgeSet.begin(); 617 617 eitr != edgeSet.end(); … … 635 635 pos = _vertices[opposite]; 636 636 } 637 637 638 638 if (edge._t2>=0) 639 639 { … … 656 656 edge._normal.set(0.0,0.0,0.0); 657 657 OSG_NOTICE<<"Warning no triangles on edge."<<std::endl; 658 break; 658 break; 659 659 case(1): 660 660 ++numEdgesWithOneTriangles; 661 661 edge._normal = pos - mid; 662 662 edge._normal.normalize(); 663 break; 663 break; 664 664 default: 665 ++numEdgesWithTwoTriangles; 665 ++numEdgesWithTwoTriangles; 666 666 edge._normal = (pos*0.5f) - mid; 667 667 edge._normal.normalize(); 668 break; 668 break; 669 669 } 670 670 … … 687 687 { 688 688 silhouetteIndices.clear(); 689 689 690 690 for(EdgeList::const_iterator eitr = _edges.begin(); 691 691 eitr != _edges.end(); … … 699 699 osg::Vec3 normal = (v2-v1) ^ lightdirection; 700 700 if (normal * edge._normal > 0.0) 701 { 701 { 702 702 silhouetteIndices.push_back(edge._p1); 703 703 silhouetteIndices.push_back(edge._p2); … … 715 715 { 716 716 silhouetteIndices.clear(); 717 717 718 718 for(EdgeList::const_iterator eitr = _edges.begin(); 719 719 eitr != _edges.end(); … … 727 727 osg::Vec3 normal = (v2-v1) ^ (v1-lightpos); 728 728 if (normal * edge._normal > 0.0) 729 { 729 { 730 730 silhouetteIndices.push_back(edge._p1); 731 731 silhouetteIndices.push_back(edge._p2); … … 765 765 // directional light. 766 766 osg::Vec3 lightdirection( -lightpos.x(), -lightpos.y(), -lightpos.z()); 767 767 768 768 // OSG_NOTICE<<"Directional light"<<std::endl; 769 769 770 770 // choose the base plane 771 771 const osg::Polytope::PlaneList& planes = _boundingPolytope.getPlaneList(); … … 787 787 UIntList silhouetteIndices; 788 788 computeLightDirectionSilhouetteEdges(lightdirection, silhouetteIndices); 789 789 790 790 osg::Vec3 offset( lightdirection*5.0f ); 791 791 792 792 float directionScale = 1.0f / basePlane.dotProductNormal(lightdirection); 793 793 794 794 for(UIntList::iterator itr = silhouetteIndices.begin(); 795 795 itr != silhouetteIndices.end(); … … 829 829 UIntList silhouetteIndices; 830 830 computeLightPositionSilhouetteEdges(lightposition, silhouetteIndices); 831 831 832 832 // OSG_NOTICE<<"basePlane "<<basePlane[0]<<" "<<basePlane[1]<<" "<<basePlane[2]<<" "<<basePlane[3]<<std::endl; 833 833 // OSG_NOTICE<<"lightpos = "<<std::endl; … … 840 840 const osg::Vec3& v1 = _vertices[*itr++]; 841 841 const osg::Vec3& v2 = _vertices[*itr++]; 842 842 843 843 osg::Vec3 d1 = v1 - lightposition; 844 844 osg::Vec3 d2 = v2 - lightposition; … … 893 893 894 894 renderInfo.getState()->setVertexPointer( 3, GL_FLOAT, 0, &(_vertices.front()) ); 895 895 896 896 if (!_normals.empty()) 897 897 { … … 937 937 { 938 938 osg::State* state = renderInfo.getState(); 939 939 940 940 state->disableAllVertexArrays(); 941 941 … … 959 959 { 960 960 osg::State* state = renderInfo.getState(); 961 961 962 962 state->disableAllVertexArrays(); 963 963 state->setVertexPointer( 3, GL_FLOAT, 0, &(_vertices.front()) ); … … 968 968 969 969 glDrawArrays( GL_QUADS, 0, _vertices.size() ); 970 970 971 971 // draw back faces of shadow volume 972 972 glCullFace(GL_FRONT); … … 974 974 975 975 glDrawArrays( GL_QUADS, 0, _vertices.size() ); 976 976 977 977 state->haveAppliedAttribute(osg::StateAttribute::CULLFACE); 978 978 state->haveAppliedAttribute(osg::StateAttribute::STENCIL); … … 982 982 { 983 983 osg::State* state = renderInfo.getState(); 984 984 985 985 state->disableAllVertexArrays(); 986 986 state->setVertexPointer( 3, GL_FLOAT, 0, &(_vertices.front()) );
