osg::ComputeBoundsVisitor用法(一)

osg::ComputeBoundsVisitor用于获取模型或绘制几何体的最小包围盒。如下代码:


#include
#include
#include
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
	osg::ref_ptr spViewer = new osgViewer::Viewer();
	osg::ref_ptr spGroup = new osg::Group();

	osg::ref_ptr spNode = osgDB::readNodeFile("cow.osg");
	spGroup->addChild(spNode);
	spViewer->setSceneData(spGroup);
	osg::ComputeBoundsVisitor boundsVist;
	spNode->accept(boundsVist);

	osg::BoundingBox box = boundsVist.getBoundingBox();
	//OSG_NOTIFY(osg::ALWAYS) << "bound info:" << std::endl;;
	osg::notify(osg::ALWAYS) << "bouding box info" << std::endl;
	osg::notify(osg::ALWAYS) << "center x:" << box.center().x() << std::endl;
	osg::notify(osg::ALWAYS) << "center y:"<< box.center().y() << std::endl;;
	osg::notify(osg::ALWAYS) << "center z:" << box.center().z() << std::endl;;
	osg::notify(osg::ALWAYS) << "x max:" << box.xMax() << std::endl;
	osg::notify(osg::ALWAYS) << "x min:" << box.xMin() << std::endl;
	osg::notify(osg::ALWAYS) << "y max:" << box.yMax() << std::endl;
	osg::notify(osg::ALWAYS) << "y min:" << box.yMin() << std::endl;
	osg::notify(osg::ALWAYS) << "z max:" << box.zMax() << std::endl;
	osg::notify(osg::ALWAYS) << "z min:" << box.zMin() << std::endl;

	// 画个外框
	osg::ref_ptrspGeodeBox = new osg::Geode();
	spGeodeBox->addDrawable(new osg::ShapeDrawable(new osg::Box(box.center(), box.xMax() - box.xMin(), box.yMax() - box.yMin(), box.zMax() - box.zMin())));
	osg::ref_ptr spGeodeStateSet = spGeodeBox->getOrCreateStateSet();
	
   // 采用线框模式,以便能看见里面的模型即cow.osg
    osg::ref_ptr spGeodePolygon = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
	spGeodeStateSet->setAttribute(spGeodePolygon);
	spGroup->addChild(spGeodeBox);
	spViewer->setUpViewInWindow(10, 10, 800, 600);
	spViewer->realize();
	spViewer->run();
    return 0;
}

获取cow.osg模型最小包围盒的中心点及x、y、z坐标的最大、最小值,输入如下:

osg::ComputeBoundsVisitor用法(一)_第1张图片

注意:上面的osg::notify(osg::ALWAYS)输出信息时,后面要加回车换行,否则日志信息只输入到缓冲区,而不会立即输出到控制台

模型显示效果如下:

osg::ComputeBoundsVisitor用法(一)_第2张图片

osg::ComputeBoundsVisitor用法(一)_第3张图片

osg::ComputeBoundsVisitor用法(一)_第4张图片

你可能感兴趣的:(#,osg基础技术点,osg,ComputeBounds)