root/OpenSceneGraph/trunk/src/osg/ClipNode.cpp @ 13041

Revision 13041, 4.6 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osg/ClipNode>
14
15#include <algorithm>
16
17using namespace osg;
18
19ClipNode::ClipNode():
20    _value(StateAttribute::ON),
21    _referenceFrame(RELATIVE_RF)
22{
23    setStateSet(new StateSet);
24}
25
26ClipNode::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
44ClipNode::~ClipNode()
45{
46}
47
48
49void ClipNode::setReferenceFrame(ReferenceFrame rf)
50{
51    _referenceFrame = rf;
52}
53
54// Create a 6 clip planes to create a clip box.
55void 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// Add a ClipPlane to a ClipNode. Return true if plane is added,
77// return false if plane already exists in ClipNode, or clipplane is false.
78bool ClipNode::addClipPlane(ClipPlane* clipplane)
79{
80    if (!clipplane) return false;
81
82    if (std::find(_planes.begin(),_planes.end(),clipplane)==_planes.end())
83    {
84        // cliplane doesn't exist in list so add it.
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// Remove ClipPlane from a ClipNode. Return true if plane is removed,
97// return false if plane does not exists in ClipNode.
98bool 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        // cliplane exist in list so erase it.
106        _stateset->removeAssociatedModes(clipplane);
107        _planes.erase(itr);
108        return true;
109    }
110    else
111    {
112        return false;
113    }
114}
115
116// Remove ClipPlane, at specified index, from a ClipNode. Return true if plane is removed,
117// return false if plane does not exists in ClipNode.
118bool 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// Set the GLModes on StateSet associated with the ClipPlanes.
135void 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
145void ClipNode::setLocalStateSetModes(StateAttribute::GLModeValue value)
146{
147    _value = value;
148    if (!_stateset) setStateSet(new StateSet);
149
150    setStateSetModes(*_stateset,value);
151}
152
153BoundingSphere ClipNode::computeBound() const
154{
155    return Group::computeBound();
156}
Note: See TracBrowser for help on using the browser.