繪製模型最小包圍盒輪廓

/* -*-c++-*- OpenSceneGraph Cookbook
 * Chapter 2 Recipe 3
 * Author: Wang Rui <wangray84 at gmail dot com>
*/

#include <osg/ComputeBoundsVisitor>
#include <osg/ShapeDrawable>
#include <osg/AnimationPath>
#include <osg/MatrixTransform>
#include <osg/PolygonMode>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/StateSet>
#include <osg/LineWidth>

osg::ref_ptr<osg::Geode> createboundingbox(osg::Node* node)
{
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	osg::ComputeBoundsVisitor boundvisitor ;
	node->accept(boundvisitor);
	osg::BoundingBox bb = boundvisitor.getBoundingBox();
  
	float lengthx = bb.xMax()-bb.xMin();
	float lengthy = bb.yMax()-bb.yMin();
	float lengthz = bb.zMax()-bb.zMin();
	osg::Vec3 center= osg::Vec3((bb.xMax()+bb.xMin())/2,(bb.yMax()+bb.yMin())/2,(bb.zMax()+bb.zMin())/2);

	osg::ref_ptr<osg::ShapeDrawable>  drawable = new osg::ShapeDrawable(new osg::Box(center,lengthx,lengthy,lengthz));
	drawable->setColor(osg::Vec4(1.0,1.0,0.0,1.0));

	osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
	stateset= drawable->getOrCreateStateSet();
	osg::ref_ptr<osg::PolygonMode> polygon = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
	stateset->setAttributeAndModes(polygon);

	//設置線寬
	osg::ref_ptr<osg::LineWidth> linewidth = new osg::LineWidth(3.0);
	stateset->setAttribute(linewidth);


	geode->addDrawable(drawable);
	return geode;

}
class InfoVisitor : public osg::NodeVisitor
{
public :
	InfoVisitor(): osg::NodeVisitor(TRAVERSE_ALL_CHILDREN), _indent(0) {}

	virtual void apply(osg::Node& node )
	{
		for (int i=0 ;i<_indent;i++)
			std::cout<<"  ";
		
			std::cout<<"["<<_indent+1<<"]"<<node.libraryName()<<"::"<<node.className()<<std::endl;
			_indent++;
			traverse(node);
			_indent--;		
	}

	virtual void apply( osg::Geode& node )
	{
		for ( int i=0; i<_indent; ++i ) std::cout << "  ";
		std::cout << "[" << _indent+1 << "] " << node.libraryName()
			<< "::" << node.className() << std::endl;

		for ( unsigned int n=0; n<node.getNumDrawables(); ++n )
		{
			osg::Drawable* drawable = node.getDrawable(n);
			if ( !drawable ) continue;

			for ( int i=0; i<=_indent; ++i ) std::cout << "  ";
			std::cout << drawable->libraryName() << "::"
				<< drawable->className() << std::endl;
		}

		_indent++;
		traverse( node );		
		_indent--;
	}


protected:
	int _indent;
	//osg::Group  _root;
};



int main( int argc, char** argv )
{
	osg::ref_ptr<osg::Group> root = new osg::Group;
	osg::ref_ptr<osg::Group> jxb = new osg::Group;
	osg::ref_ptr<osg::MatrixTransform>  axes = new osg::MatrixTransform;
	axes->setMatrix(osg::Matrix::translate(5,0,0));	
	axes->addChild(osgDB::readNodeFile("D:\\data\\axes.osg"));
	InfoVisitor infovisitor;	
	std::cout<<"-----------------------------------------------"<<std::endl;
	if(axes) axes->accept(infovisitor);

	
	root->addChild(axes);
	
	osg::ref_ptr<osg::Geode> geode2 = createboundingbox(axes);
	
	
	root->addChild(geode2);
    
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );	
    return viewer.run();
}


發佈了3 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章