Archie OSGStep By Step② 写入OSG场景图形到文件(从文件读取OSG场景图形)

在第一课基础之上修改代码,其实仅需要增加两句话即可。也就是这两句

 bool result = osgDB::writeNodeFile(*(root.get()),"Simple.osg");
    if (!result)
    {
        osg::notify(osg::FATAL) << "Failed int osgDB::writeNodeFile()." << endl;
    }


在调用createSceneGraph()函数创建场景图形之后,这两行的代码将这个场景图形作为文件“Simple.osg”写入到磁盘中。文件格式.osg 是OSG 特有的ASCII编码文件格式。作为ASCII 文件,.osg 通常较大且载入较为缓慢,因此在产品级的代码中很少使用。不过,作为开发时的调试环境和快速演示,这种格式还是十分有用的。

对于文件IO操作,需要OSG的数据管理类,也就是osgDB模块。

对于文件格式需要#include <osgDB/Registry>进行注册

读取操作需要#include <osgDB/WriteFile>写入文件

完整代码如下

// Simple.cpp : 定义控制台应用程序的入口点。
//Archie OSG教程 第二课 场景图形写入到文件

#include "stdafx.h"
// Simple Example, Basic Geode and Geometry class usage
#include <osgViewer/Viewer>
#include <osg/ref_ptr>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <iostream>
using std::endl;

osg::ref_ptr<osg::Node> createSceneGraph();
int _tmain(int argc, _TCHAR* argv[])
{
    osg::ref_ptr<osg::Node> root = createSceneGraph();
    if (!root.valid())
    {
        osg::notify(osg::FATAL) << "Failed in createSceneGraph()." << endl;
        return 1;
    }
    bool result = osgDB::writeNodeFile(*(root.get()),"Simple.osg");
    if (!result)
    {
        osg::notify(osg::FATAL) << "Failed int osgDB::writeNodeFile()." << endl;
    }

    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    return viewer.run();
}

osg::ref_ptr<osg::Node> createSceneGraph()
{
    // Create an object to store geometry in.
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;

    // Create an array of four vertices.
#if 1
    // Using the push_back interface
    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
    geom->setVertexArray( v.get() );
    v->push_back( osg::Vec3( -1.f, 0.f, -1.f ) );
    v->push_back( osg::Vec3( 1.f, 0.f, -1.f ) );
    v->push_back( osg::Vec3( 1.f, 0.f, 1.f ) );
    v->push_back( osg::Vec3( -1.f, 0.f, 1.f ) );
#else
    // Using resize() and operator[]().
    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
    geom->setVertexArray( v.get() );
    v->resize( 4 );
    (*v)[ 0 ] = osg::Vec3( -1.f, 0.f, -1.f );
    (*v)[ 1 ] = osg::Vec3( 1.f, 0.f, -1.f );
    (*v)[ 2 ] = osg::Vec3( 1.f, 0.f, 1.f );
    (*v)[ 3 ] = osg::Vec3( -1.f, 0.f, 1.f );
#endif

    // Create an array of four colors.
    osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
    geom->setColorArray( c.get() );
    geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
    c->push_back( osg::Vec4( 1.f, 0.f, 0.f, 1.f ) );
    c->push_back( osg::Vec4( 0.f, 1.f, 0.f, 1.f ) );
    c->push_back( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) );
    c->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) );

    // Create an array for the single normal.
    osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
    geom->setNormalArray( n.get() );
    geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
    n->push_back( osg::Vec3( 0.f, -1.f, 0.f ) );

    // Draw a four-vertex quad from the stored data.
    geom->addPrimitiveSet(
        new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );

    // Add the Geometry (Drawable) to a Geode and return the Geode.
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable( geom.get() );
    return geode.get();
}


 

你可能感兴趣的:(Archie OSGStep By Step② 写入OSG场景图形到文件(从文件读取OSG场景图形))