[原][資料整理][osg]osgDB文件讀取插件,工做機制,支持格式,自定義插件

參考:php

osgPlugins相關node

osg讀取文件的原理(插件工做機制)數據庫

當使用osgDB讀取文件時,會自動根據文件的擴展名來到插件目錄中尋找相應的插件,來實現。設計模式

好比:網絡

osgviewer cow.osg

調用了osgdb_osg插件去讀*.osg文件。 curl

osgviewer model.flt

調用了osgdb_flt插件去讀*.flt文件。 函數

 

使用代碼咱們是使用以下代碼讀取寫入模型:字體

//讀取
#include <osgDB/ReadFile>

osg::ref_ptr<Node> node = osgDB::readNodeFile("cow.osg");
osg::ref_ptr<Image> image = osgDB::readImageFile("lz.rgb");

//保存
#include <osgDB/WriteFile>
osgDB::writeNodeFile(*node, "saved.osg");

 

使用OSG插件是用戶無需過度關心的事情,機制已經創建,當讀某文件時,它會本身去尋找相應的插件,當你須要讀定特文件的時候,就要確認這個文件的插件是否存在,若是不存在須要編譯。ui

OSG支持的模型類型

Plugin nameurl

Description

Extensions

Read

Write

Notes

3dc

3DC point cloud reader

.3dc .asc

yes

no

 

3ds

3D Studio

.3ds

yes

yes

 

ac3d

AC3D modeler

.ac

yes

yes

http://www.inivis.com/

bsp

Quake3 BSP

.bsp

yes

no

 

dae

COLLADA 1.4.x

.dae

yes

yes

Requires Collada DOM library. See Collada plugin details

dw

Design Workshop Database

.dw

yes

no

 

dxf

Autodesk DXF Reader

.dxf

yes

no

 

fbx

Autodesk FBX

.fbx

yes

yes

Requires Autodesk's FBX SDK. If using OpenSceneGraph 2.8.x you'll need version 2010.2 of the SDK. Otherwise checkReaderWriterFBX.h to see which version of the SDK is required (currently 2012.1). If you have problems loading an FBX model please check that it works in Autodesk's FBX QuickTime Viewer first to see if the model was correctly exported.

geo

Geo

.gem .geo

yes

no

 

Inventor

Open Inventor format

.iv .wrl

yes

yes

.wrl Only VRML1.0

ive

Native osg binary

.ive

yes

yes

May also read/write (internal) images

logo

Logo database

.logo

yes

no

 

lwo

Lightwave Object

.lwo .lw .geo

yes

no

 

lws

Lightwave Scene

.lws

yes

no

 

md2

Quake MD2

.md2

yes

no

 

obj

Alias Wavefront

.obj

yes

yes

Also writes materials to .mtl

ogr

 

.ogr

yes

no

 

Openflight

Multigen Openflight

.flt

yes

yes

See OpenFlight plugin details

osg

Native osg ascii

.osg

yes

yes

Currently the writer supports OVERRIDE/PROTECTED flags in "StateSets" only for "Modes" and not for "StateAttributes".

pfb

Performer loader

All extensions supported by Performer loaders

yes

no

Requires Performer library

shp

ESRI Shapefile

.shp

yes

no

 

stl

Stereolithography file

.stl .sta

yes

no

 

vrml

VRML2

.wrl

yes

no

Requires OpenVRML library

x

DirectX 3D model

.x

yes

no

 

圖片和視頻

Plugin name

Description

Extensions

Read

Write

Notes

bmp

Window bitmap

.bmp

yes

yes

 

dds

DirectX image format

.dds

yes

yes

 

gdal

GDAL image loaders

All extensions supported by GDAL

yes

no

 

gif

GIF Image

.gif

yes

no

 

hdr

High Dynamic Range image

.hdr

yes

yes

 

jp2

 

.jps .jpc

yes

yes

 

jpeg

Jpeg image

.jpeg .jpg

yes

yes

 

Openflight

Image attribute files

.attr

yes

yes

Stores additional attributes that are not stored in .rgb files

pic

PIC image

.pic

yes

no

 

png

Portable Network Graphics

.png

yes

yes

 

pnm

 

.pnm. ppm .pgm .pbm

yes

yes

 

rgb

 

.rgb .sgi .rgba .int .inta .bw

yes

yes

 

tga

Targa image

.tga

yes

no

 

tiff

Tiff image

.tiff .tif

yes

yes

 

quicktime

Quicktime movie loader

.mov .mpg .mpv .mp4 .m4v .dc .avi .flv .swf

yes

yes

 
 

Quicktime image loader

rgb. rgba .jpg .jpeg .tif .tiff .gif .png .pict .pct .tga .psd

yes

yes

 

xine

Xine image stream

.mpg .mpv .db .flv .mov .avi .wmv. xine

yes

no

 

壓縮以及網絡傳輸類型

Plugin name

Description

Extensions

Read

Write

Notes

curl

HTTP Protocol Model

.curl

yes

no

 

osga

         

txp

Terrapage file

.txp

yes

no

Produced by Terra Vista terrain building tools

net

         

zip

         

tgz

         

osgtgz

         

字體類型

Plugin name

Description

Extensions

Read

Write

Notes

freetype

Freetype font loader

.ttf .ttc .pfb .pfa .cid .cff .cef .fon .fnt

yes

no

 

txf

GLU texture font

.txf

yes

no

 

加載時能夠顯式加的矩陣操做

Plugin name

Description

Extensions

Read

Write

Notes

rot

Rotates to the loaded model

.rot

     

scale

Scales the loaded model

.scale

     

trans

Translates the loaded model

.trans

     

normals

Shows normals of the loaded model

.normals

     

 

如下來源:圖形碼農

osgDB負責維護插件的信息註冊表,並負責檢查將要被載入的OSG插件接口的合法性。

應用程序從文件中讀取各部分數據庫信息時,須要在不干擾當前渲染的前提下之後臺線程的方式進行。

osgDB::DatabaseParger提供了這樣的功能。

插件的工做機制

osg插件是一組動態連接庫,其中實現了osgDB頭文件ReaderWriter定義的接口。OSG不可能查找並加載全部的插件以獲取它們支持的文件格式,這樣,在程序啓動時將會是一個很大的開銷。所以,OSG使用職責鏈(Chain of Responsibility)的設計模式,以加載儘可能少的插件。當用戶程序嘗試使用osgDB讀取或寫入文件時,OSG將按照以下步驟來查找合適的插件。

  1. OSG搜索已註冊的插件列表,查找支持文件格式的插件。開始時已註冊插件列表僅包含了Registry類構造函數中註冊的插件。若是OSG找到了能夠支持此文件格式的插件,併成功執行了I/O操做,那麼它將返回相應的數據。
  2. 若是沒有發現能夠支持此格式的已註冊插件,或者I/0操做失敗,那麼OSG將根據前面所述的文件命名規則建立插件文件的名稱,並嘗試讀取相應的插件庫。若是讀取成功,OSG將添加此插件到已註冊插件列表中。
  3. OSG將重複步驟(1),若是文件I/O的操做再次失敗,OSG將返回失敗信息。

總的來講,用戶沒必要了解OSG內部如何實現文件I/O操做,就可使用插件順利工做。反之,若是文件I/O操做失敗,用戶也能夠根據給出的錯誤信息跟蹤插件源代碼中的相關內容。其在程序中代碼的實現順序以下圖所示:


主要的實現的代碼是在ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)函數中

ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
{
    // first attempt to load the file from existing ReaderWriter's
    //看是否有可用的ReaderWriter,對當前的數據進行解析,若是解析成功,就返回結果
    AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
    for(;itr.valid();++itr)
    {
        ReaderWriter::ReadResult rr = readFunctor.doRead(*itr);
        if (readFunctor.isValid(rr)) return rr;
        else results.push_back(rr);
    }
 
 
    // now look for a plug-in to load the file. 
    //根據文件名稱建立新的動態庫名稱,而後加載動態庫,增長_rwList對象個數
    std::string libraryName = createLibraryNameForFile(readFunctor._filename);
    if (loadLibrary(libraryName)!=NOT_LOADED)
    {
        //從新遍歷一下,使用新的ReaderWriter進行數據的解析
        for(;itr.valid();++itr)
        {
            ReaderWriter::ReadResult rr = readFunctor.doRead(*itr);
            if (readFunctor.isValid(rr)) return rr;
            else results.push_back(rr);
        }
    }
}

 

自定義文件插件:

自定義文件插件主要是自定義一個插件讀寫類,繼承osgDB::ReaderWriter類,而後根據需求重寫如readNode等函數方法便可

 

在創建插件讀寫類時須要注意:

  • 須要創建一個dll項目工程,輸出的dll必須爲osgdb_擴展名.dll或osgdb_擴展名d.dll的形式
  • 爲了實現插件註冊,須要定義全局變量,方法以下REGISTER_OSGPLUGIN(VR, ReaderWriterVR),在該全局變量的初始化過程當中,會使用Registery::addReaderWriter函數自動註冊插件所對應的擴展名。
  • 在應用程序使用中,須要註冊插件,方法以下 osgDB::Registry::instance->addFileExtensionAlias("VR", "VR")
相關文章
相關標籤/搜索