Archie OSGStep By Step② 写入OSG场景图形到文件(从文件读取OSG场景图形)
来源:互联网 发布:qq群机器人源码 编辑:程序博客网 时间:2024/06/08 05:33
在第一课基础之上修改代码,其实仅需要增加两句话即可。也就是这两句
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场景图形)
- Archie OSG Step By Step④ MFC+OSG添加事件
- OSG多线程,多显示场景图形设计
- Archie OSG Step By Step⑤ 显示位置及拾取
- Archie OSG Step By Step⑥ 对象选取
- Archie OSG Step By Step( 0) -OpenSceneGraph环境设置及第一个程序
- OSG入门_场景图形与OpenSceneGraph概述01
- OSG入门_场景图形与OpenSceneGraph概述02
- Archie OSG Step By Step③ OSG+VS2010+MFC单文档程序框架构建
- OSG中读取osg文件到geode中
- Archie OSGStep By Step① 绘制简单多边形
- OSG场景漫游(二)
- OSG:场景管理
- OSG实现场景漫游
- osg场景中的树
- OSG场景漫游(一)
- OSG场景保存
- OSG场景保存
- ios开发点滴-常量使用
- 2012年末总结:博客想要活下去很简单也很难
- yii框架使用心得(实用性文档)
- 玩转vim之第二篇之如何复制文本到vim
- 网站运营:应从网站数据中给网站找出路
- Archie OSGStep By Step② 写入OSG场景图形到文件(从文件读取OSG场景图形)
- Double Squares(Python版)
- SQL Server 时间格式化
- 垂直行业网站建设风险的讨论
- 转:vim的复制粘贴小结
- [MSDN].msdn_oct_2001我的资源链接
- 小米成功启示录:完美运用二八法则和心理营销
- 科技巨头们2013都有什么计划?
- 移植UDT到android