| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/ClipNode> |
|---|
| 14 | |
|---|
| 15 | #include <algorithm> |
|---|
| 16 | |
|---|
| 17 | using namespace osg; |
|---|
| 18 | |
|---|
| 19 | ClipNode::ClipNode(): |
|---|
| 20 | _value(StateAttribute::ON), |
|---|
| 21 | _referenceFrame(RELATIVE_RF) |
|---|
| 22 | { |
|---|
| 23 | setStateSet(new StateSet); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | ClipNode::ClipNode(const ClipNode& cn, const CopyOp& copyop): |
|---|
| 27 | Group(cn,copyop), |
|---|
| 28 | _value(cn._value), |
|---|
| 29 | _referenceFrame(cn._referenceFrame) |
|---|
| 30 | { |
|---|
| 31 | setStateSet(new StateSet); |
|---|
| 32 | for(ClipPlaneList::const_iterator itr=cn._planes.begin(); |
|---|
| 33 | itr!=cn._planes.end(); |
|---|
| 34 | ++itr) |
|---|
| 35 | { |
|---|
| 36 | ClipPlane* plane = dynamic_cast<ClipPlane*>(copyop(itr->get())); |
|---|
| 37 | if (!plane) |
|---|
| 38 | continue; |
|---|
| 39 | _planes.push_back(plane); |
|---|
| 40 | _stateset->setAssociatedModes(plane, _value); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | ClipNode::~ClipNode() |
|---|
| 45 | { |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | void ClipNode::setReferenceFrame(ReferenceFrame rf) |
|---|
| 50 | { |
|---|
| 51 | _referenceFrame = rf; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | void ClipNode::createClipBox(const BoundingBox& bb,unsigned int clipPlaneNumberBase) |
|---|
| 56 | { |
|---|
| 57 | _planes.clear(); |
|---|
| 58 | if (!_stateset.valid()) _stateset = new osg::StateSet; |
|---|
| 59 | |
|---|
| 60 | _planes.push_back(new ClipPlane(clipPlaneNumberBase ,1.0,0.0,0.0,-bb.xMin())); |
|---|
| 61 | _stateset->setAssociatedModes(_planes.back().get(), _value); |
|---|
| 62 | _planes.push_back(new ClipPlane(clipPlaneNumberBase+1,-1.0,0.0,0.0,bb.xMax())); |
|---|
| 63 | _stateset->setAssociatedModes(_planes.back().get(), _value); |
|---|
| 64 | |
|---|
| 65 | _planes.push_back(new ClipPlane(clipPlaneNumberBase+2,0.0,1.0,0.0,-bb.yMin())); |
|---|
| 66 | _stateset->setAssociatedModes(_planes.back().get(), _value); |
|---|
| 67 | _planes.push_back(new ClipPlane(clipPlaneNumberBase+3,0.0,-1.0,0.0,bb.yMax())); |
|---|
| 68 | _stateset->setAssociatedModes(_planes.back().get(), _value); |
|---|
| 69 | |
|---|
| 70 | _planes.push_back(new ClipPlane(clipPlaneNumberBase+4,0.0,0.0,1.0,-bb.zMin())); |
|---|
| 71 | _stateset->setAssociatedModes(_planes.back().get(), _value); |
|---|
| 72 | _planes.push_back(new ClipPlane(clipPlaneNumberBase+5,0.0,0.0,-1.0,bb.zMax())); |
|---|
| 73 | _stateset->setAssociatedModes(_planes.back().get(), _value); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | bool ClipNode::addClipPlane(ClipPlane* clipplane) |
|---|
| 79 | { |
|---|
| 80 | if (!clipplane) return false; |
|---|
| 81 | |
|---|
| 82 | if (std::find(_planes.begin(),_planes.end(),clipplane)==_planes.end()) |
|---|
| 83 | { |
|---|
| 84 | |
|---|
| 85 | _planes.push_back(clipplane); |
|---|
| 86 | if (!_stateset.valid()) _stateset = new osg::StateSet; |
|---|
| 87 | _stateset->setAssociatedModes(clipplane, _value); |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | return false; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | bool ClipNode::removeClipPlane(ClipPlane* clipplane) |
|---|
| 99 | { |
|---|
| 100 | if (!clipplane) return false; |
|---|
| 101 | |
|---|
| 102 | ClipPlaneList::iterator itr = std::find(_planes.begin(),_planes.end(),clipplane); |
|---|
| 103 | if (itr!=_planes.end()) |
|---|
| 104 | { |
|---|
| 105 | |
|---|
| 106 | _stateset->removeAssociatedModes(clipplane); |
|---|
| 107 | _planes.erase(itr); |
|---|
| 108 | return true; |
|---|
| 109 | } |
|---|
| 110 | else |
|---|
| 111 | { |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | bool ClipNode::removeClipPlane(unsigned int pos) |
|---|
| 119 | { |
|---|
| 120 | if (pos<_planes.size()) |
|---|
| 121 | { |
|---|
| 122 | ClipPlaneList::iterator itr = _planes.begin(); |
|---|
| 123 | std::advance(itr, pos); |
|---|
| 124 | _stateset->removeAssociatedModes(itr->get()); |
|---|
| 125 | _planes.erase(itr); |
|---|
| 126 | return true; |
|---|
| 127 | } |
|---|
| 128 | else |
|---|
| 129 | { |
|---|
| 130 | return false; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | void ClipNode::setStateSetModes(StateSet& stateset,StateAttribute::GLModeValue value) const |
|---|
| 136 | { |
|---|
| 137 | for(ClipPlaneList::const_iterator itr=_planes.begin(); |
|---|
| 138 | itr!=_planes.end(); |
|---|
| 139 | ++itr) |
|---|
| 140 | { |
|---|
| 141 | stateset.setAssociatedModes(itr->get(),value); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | void ClipNode::setLocalStateSetModes(StateAttribute::GLModeValue value) |
|---|
| 146 | { |
|---|
| 147 | _value = value; |
|---|
| 148 | if (!_stateset) setStateSet(new StateSet); |
|---|
| 149 | |
|---|
| 150 | setStateSetModes(*_stateset,value); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | BoundingSphere ClipNode::computeBound() const |
|---|
| 154 | { |
|---|
| 155 | return Group::computeBound(); |
|---|
| 156 | } |
|---|