在使用OSG(OpenSceneGraph)存储带纹理osgb格式的过程中,大家会遇到这样一种情况:存储后的osgb文件所占用的大小远大于原始文件的大小,几倍至几十倍。这是为何呢?原因是OSG默认的存储格式是不压缩存储,所以解决方案就是设置参数将存储格式改为压缩存储。方法如下:
osg::ref_ptr<osgDB::ReaderWriter::Options> options = new osgDB::ReaderWriter::Options; options->setOptionString("WriteImageHint=IncludeFile"); // 设置压缩 osgDB::writeNodeFile(*(node.get()), osgb_path, options);
如下文档可以找到解决方法的来源:
$ osgconv --format osgb Plugin osgPlugins-3.3.9/osgdb_osg.so { ReaderWriter : OSG Reader/Writer { features : readObject readNode writeObject writeNode extensions : .osg OpenSceneGraph Ascii file format extensions : .osgs Pseudo OpenSceneGraph file loaded, with file encoded in filename string options : OutputTextureFiles Write out the texture images to file options : includeExternalReferences Export option options : precision Set the floating point precision when writing out files options : writeExternalReferenceFiles Export option } ReaderWriter : OpenSceneGraph Native Format Reader/Writer { features : readObject readImage readNode writeObject writeImage writeNode extensions : .osg2 OpenSceneGraph extendable format extensions : .osgb OpenSceneGraph extendable binary format extensions : .osgt OpenSceneGraph extendable ascii format extensions : .osgx OpenSceneGraph extendable XML format options : Ascii Import/Export option: Force reading/writing ascii file options : Compressor=<name> Export option: Use an inbuilt or user-defined compressor options : ForceReadingImage Import option: Load an empty image instead if required file missed options : SchemaData Export option: Record inbuilt schema data into a binary file options : SchemaFile=<file> Import/Export option: Use/Record an ascii schema file options : WriteImageHint=<hint> Export option: Hint of writing image to stream: <IncludeData> writes Image::data() directly; <IncludeFile> writes the image file itself to stream; <UseExternal> writes only the fi lename; <WriteOut> writes Image::data() to disk as external file. options : XML Import/Export option: Force reading/writing XML file } }
上面文档中,有关WriteImageHint的项的描述(第26行):选择IncludeData是直接写入数据,无压缩,是默认选项。而includeFile则是写入流,按我的理解,就是将原始的影像数据流原原本本的写入osgb,原始的影像是压缩过的,比如jpg格式,那保存出来的osgb就是压缩过的。因此,将WriteImageHint设置为IncludeFile可以解决OSG存储带纹理osgb格式数据量大的问题,节省空间。