dded osg::ClipNode class for managing OpenGL clipping planes, and osgclip demo.

This commit is contained in:
Robert Osfield
2002-05-02 00:14:40 +00:00
parent 3c25b7ab7c
commit ae8076f83b
20 changed files with 610 additions and 35 deletions

View File

@@ -0,0 +1,15 @@
TOPDIR = ../../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgclip.cpp\
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
Makefile.inst=Makefile
EXEC = osgclip
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,11 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgclip.cpp\
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgclip
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,189 @@
#include <osg/Transform>
#include <osg/ClipNode>
#include <osg/Billboard>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Notify>
#include <osg/Material>
#include <osg/PolygonOffset>
#include <osg/PolygonMode>
#include <osg/LineStipple>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgUtil/TrackballManipulator>
#include <osgUtil/FlightManipulator>
#include <osgUtil/DriveManipulator>
#include <osgUtil/TransformCallback>
#include <osgGLUT/glut>
#include <osgGLUT/Viewer>
#include <osgUtil/Optimizer>
void write_usage(std::ostream& out,const std::string& name)
{
out << std::endl;
out <<"usage:"<< std::endl;
out <<" "<<name<<" [options] infile1 [infile2 ...]"<< std::endl;
out << std::endl;
out <<"options:"<< std::endl;
out <<" -l libraryName - load plugin of name libraryName"<< std::endl;
out <<" i.e. -l osgdb_pfb"<< std::endl;
out <<" Useful for loading reader/writers which can load"<< std::endl;
out <<" other file formats in addition to its extension."<< std::endl;
out <<" -e extensionName - load reader/wrter plugin for file extension"<< std::endl;
out <<" i.e. -e pfb"<< std::endl;
out <<" Useful short hand for specifying full library name as"<< std::endl;
out <<" done with -l above, as it automatically expands to"<< std::endl;
out <<" the full library name appropriate for each platform."<< std::endl;
out <<std::endl;
out <<" -stereo - switch on stereo rendering, using the default of,"<< std::endl;
out <<" ANAGLYPHIC or the value set in the OSG_STEREO_MODE "<< std::endl;
out <<" environmental variable. See doc/stereo.html for "<< std::endl;
out <<" further details on setting up accurate stereo "<< std::endl;
out <<" for your system. "<< std::endl;
out <<" -stereo ANAGLYPHIC - switch on anaglyphic(red/cyan) stereo rendering."<< std::endl;
out <<" -stereo QUAD_BUFFER - switch on quad buffered stereo rendering."<< std::endl;
out <<std::endl;
out <<" -stencil - use a visual with stencil buffer enabled, this "<< std::endl;
out <<" also allows the depth complexity statistics mode"<< std::endl;
out <<" to be used (press 'p' three times to cycle to it)."<< std::endl;
out << std::endl;
}
osg::Node* decorate_with_clip_node(osg::Node* subgraph)
{
osg::Group* rootnode = osgNew osg::Group;
// create wireframe view of the model so the user can see
// what parts are being culled away.
osg::StateSet* stateset = new osg::StateSet;
osg::Material* material = new osg::Material;
osg::PolygonMode* polymode = new osg::PolygonMode;
polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE_ON);
osg::Group* wireframe_subgraph = osgNew osg::Group;
wireframe_subgraph->setStateSet(stateset);
wireframe_subgraph->addChild(subgraph);
rootnode->addChild(wireframe_subgraph);
/*
// simple approach to adding a clipnode above a subrgaph.
// create clipped part.
osg::ClipNode* clipped_subgraph = osgNew osg::ClipNode;
osg::BoundingSphere bs = subgraph->getBound();
bs.radius()*= 0.4f;
osg::BoundingBox bb;
bb.expandBy(bs);
clipped_subgraph->createClipBox(bb);
clipped_subgraph->addChild(subgraph);
rootnode->addChild(clipped_subgraph);
*/
// more complex approach to managing ClipNode, allowing
// ClipNode node to be transformed independantly from the subgraph
// that it is clipping.
osg::Transform* transform= osgNew osg::Transform;
osg::NodeCallback* nc = new osgUtil::TransformCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f));
transform->setAppCallback(nc);
osg::ClipNode* clipnode = osgNew osg::ClipNode;
osg::BoundingSphere bs = subgraph->getBound();
bs.radius()*= 0.4f;
osg::BoundingBox bb;
bb.expandBy(bs);
clipnode->createClipBox(bb);
clipnode->setCullingActive(false);
transform->addChild(clipnode);
rootnode->addChild(transform);
// create clipped part.
osg::Group* clipped_subgraph = osgNew osg::Group;
clipped_subgraph->setStateSet(clipnode->getStateSet());
clipped_subgraph->addChild(subgraph);
rootnode->addChild(clipped_subgraph);
return rootnode;
}
int main( int argc, char **argv )
{
// initialize the GLUT
glutInit( &argc, argv );
if (argc<2)
{
write_usage(osg::notify(osg::NOTICE),argv[0]);
return 0;
}
// create the commandline args.
std::vector<std::string> commandLine;
for(int i=1;i<argc;++i) commandLine.push_back(argv[i]);
// initialize the viewer.
osgGLUT::Viewer viewer;
viewer.setWindowTitle(argv[0]);
// configure the viewer from the commandline arguments, and eat any
// parameters that have been matched.
viewer.readCommandLine(commandLine);
// configure the plugin registry from the commandline arguments, and
// eat any parameters that have been matched.
osgDB::readCommandLine(commandLine);
// load the nodes from the commandline arguments.
osg::Node* loadedModel = osgDB::readNodeFiles(commandLine);
if (!loadedModel)
{
write_usage(osg::notify(osg::NOTICE),argv[0]);
return 1;
}
// decorate the scenegraph with a clip node.
osg::Node* rootnode = decorate_with_clip_node(loadedModel);
// run optimization over the scene graph
osgUtil::Optimizer optimzer;
optimzer.optimize(rootnode);
// add a viewport to the viewer and attach the scene graph.
viewer.addViewport( rootnode );
// register trackball, flight and drive.
viewer.registerCameraManipulator(new osgUtil::TrackballManipulator);
viewer.registerCameraManipulator(new osgUtil::FlightManipulator);
viewer.registerCameraManipulator(new osgUtil::DriveManipulator);
// open the viewer window.
viewer.open();
// fire up the event loop.
viewer.run();
return 0;
}

119
src/osg/ClipNode.cpp Normal file
View File

@@ -0,0 +1,119 @@
#include <osg/ClipNode>
using namespace osg;
ClipNode::ClipNode()
{
_value = StateAttribute::ON;
_dstate = osgNew StateSet;
}
ClipNode::ClipNode(const ClipNode& cn, const CopyOp& copyop):Group(cn,copyop)
{
for(ClipPlaneList::const_iterator itr=cn._planes.begin();
itr!=cn._planes.end();
++itr)
{
ClipPlane* plane = dynamic_cast<ClipPlane*>(copyop(itr->get()));
if (plane) addClipPlane(plane);
}
}
ClipNode::~ClipNode()
{
}
// Create a 6 clip planes to create a clip box.
void ClipNode::createClipBox(const BoundingBox& bb,unsigned int clipPlaneNumberBase)
{
_planes.clear();
_planes.push_back(new ClipPlane(clipPlaneNumberBase ,1.0,0.0,0.0,-bb.xMin()));
_planes.push_back(new ClipPlane(clipPlaneNumberBase+1,-1.0,0.0,0.0,bb.xMax()));
_planes.push_back(new ClipPlane(clipPlaneNumberBase+2,0.0,1.0,0.0,-bb.yMin()));
_planes.push_back(new ClipPlane(clipPlaneNumberBase+3,0.0,-1.0,0.0,bb.yMax()));
_planes.push_back(new ClipPlane(clipPlaneNumberBase+4,0.0,0.0,1.0,-bb.zMin()));
_planes.push_back(new ClipPlane(clipPlaneNumberBase+5,0.0,0.0,-1.0,bb.zMax()));
setLocalStateSetModes(_value);
}
// Add a ClipPlane to a ClipNode. Return true if plane is added,
// return false if plane already exists in ClipNode, or clipplane is false.
const bool ClipNode::addClipPlane(ClipPlane* clipplane)
{
if (!clipplane) return false;
if (std::find(_planes.begin(),_planes.end(),clipplane)==_planes.end())
{
// cliplane doesn't exist in list so add it.
_planes.push_back(clipplane);
setLocalStateSetModes(_value);
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.
const bool ClipNode::removeClipPlane(ClipPlane* clipplane)
{
if (!clipplane) return false;
ClipPlaneList::iterator itr = std::find(_planes.begin(),_planes.end(),clipplane);
if (itr!=_planes.end())
{
// cliplane exist in list so erase it.
_planes.erase(itr);
setLocalStateSetModes(_value);
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.
const bool ClipNode::removeClipPlane(unsigned int pos)
{
if (pos<_planes.size())
{
_planes.erase(_planes.begin()+pos);
setLocalStateSetModes(_value);
return true;
}
else
{
return NULL;
}
}
// Set the GLModes on StateSet associated with the ClipPlanes.
void ClipNode::setStateSetModes(StateSet& stateset,const StateAttribute::GLModeValue value) const
{
for(ClipPlaneList::const_iterator itr=_planes.begin();
itr!=_planes.end();
++itr)
{
(*itr)->setStateSetModes(stateset,value);
}
}
void ClipNode::setLocalStateSetModes(const StateAttribute::GLModeValue value)
{
if (!_dstate) _dstate = osgNew StateSet;
_dstate->setAllToInherit();
setStateSetModes(*_dstate,value);
}
const bool ClipNode::computeBound() const
{
return Group::computeBound();
}

View File

@@ -8,6 +8,7 @@ CXXFILES =\
BoundingBox.cpp\
BoundingSphere.cpp\
Camera.cpp\
ClipNode.cpp\
ClipPlane.cpp\
ColorMask.cpp\
ColorMatrix.cpp\

View File

@@ -4,6 +4,7 @@
#include <osg/LOD>
#include <osg/Billboard>
#include <osg/LightSource>
#include <osg/ClipNode>
#include <osg/Notify>
#include <osg/TexEnv>
#include <osg/AlphaFunc>
@@ -60,6 +61,7 @@ class PrintVisitor : public NodeVisitor
virtual void apply(Geode& node) { apply((Node&)node); }
virtual void apply(Billboard& node) { apply((Geode&)node); }
virtual void apply(LightSource& node) { apply((Node&)node); }
virtual void apply(ClipNode& node) { apply((Node&)node); }
virtual void apply(Group& node) { apply((Node&)node); }
virtual void apply(Transform& node) { apply((Group&)node); }
@@ -524,13 +526,34 @@ void CullVisitor::apply(LightSource& node)
Light* light = node.getLight();
if (light)
{
addLight(light,&matrix);
addPositionedAttribute(&matrix,light);
}
// pop the node's state off the geostate stack.
if (node_state) popStateSet();
}
void CullVisitor::apply(ClipNode& node)
{
// push the node's state.
StateSet* node_state = node.getStateSet();
if (node_state) pushStateSet(node_state);
Matrix& matrix = getModelViewMatrix();
const ClipNode::ClipPlaneList& planes = node.getClipPlaneList();
for(ClipNode::ClipPlaneList::const_iterator itr=planes.begin();
itr!=planes.end();
++itr)
{
addPositionedAttribute(&matrix,itr->get());
}
handle_cull_callbacks_and_traverse(node);
// pop the node's state off the geostate stack.
if (node_state) popStateSet();
}
void CullVisitor::apply(Group& node)
{
@@ -824,7 +847,7 @@ ImpostorSprite* CullVisitor::createImpostorSprite(Impostor& node)
if (!bs.isValid())
{
std::cout << "bb invalid"<<&node<<std::endl;
osg::notify(osg::WARN) << "bb invalid"<<&node<<std::endl;
return NULL;
}

View File

@@ -119,6 +119,11 @@ void RenderStage::draw(osg::State& state,RenderLeaf*& previous)
// Statistics features
bool RenderStage::getStats(Statistics* primStats)
{
if (_renderStageLighting.valid()) primStats->addLight(_renderStageLighting->_lightList.size());
if (_renderStageLighting.valid())
{
// need to re-implement by checking for lights in the scene
// by downcasting the positioned attribute list. RO. May 2002.
//primStats->addLight(_renderStageLighting->_lightList.size());
}
return RenderBin::getStats(primStats);
}

View File

@@ -16,7 +16,7 @@ RenderStageLighting::~RenderStageLighting()
void RenderStageLighting::reset()
{
_lightList.clear();
_attrList.clear();
}
void RenderStageLighting::draw(osg::State& state,RenderLeaf*& previous)
@@ -30,8 +30,8 @@ void RenderStageLighting::draw(osg::State& state,RenderLeaf*& previous)
}
// apply the light list.
for(LightList::iterator litr=_lightList.begin();
litr!=_lightList.end();
for(AttrMatrixList::iterator litr=_attrList.begin();
litr!=_attrList.end();
++litr)
{
state.applyModelViewMatrix((*litr).second.get());

View File

@@ -325,10 +325,10 @@ void SceneView::cullStage(osg::Matrix* projection,osg::Matrix* modelview,osgUtil
switch(_lightingMode)
{
case(HEADLIGHT):
renderStage->addLight(_light.get(),NULL);
renderStage->addPositionedAttribute(NULL,_light.get());
break;
case(SKY_LIGHT):
renderStage->addLight(_light.get(),modelview);
renderStage->addPositionedAttribute(modelview,_light.get());
break;
case(NO_SCENEVIEW_LIGHT):
break;