osg::ComputeBoundsVisitor用法(一)

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


#include<osg/Geometry>
#include<osg/Geode>
#include<osgViewer/Viewer>
#include<osgViewer/ViewerEventHandlers>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include<osg/DrawPixels>
#include<osg/PositionAttitudeTransform>
#include<osg/computeBoundsVisitor>
#include<osg/MatrixTransform>
#include<osg/ShapeDrawable>
#include<osg/Shape>
#include<osg/PolygonMode>
int main(int argc, char *argv[])
{
	osg::ref_ptr<osgViewer::Viewer> spViewer = new osgViewer::Viewer();
	osg::ref_ptr<osg::Group> spGroup = new osg::Group();

	osg::ref_ptr<osg::Node> 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_ptr<osg::Geode>spGeodeBox = 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<osg::StateSet> spGeodeStateSet = spGeodeBox->getOrCreateStateSet();
	
   // 采用线框模式,以便能看见里面的模型即cow.osg
    osg::ref_ptr<osg::PolygonMode> 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::notify(osg::ALWAYS)输出信息时,后面要加回车换行,否则日志信息只输入到缓冲区,而不会立即输出到控制台

模型显示效果如下:

osg::CullSettings是OpenSceneGraph(OSG)中的一个类,用于控制场景的剔除(culling),即确定哪些节点需要在屏幕上进行渲染。 osg::CullSettings类具有以下一些重要的成员变量和函数: - float _nearFarRatio: 近平面和远平面之间的比率,默认为0.0001。 - float _pixelSizeScale: 像素大小的缩放因子,默认为1.0。 - float _smallFeatureCullingPixelSize: 小特征剔除的像素大小,默认为2.0。 - float _minimumShadowMapNearFarRatio: 阴影贴图近平面和远平面之间的最小比率,默认为0.1。 - bool _computeNearFarMode: 是否自动计算近平面和远平面,默认为osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR。 - void setCullingMode(osg::CullSettings::CullingMode mode): 设置剔除模式,默认为osg::CullSettings::DEFAULT_CULLING。 - void setNearFarRatio(float ratio): 设置近平面和远平面之间的比率。 - void setPixelSizeScale(float scale): 设置像素大小的缩放因子。 - void setSmallFeatureCullingPixelSize(float size): 设置小特征剔除的像素大小。 - void setMinimumShadowMapNearFarRatio(float ratio): 设置阴影贴图近平面和远平面之间的最小比率。 - void setComputeNearFarMode(osg::CullSettings::ComputeNearFarMode mode): 设置自动计算近平面和远平面的模式。 osg::CullSettings一般是通过设置osg::Camera的 setCullSettings() 函数来使用的,例如: ``` osg::ref_ptr<osg::Camera> camera = new osg::Camera; osg::ref_ptr<osg::CullSettings> cullSettings = new osg::CullSettings; cullSettings->setNearFarRatio(0.0001f); camera->setCullSettings(cullSettings.get()); ``` 这样就设置了一个近平面和远平面之间的比率为0.0001的剔除设置,并将其应用于相机对象camera上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值