OSG得到OSGB中几何体顶点、法向量、三角形数据并可视化

本文介绍如何将OSGB文件读取到osg节点,并使用自定义的PositionVisitor遍历几何体数据,获取顶点、法向量和三角形信息。通过处理这些数据创建osg::Geode并在osg::Group中显示模型。此外,还讨论了如何以点云形式渲染模型,以清晰展示每个三角形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考这位大哥的博客https://blog.csdn.net/qq_31709249/article/details/94357183

代码放到我的github上面了

https://github.com/DwyaneLegend/osgb_data_analysis

以下是我的理解以及改动之后的代码

我先展示一下最终main函数中实现的核心代码

osgViewer::Viewer viewer;
			osg::ref_ptr<osg::Group> root = new osg::Group;
			osg::ref_ptr<osgDB::Options> options = new osgDB::Options("noRotation");
			for (size_t i = 0; i<fileNames.size(); i++)
			{
				string name = fileNames[i];
				osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(name);
				string modelName = Utility::getFileNameFromPath(name);
				PositionVisitor visitor = PositionVisitor(modelName,textSize);
				node->accept(visitor);
				root->addChild(visitor.createRandomColorOsgNode(i));				
			}
			//root->addChild(Utility::createCoorAxis(textSize));
			cout<<"num children"<<root->getNumChildren()<<endl;
			viewer.setSceneData(root);
			viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
			viewer.addEventHandler(new osgViewer::StatsHandler);
			viewer.setUpViewOnSingleScreen(1);//这里是单屏幕显示
			viewer.run();

首先将osgb文件读取到node中   name为osgb文件绝对路径

osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(name);

然后自己编写Positionvisitor类,继承于NodeVisitor,相当于访问器,用于访问一个node中drawable的所有Geom

PositionVisitor visitor = PositionVisitor(modelName,textSize);

 以下为Positionvisitor.h

class PositionVisitor
	:public osg::NodeVisitor
{
protected:
	vector<Geom*> allGeom;
	osg::Vec4 geomColor;
	string modelName;
	int textSize;//提示文字的大小
	osg::BoundingBox boundingBox;
public:
	virtual  void apply(osg::Geode& node) override;
	void dealTriangleInfo(ModelAttributeFunctor attributeFunctor,osg::TriangleIndexFunctor<TriangleIndex> indexFunctor);//处理访问器得到的信息,构建三角形关系
	osg::ref_ptr<osg::Node> createOsgNode(osg::Vec4 color,int order);//根据指定的颜色,将geom中的数据创建成osg节点
	osg::ref_ptr<osg::Node> createRandomColorOsgNode(int order);//将geom中的数据创建成osg节点,颜色随机
	osg::ref_ptr<osgText::Text> createTipText(short direction);//创建提示文字
	osg::ref_ptr<osgText::Text> createCenterText(osg::BoundingBox);//创建geom的中心点
	PositionVisitor(string ModelName);
	PositionVisitor(string ModelName, int TextSize);
	~PositionVisitor();
};

上面提到的Geom 如下,存储了顶点,三角形等信息,之后我再解释geom是什么

class Geom
{
public:
	vector<Vertex*> vertices;
	vector<Triangle*> triangles;
	osg::BoundingBox  boundingBox;
	bool isTwoTriangleNeighbor(int triangle1Index,int triangle2Index);
	osg::ref_ptr<osg::Geode> createOsgNode(osg::Vec4 color);
	osg::ref_ptr<osg::Geode> Geom::createOsgNode_Point(osg::Vec4 color);
	osg::ref_ptr<osg::Geode> Geom::createOsgNode_Triangle(osg::Vec4 color);
	Geom();
	~Geom();
};

然后node的accept函数可以调用visitor(基类NodeVisitor)的apply函数


                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值