参考这位大哥的博客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函数
node->accept(visitor);
我们自己重写apply函数 通过node得到drawable,然后drawable的accep