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();
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值