| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgSim/VisibilityGroup> |
|---|
| 15 | |
|---|
| 16 | #include <osgUtil/CullVisitor> |
|---|
| 17 | #include <osgUtil/IntersectVisitor> |
|---|
| 18 | |
|---|
| 19 | using namespace osgSim; |
|---|
| 20 | using namespace osg; |
|---|
| 21 | |
|---|
| 22 | VisibilityGroup::VisibilityGroup(): |
|---|
| 23 | _volumeIntersectionMask(0xFFFFFFFF), |
|---|
| 24 | _segmentLength(0.f) |
|---|
| 25 | { |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | VisibilityGroup::VisibilityGroup(const VisibilityGroup& sw,const osg::CopyOp& copyop): |
|---|
| 29 | osg::Group(sw,copyop), |
|---|
| 30 | _volumeIntersectionMask(0xFFFFFFFF), |
|---|
| 31 | _segmentLength(0.f) |
|---|
| 32 | { |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | void VisibilityGroup::traverse(osg::NodeVisitor& nv) |
|---|
| 36 | { |
|---|
| 37 | if (nv.getTraversalMode()==osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN && nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR) |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | osgUtil::CullVisitor& cv = (osgUtil::CullVisitor&) nv; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | osg::Vec3 eye = cv.getEyeLocal(); |
|---|
| 46 | osg::Vec3 look = cv.getLookVectorLocal(); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | float length = _segmentLength; |
|---|
| 50 | if(length == 0.f) |
|---|
| 51 | length = 2.0f*getBound().radius(); |
|---|
| 52 | look *= length; |
|---|
| 53 | osg::Vec3 center = eye + look; |
|---|
| 54 | |
|---|
| 55 | osg::Vec3 seg = center - eye; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | osgUtil::IntersectVisitor iv; |
|---|
| 59 | osg::ref_ptr<osg::LineSegment> lineseg = new osg::LineSegment; |
|---|
| 60 | lineseg->set(eye, center); |
|---|
| 61 | iv.addLineSegment(lineseg.get()); |
|---|
| 62 | iv.setTraversalMask(_volumeIntersectionMask); |
|---|
| 63 | |
|---|
| 64 | if(_visibilityVolume.valid()) |
|---|
| 65 | _visibilityVolume->accept(iv); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | if(iv.hits()) |
|---|
| 69 | { |
|---|
| 70 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(lineseg.get()); |
|---|
| 71 | if(!hitList.empty()) |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | osg::Vec3 normal = hitList.front().getWorldIntersectNormal(); |
|---|
| 75 | if((normal*seg) > 0.f ) |
|---|
| 76 | Group::traverse(nv); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | Group::traverse(nv); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|