| 1 | #include <osg/Geometry> |
|---|
| 2 | #include <osgDB/ObjectWrapper> |
|---|
| 3 | #include <osgDB/InputStream> |
|---|
| 4 | #include <osgDB/OutputStream> |
|---|
| 5 | |
|---|
| 6 | BEGIN_USER_TABLE( AttributeBinding, osg::Geometry ); |
|---|
| 7 | ADD_USER_VALUE( BIND_OFF ); |
|---|
| 8 | ADD_USER_VALUE( BIND_OVERALL ); |
|---|
| 9 | ADD_USER_VALUE( BIND_PER_PRIMITIVE_SET ); |
|---|
| 10 | ADD_USER_VALUE( BIND_PER_PRIMITIVE ); |
|---|
| 11 | ADD_USER_VALUE( BIND_PER_VERTEX ); |
|---|
| 12 | END_USER_TABLE() |
|---|
| 13 | |
|---|
| 14 | USER_READ_FUNC( AttributeBinding, readAttributeBinding ) |
|---|
| 15 | USER_WRITE_FUNC( AttributeBinding, writeAttributeBinding ) |
|---|
| 16 | |
|---|
| 17 | static void readArrayData( osgDB::InputStream& is, osg::Geometry::ArrayData& data ) |
|---|
| 18 | { |
|---|
| 19 | bool hasArray = false; |
|---|
| 20 | is >> osgDB::PROPERTY("Array") >> hasArray; |
|---|
| 21 | if ( hasArray ) data.array = is.readArray(); |
|---|
| 22 | |
|---|
| 23 | bool hasIndices = false; |
|---|
| 24 | is >> osgDB::PROPERTY("Indices") >> hasIndices; |
|---|
| 25 | if ( hasIndices ) data.indices = dynamic_cast<osg::IndexArray*>( is.readArray() ); |
|---|
| 26 | |
|---|
| 27 | is >> osgDB::PROPERTY("Binding"); |
|---|
| 28 | data.binding = (osg::Geometry::AttributeBinding)readAttributeBinding(is); |
|---|
| 29 | |
|---|
| 30 | int normalizeValue = 0; |
|---|
| 31 | is >> osgDB::PROPERTY("Normalize") >> normalizeValue; |
|---|
| 32 | data.normalize = normalizeValue; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | static void writeArrayData( osgDB::OutputStream& os, const osg::Geometry::ArrayData& data ) |
|---|
| 36 | { |
|---|
| 37 | os << osgDB::PROPERTY("Array") << data.array.valid(); |
|---|
| 38 | if ( data.array.valid() ) os << data.array.get(); |
|---|
| 39 | else os << std::endl; |
|---|
| 40 | |
|---|
| 41 | os << osgDB::PROPERTY("Indices") << data.indices.valid(); |
|---|
| 42 | if ( data.indices.valid() ) os << data.indices.get(); |
|---|
| 43 | else os << std::endl; |
|---|
| 44 | |
|---|
| 45 | os << osgDB::PROPERTY("Binding"); writeAttributeBinding(os, data.binding); os << std::endl; |
|---|
| 46 | os << osgDB::PROPERTY("Normalize") << (int)data.normalize << std::endl; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | #define ADD_ARRAYDATA_FUNCTIONS( PROP ) \ |
|---|
| 50 | static bool check##PROP( const osg::Geometry& geom ) \ |
|---|
| 51 | { return geom.get##PROP().array.valid(); } \ |
|---|
| 52 | static bool read##PROP( osgDB::InputStream& is, osg::Geometry& geom ) { \ |
|---|
| 53 | osg::Geometry::ArrayData data; \ |
|---|
| 54 | is >> osgDB::BEGIN_BRACKET; readArrayData(is, data); \ |
|---|
| 55 | is >> osgDB::END_BRACKET; \ |
|---|
| 56 | geom.set##PROP(data); \ |
|---|
| 57 | return true; \ |
|---|
| 58 | } \ |
|---|
| 59 | static bool write##PROP( osgDB::OutputStream& os, const osg::Geometry& geom ) { \ |
|---|
| 60 | os << osgDB::BEGIN_BRACKET << std::endl; \ |
|---|
| 61 | writeArrayData(os, geom.get##PROP()); \ |
|---|
| 62 | os << osgDB::END_BRACKET << std::endl; \ |
|---|
| 63 | return true; \ |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | ADD_ARRAYDATA_FUNCTIONS( VertexData ) |
|---|
| 67 | ADD_ARRAYDATA_FUNCTIONS( NormalData ) |
|---|
| 68 | ADD_ARRAYDATA_FUNCTIONS( ColorData ) |
|---|
| 69 | ADD_ARRAYDATA_FUNCTIONS( SecondaryColorData ) |
|---|
| 70 | ADD_ARRAYDATA_FUNCTIONS( FogCoordData ) |
|---|
| 71 | |
|---|
| 72 | #define ADD_ARRAYLIST_FUNCTIONS( PROP, LISTNAME ) \ |
|---|
| 73 | static bool check##PROP( const osg::Geometry& geom ) \ |
|---|
| 74 | { return geom.get##LISTNAME().size()>0; } \ |
|---|
| 75 | static bool read##PROP( osgDB::InputStream& is, osg::Geometry& geom ) { \ |
|---|
| 76 | unsigned int size = is.readSize(); is >> osgDB::BEGIN_BRACKET; \ |
|---|
| 77 | for ( unsigned int i=0; i<size; ++i ) { \ |
|---|
| 78 | osg::Geometry::ArrayData data; \ |
|---|
| 79 | is >> osgDB::PROPERTY("Data") >> osgDB::BEGIN_BRACKET; \ |
|---|
| 80 | readArrayData(is, data); \ |
|---|
| 81 | is >> osgDB::END_BRACKET; geom.set##PROP(i, data); } \ |
|---|
| 82 | is >> osgDB::END_BRACKET; \ |
|---|
| 83 | return true; \ |
|---|
| 84 | } \ |
|---|
| 85 | static bool write##PROP( osgDB::OutputStream& os, const osg::Geometry& geom ) { \ |
|---|
| 86 | const osg::Geometry::ArrayDataList& LISTNAME = geom.get##LISTNAME(); \ |
|---|
| 87 | os.writeSize(LISTNAME.size()); os << osgDB::BEGIN_BRACKET << std::endl; \ |
|---|
| 88 | for ( osg::Geometry::ArrayDataList::const_iterator itr=LISTNAME.begin(); \ |
|---|
| 89 | itr!=LISTNAME.end(); ++itr ) { \ |
|---|
| 90 | os << osgDB::PROPERTY("Data") << osgDB::BEGIN_BRACKET << std::endl; \ |
|---|
| 91 | writeArrayData(os, *itr); os << osgDB::END_BRACKET << std::endl; \ |
|---|
| 92 | } \ |
|---|
| 93 | os << osgDB::END_BRACKET << std::endl; \ |
|---|
| 94 | return true; \ |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | ADD_ARRAYLIST_FUNCTIONS( TexCoordData, TexCoordArrayList ) |
|---|
| 98 | ADD_ARRAYLIST_FUNCTIONS( VertexAttribData, VertexAttribArrayList ) |
|---|
| 99 | |
|---|
| 100 | struct GeometryFinishedObjectReadCallback : public osgDB::FinishedObjectReadCallback |
|---|
| 101 | { |
|---|
| 102 | virtual void objectRead(osgDB::InputStream&, osg::Object& obj) |
|---|
| 103 | { |
|---|
| 104 | osg::Geometry& geometry = static_cast<osg::Geometry&>(obj); |
|---|
| 105 | if (geometry.getUseVertexBufferObjects()) |
|---|
| 106 | { |
|---|
| 107 | geometry.setUseVertexBufferObjects(false); |
|---|
| 108 | geometry.setUseVertexBufferObjects(true); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | REGISTER_OBJECT_WRAPPER( Geometry, |
|---|
| 114 | new osg::Geometry, |
|---|
| 115 | osg::Geometry, |
|---|
| 116 | "osg::Object osg::Drawable osg::Geometry" ) |
|---|
| 117 | { |
|---|
| 118 | ADD_LIST_SERIALIZER( PrimitiveSetList, osg::Geometry::PrimitiveSetList ); |
|---|
| 119 | ADD_USER_SERIALIZER( VertexData ); |
|---|
| 120 | ADD_USER_SERIALIZER( NormalData ); |
|---|
| 121 | ADD_USER_SERIALIZER( ColorData ); |
|---|
| 122 | ADD_USER_SERIALIZER( SecondaryColorData ); |
|---|
| 123 | ADD_USER_SERIALIZER( FogCoordData ); |
|---|
| 124 | ADD_USER_SERIALIZER( TexCoordData ); |
|---|
| 125 | ADD_USER_SERIALIZER( VertexAttribData ); |
|---|
| 126 | ADD_BOOL_SERIALIZER( FastPathHint, true ); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | wrapper->addFinishedObjectReadCallback( new GeometryFinishedObjectReadCallback() ); |
|---|
| 130 | } |
|---|