2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgoccluder.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/Billboard>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Notify>
|
2005-04-08 17:51:56 +08:00
|
|
|
#include <osg/io_utils>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/WriteFile>
|
|
|
|
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
|
|
|
#include <osg/OccluderNode>
|
|
|
|
#include <osg/Geometry>
|
2003-04-19 05:44:48 +08:00
|
|
|
#include <osg/ShapeDrawable>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <iostream>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
class OccluderEventHandler : public osgGA::GUIEventHandler
|
|
|
|
{
|
|
|
|
public:
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
OccluderEventHandler(osgViewer::Viewer* viewer):_viewer(viewer) {}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
|
|
|
|
|
|
|
|
void addPoint(const osg::Vec3& pos);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
void endOccluder();
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-04-18 23:59:34 +08:00
|
|
|
osg::Group* rootNode() { return dynamic_cast<osg::Group*>(_viewer->getSceneData()); }
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
osgViewer::Viewer* _viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::ref_ptr<osg::Group> _occluders;
|
|
|
|
osg::ref_ptr<osg::ConvexPlanarOccluder> _convexPlanarOccluder;
|
|
|
|
};
|
|
|
|
|
2007-01-10 01:35:46 +08:00
|
|
|
bool OccluderEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::KEYDOWN):
|
|
|
|
{
|
|
|
|
if (ea.getKey()=='a')
|
|
|
|
|
2007-01-10 01:35:46 +08:00
|
|
|
{
|
|
|
|
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
|
osgUtil::LineSegmentIntersector::Intersections intersections;
|
2013-05-04 03:26:27 +08:00
|
|
|
if (view && view->computeIntersections(ea, intersections))
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2007-01-10 01:35:46 +08:00
|
|
|
const osgUtil::LineSegmentIntersector::Intersection& hit = *(intersections.begin());
|
|
|
|
if (hit.matrix.valid()) addPoint(hit.localIntersectionPoint * (*hit.matrix));
|
|
|
|
else addPoint(hit.localIntersectionPoint);
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()=='e')
|
|
|
|
{
|
|
|
|
endOccluder();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()=='O')
|
|
|
|
{
|
|
|
|
if (_occluders.valid())
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
if (osgDB::writeNodeFile(*_occluders,"saved_occluders.osgt"))
|
|
|
|
std::cout<<"saved occluders to 'saved_occluders.osgt'"<<std::endl;
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout<<"no occluders to save"<<std::endl;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OccluderEventHandler::addPoint(const osg::Vec3& pos)
|
|
|
|
{
|
|
|
|
std::cout<<"add point "<<pos<<std::endl;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
if (!_convexPlanarOccluder.valid()) _convexPlanarOccluder = new osg::ConvexPlanarOccluder;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::ConvexPlanarPolygon& occluder = _convexPlanarOccluder->getOccluder();
|
|
|
|
occluder.add(pos);
|
2003-04-19 05:44:48 +08:00
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
//
|
2003-04-19 05:44:48 +08:00
|
|
|
// osg::BoundingSphere bs = rootNode()->getBound();
|
2013-06-27 17:54:12 +08:00
|
|
|
//
|
2003-04-19 05:44:48 +08:00
|
|
|
// osg::ShapeDrawable* sd = new osg::ShapeDrawable(new osg::Sphere(pos,bs.radius()*0.001f));
|
|
|
|
// osg::Geode* geode = new osg::Geode;
|
|
|
|
// geode->addDrawable(sd);
|
2013-06-27 17:54:12 +08:00
|
|
|
//
|
2003-04-19 05:44:48 +08:00
|
|
|
// rootNode()->addChild(geode);
|
2013-06-27 17:54:12 +08:00
|
|
|
//
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
void OccluderEventHandler::endOccluder()
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
if (_convexPlanarOccluder.valid())
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
if (_convexPlanarOccluder->getOccluder().getVertexList().size()>=3)
|
|
|
|
{
|
|
|
|
osg::OccluderNode* occluderNode = new osg::OccluderNode;
|
|
|
|
occluderNode->setOccluder(_convexPlanarOccluder.get());
|
|
|
|
|
|
|
|
if (!_occluders.valid())
|
|
|
|
{
|
|
|
|
_occluders = new osg::Group;
|
2003-04-18 23:59:34 +08:00
|
|
|
if (rootNode()) rootNode()->addChild(_occluders.get());
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
_occluders->addChild(occluderNode);
|
|
|
|
|
|
|
|
std::cout<<"created occluder"<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout<<"Occluder requires at least 3 points to create occluder."<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout<<"No occluder points to create occluder with."<<std::endl;
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// reset current occluder.
|
|
|
|
_convexPlanarOccluder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* createOccluder(const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3,const osg::Vec3& v4,float holeRatio=-1.0f)
|
|
|
|
{
|
2010-09-15 20:00:12 +08:00
|
|
|
// create an occluder which will sit alongside the loaded model.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::OccluderNode* occluderNode = new osg::OccluderNode;
|
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// create the convex planar occluder
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::ConvexPlanarOccluder* cpo = new osg::ConvexPlanarOccluder;
|
|
|
|
|
|
|
|
// attach it to the occluder node.
|
|
|
|
occluderNode->setOccluder(cpo);
|
|
|
|
occluderNode->setName("occluder");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// set the occluder up for the front face of the bounding box.
|
|
|
|
osg::ConvexPlanarPolygon& occluder = cpo->getOccluder();
|
|
|
|
occluder.add(v1);
|
|
|
|
occluder.add(v2);
|
|
|
|
occluder.add(v3);
|
|
|
|
occluder.add(v4);
|
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// create a hole at the center of the occluder if needed.
|
2003-03-15 04:35:45 +08:00
|
|
|
if (holeRatio>0.0f)
|
|
|
|
{
|
|
|
|
// create hole.
|
|
|
|
float ratio = holeRatio;
|
|
|
|
float one_minus_ratio = 1-ratio;
|
|
|
|
osg::Vec3 center = (v1+v2+v3+v4)*0.25f;
|
|
|
|
osg::Vec3 v1dash = v1*ratio + center*one_minus_ratio;
|
|
|
|
osg::Vec3 v2dash = v2*ratio + center*one_minus_ratio;
|
|
|
|
osg::Vec3 v3dash = v3*ratio + center*one_minus_ratio;
|
|
|
|
osg::Vec3 v4dash = v4*ratio + center*one_minus_ratio;
|
|
|
|
|
|
|
|
osg::ConvexPlanarPolygon hole;
|
|
|
|
hole.add(v1dash);
|
|
|
|
hole.add(v2dash);
|
|
|
|
hole.add(v3dash);
|
|
|
|
hole.add(v4dash);
|
|
|
|
|
|
|
|
cpo->addHole(hole);
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// create a drawable for occluder.
|
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* coords = new osg::Vec3Array(occluder.getVertexList().begin(),occluder.getVertexList().end());
|
|
|
|
geom->setVertexArray(coords);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array(1);
|
|
|
|
(*colors)[0].set(1.0f,1.0f,1.0f,0.5f);
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(geom);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::StateSet* stateset = new osg::StateSet;
|
|
|
|
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
|
|
|
|
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
geom->setStateSet(stateset);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// add the occluder geode as a child of the occluder,
|
|
|
|
// as the occluder can't self occlude its subgraph the
|
2010-09-15 20:00:12 +08:00
|
|
|
// geode will never be occluded by this occluder.
|
2013-06-27 17:54:12 +08:00
|
|
|
occluderNode->addChild(geode);
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
return occluderNode;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Group* createOccludersAroundModel(osg::Node* model)
|
|
|
|
{
|
|
|
|
osg::Group* scene = new osg::Group;
|
|
|
|
scene->setName("rootgroup");
|
|
|
|
|
|
|
|
|
|
|
|
// add the loaded model into a the scene group.
|
|
|
|
scene->addChild(model);
|
|
|
|
model->setName("model");
|
|
|
|
|
|
|
|
// get the bounding volume of the model.
|
|
|
|
const osg::BoundingSphere bs = model->getBound();
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// create a bounding box around the sphere.
|
|
|
|
osg::BoundingBox bb;
|
|
|
|
bb.expandBy(bs);
|
|
|
|
|
|
|
|
// front
|
|
|
|
scene->addChild(createOccluder(bb.corner(0),
|
|
|
|
bb.corner(1),
|
|
|
|
bb.corner(5),
|
|
|
|
bb.corner(4)));
|
|
|
|
|
|
|
|
// right side
|
|
|
|
scene->addChild(createOccluder(bb.corner(1),
|
|
|
|
bb.corner(3),
|
|
|
|
bb.corner(7),
|
|
|
|
bb.corner(5)));
|
|
|
|
|
|
|
|
// left side
|
|
|
|
scene->addChild(createOccluder(bb.corner(2),
|
|
|
|
bb.corner(0),
|
|
|
|
bb.corner(4),
|
|
|
|
bb.corner(6)));
|
|
|
|
|
|
|
|
// back side
|
|
|
|
scene->addChild(createOccluder(bb.corner(3),
|
|
|
|
bb.corner(2),
|
|
|
|
bb.corner(6),
|
|
|
|
bb.corner(7),
|
|
|
|
0.5f)); // create a hole half the size of the occluder.
|
|
|
|
|
|
|
|
return scene;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// set up the usage document, in case we need to print out how to use this program.
|
2003-04-09 19:44:32 +08:00
|
|
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of convex planer occluders.");
|
2003-04-07 05:32:44 +08:00
|
|
|
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
|
2003-03-15 04:35:45 +08:00
|
|
|
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
2003-04-10 18:21:20 +08:00
|
|
|
arguments.getApplicationUsage()->addCommandLineOption("-m","Mannually create occluders");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// initialize the viewer.
|
2007-01-08 18:00:16 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-19 18:28:19 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
bool manuallyCreateOccluders = false;
|
2003-04-10 18:21:20 +08:00
|
|
|
while (arguments.read("-m")) { manuallyCreateOccluders = true; }
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2003-04-18 23:59:34 +08:00
|
|
|
if (manuallyCreateOccluders)
|
|
|
|
{
|
2007-01-08 18:00:16 +08:00
|
|
|
viewer.addEventHandler(new OccluderEventHandler(&viewer));
|
2003-04-18 23:59:34 +08:00
|
|
|
}
|
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// if user requests help write it out to cout.
|
2003-03-15 04:35:45 +08:00
|
|
|
if (arguments.read("-h") || arguments.read("--help"))
|
|
|
|
{
|
|
|
|
arguments.getApplicationUsage()->write(std::cout);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the nodes from the commandline arguments.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedmodel = osgDB::readRefNodeFiles(arguments);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// if not loaded assume no arguments passed in, try using default mode instead.
|
2015-10-22 21:42:19 +08:00
|
|
|
if (!loadedmodel) loadedmodel = osgDB::readRefNodeFile("glider.osgt");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
if (!loadedmodel)
|
|
|
|
{
|
2010-09-15 20:00:12 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
|
2003-03-15 04:35:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// run optimization over the scene graph
|
|
|
|
osgUtil::Optimizer optimzer;
|
|
|
|
optimzer.optimize(loadedmodel);
|
|
|
|
|
|
|
|
// add the occluders to the loaded model.
|
2003-03-19 18:28:19 +08:00
|
|
|
osg::ref_ptr<osg::Group> rootnode;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
if (manuallyCreateOccluders)
|
|
|
|
{
|
|
|
|
rootnode = new osg::Group;
|
|
|
|
rootnode->addChild(loadedmodel);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
else
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
rootnode = createOccludersAroundModel(loadedmodel.get());
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// add a viewport to the viewer and attach the scene graph.
|
2015-10-22 21:42:19 +08:00
|
|
|
viewer.setSceneData( rootnode );
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|