2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-05-02 08:14:40 +08:00
|
|
|
#include <osg/ClipNode>
|
|
|
|
|
2005-04-08 20:22:03 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-05-02 08:14:40 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
2007-12-11 19:55:15 +08:00
|
|
|
ClipNode::ClipNode():
|
|
|
|
_value(StateAttribute::ON)
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
2002-12-16 21:40:58 +08:00
|
|
|
_stateset = new StateSet;
|
2002-05-02 08:14:40 +08:00
|
|
|
}
|
|
|
|
|
2007-12-11 19:55:15 +08:00
|
|
|
ClipNode::ClipNode(const ClipNode& cn, const CopyOp& copyop):
|
|
|
|
Group(cn,copyop),
|
|
|
|
_value(cn._value)
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
|
|
|
for(ClipPlaneList::const_iterator itr=cn._planes.begin();
|
|
|
|
itr!=cn._planes.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
ClipPlane* plane = dynamic_cast<ClipPlane*>(copyop(itr->get()));
|
2007-12-11 19:55:15 +08:00
|
|
|
if (!plane)
|
|
|
|
continue;
|
|
|
|
_planes.push_back(plane);
|
|
|
|
_stateset->setAssociatedModes(plane, _value);
|
2002-05-02 08:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClipNode::~ClipNode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a 6 clip planes to create a clip box.
|
|
|
|
void ClipNode::createClipBox(const BoundingBox& bb,unsigned int clipPlaneNumberBase)
|
|
|
|
{
|
|
|
|
_planes.clear();
|
2007-12-11 19:55:15 +08:00
|
|
|
if (!_stateset.valid()) _stateset = new osg::StateSet;
|
2002-05-02 08:14:40 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
_planes.push_back(new ClipPlane(clipPlaneNumberBase ,1.0,0.0,0.0,-bb.xMin()));
|
2007-12-11 19:55:15 +08:00
|
|
|
_stateset->setAssociatedModes(_planes.back().get(), _value);
|
2002-12-16 21:40:58 +08:00
|
|
|
_planes.push_back(new ClipPlane(clipPlaneNumberBase+1,-1.0,0.0,0.0,bb.xMax()));
|
2007-12-11 19:55:15 +08:00
|
|
|
_stateset->setAssociatedModes(_planes.back().get(), _value);
|
2002-05-02 08:14:40 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
_planes.push_back(new ClipPlane(clipPlaneNumberBase+2,0.0,1.0,0.0,-bb.yMin()));
|
2007-12-11 19:55:15 +08:00
|
|
|
_stateset->setAssociatedModes(_planes.back().get(), _value);
|
2002-12-16 21:40:58 +08:00
|
|
|
_planes.push_back(new ClipPlane(clipPlaneNumberBase+3,0.0,-1.0,0.0,bb.yMax()));
|
2007-12-11 19:55:15 +08:00
|
|
|
_stateset->setAssociatedModes(_planes.back().get(), _value);
|
2002-05-02 08:14:40 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
_planes.push_back(new ClipPlane(clipPlaneNumberBase+4,0.0,0.0,1.0,-bb.zMin()));
|
2007-12-11 19:55:15 +08:00
|
|
|
_stateset->setAssociatedModes(_planes.back().get(), _value);
|
2002-12-16 21:40:58 +08:00
|
|
|
_planes.push_back(new ClipPlane(clipPlaneNumberBase+5,0.0,0.0,-1.0,bb.zMax()));
|
2007-12-11 19:55:15 +08:00
|
|
|
_stateset->setAssociatedModes(_planes.back().get(), _value);
|
2002-05-02 08:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a ClipPlane to a ClipNode. Return true if plane is added,
|
|
|
|
// return false if plane already exists in ClipNode, or clipplane is false.
|
2002-09-02 20:31:35 +08:00
|
|
|
bool ClipNode::addClipPlane(ClipPlane* clipplane)
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
|
|
|
if (!clipplane) return false;
|
|
|
|
|
|
|
|
if (std::find(_planes.begin(),_planes.end(),clipplane)==_planes.end())
|
|
|
|
{
|
2007-12-11 19:55:15 +08:00
|
|
|
// cliplane doesn't exist in list so add it.
|
2002-05-02 08:14:40 +08:00
|
|
|
_planes.push_back(clipplane);
|
2007-12-11 19:55:15 +08:00
|
|
|
if (!_stateset.valid()) _stateset = new osg::StateSet;
|
|
|
|
_stateset->setAssociatedModes(clipplane, _value);
|
2002-05-02 08:14:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove ClipPlane from a ClipNode. Return true if plane is removed,
|
|
|
|
// return false if plane does not exists in ClipNode.
|
2002-09-02 20:31:35 +08:00
|
|
|
bool ClipNode::removeClipPlane(ClipPlane* clipplane)
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
|
|
|
if (!clipplane) return false;
|
|
|
|
|
|
|
|
ClipPlaneList::iterator itr = std::find(_planes.begin(),_planes.end(),clipplane);
|
|
|
|
if (itr!=_planes.end())
|
|
|
|
{
|
2007-12-11 19:55:15 +08:00
|
|
|
// cliplane exist in list so erase it.
|
|
|
|
_stateset->removeAssociatedModes(clipplane);
|
2002-05-02 08:14:40 +08:00
|
|
|
_planes.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove ClipPlane, at specified index, from a ClipNode. Return true if plane is removed,
|
|
|
|
// return false if plane does not exists in ClipNode.
|
2002-09-02 20:31:35 +08:00
|
|
|
bool ClipNode::removeClipPlane(unsigned int pos)
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
|
|
|
if (pos<_planes.size())
|
|
|
|
{
|
2007-12-11 19:55:15 +08:00
|
|
|
ClipPlaneList::iterator itr = _planes.begin();
|
|
|
|
std::advance(itr, pos);
|
|
|
|
_stateset->removeAssociatedModes(itr->get());
|
|
|
|
_planes.erase(itr);
|
2002-05-02 08:14:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-07 19:44:24 +08:00
|
|
|
return false;
|
2002-05-02 08:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the GLModes on StateSet associated with the ClipPlanes.
|
2007-09-11 21:39:55 +08:00
|
|
|
void ClipNode::setStateSetModes(StateSet& stateset,StateAttribute::GLModeValue value) const
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
|
|
|
for(ClipPlaneList::const_iterator itr=_planes.begin();
|
|
|
|
itr!=_planes.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2002-07-09 17:35:42 +08:00
|
|
|
stateset.setAssociatedModes(itr->get(),value);
|
2002-05-02 08:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-19 05:08:42 +08:00
|
|
|
void ClipNode::setLocalStateSetModes(StateAttribute::GLModeValue value)
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
2007-12-11 19:55:15 +08:00
|
|
|
_value = value;
|
2002-12-16 21:40:58 +08:00
|
|
|
if (!_stateset) _stateset = new StateSet;
|
2002-09-02 20:31:35 +08:00
|
|
|
setStateSetModes(*_stateset,value);
|
2002-05-02 08:14:40 +08:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere ClipNode::computeBound() const
|
2002-05-02 08:14:40 +08:00
|
|
|
{
|
|
|
|
return Group::computeBound();
|
|
|
|
}
|