问题的提出:
用如下代码,加载一个osg文件
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Geometry>
#include <osg/Camera>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include<osg/PolygonMode>
#include <iostream>
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
//初始化场景根节点
osg::ref_ptr<osg::Group> root = new osg::Group;
//场景数据
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("d:/gzbody.OSGB");
root->addChild(node);
//将场景数据加入视景器中
viewer->setSceneData(root);
viewer->run();
return 0;
}
展示的模型如下所示:
用户说有时需要提供线框模式,即像下图那样:
代码如下:
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Geometry>
#include <osg/Camera>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include<osg/PolygonMode>
#include <iostream>
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
// 初始化场景根节点
osg::ref_ptr<osg::Group> root = new osg::Group;
// 场景数据
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("d:/gzbody.OSGB");
osg::ref_ptr<osg::StateSet>spStateSet = node->getOrCreateStateSet();
osg::ref_ptr<osg::PolygonMode> polyMode = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
spStateSet->setAttribute(polyMode);
root->addChild(node);
// 将场景数据加入视景器中
viewer->setSceneData(root);
viewer->run();
return 0;
}