root/OpenSceneGraph/trunk/src/osgWrappers/serializers/osg/Geometry.cpp @ 13502

Revision 13502, 6.2 kB (checked in by robert, 6 hours ago)

From Mattias Helsing, "Seems I was only half right given what you asked for. CMP0017 only
says that modules that are found and ran from cmake modules dir should
prefer cmake-provided modules. find_package() and include() still look
in CMAKE_MODULE_PATH first.

After some investigating I've come up with a proposal examplified in
the attached FindGDAL.cmake script. It simply calls the cmake provided
FindGDAL.cmake if it exists and returns if it succeeds in finding GDAL
using that, otherwise continue with our local cmake code.
Pro: Wont clutter our root CMakeLists.txt
Con: If we begin to write more advanced Findxxx modules (using
COMPONENTS, REQUIRED etc.) we may have to revise this scheme.
"

  • Property svn:eol-style set to native
Line 
1#include <osg/Geometry>
2#include <osgDB/ObjectWrapper>
3#include <osgDB/InputStream>
4#include <osgDB/OutputStream>
5
6BEGIN_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    lookup->add("BIND_PER_PRIMITIVE",3); //ADD_USER_VALUE( BIND_PER_PRIMITIVE );
11    ADD_USER_VALUE( BIND_PER_VERTEX );
12END_USER_TABLE()
13
14USER_READ_FUNC( AttributeBinding, readAttributeBinding )
15USER_WRITE_FUNC( AttributeBinding, writeAttributeBinding )
16
17static osg::Array* readArray( osgDB::InputStream& is)
18{
19    osg::ref_ptr<osg::Array> array;
20    bool hasArray = false;
21    is >> is.PROPERTY("Array") >> hasArray;
22    if ( hasArray ) array = is.readArray();
23
24    bool hasIndices = false;
25    is >> is.PROPERTY("Indices") >> hasIndices;
26    if ( hasIndices )
27    {
28        osg::ref_ptr<osg::Array> indices_array = is.readArray();
29        osg::ref_ptr<osg::IndexArray> indices = dynamic_cast<osg::IndexArray*>( indices_array.get() );
30        if (array.valid() && indices.valid()) array->setUserData(indices.get());
31    }
32
33    is >> is.PROPERTY("Binding");
34    int binding = readAttributeBinding(is);
35    if (array.valid()) array->setBinding(static_cast<osg::Array::Binding>(binding));
36
37    int normalizeValue = 0;
38    is >> is.PROPERTY("Normalize") >> normalizeValue;
39    if (array.valid()) array->setNormalize(normalizeValue!=0);
40   
41    return array.release();
42}
43
44static void writeArray( osgDB::OutputStream& os, const osg::Array* array)
45{
46    os << os.PROPERTY("Array") << (array!=0);
47    if ( array!=0 ) os << array;
48    else os << std::endl;
49
50    const osg::IndexArray* indices = (array!=0) ? dynamic_cast<const osg::IndexArray*>(array->getUserData()) : 0;
51    os << os.PROPERTY("Indices") << (indices!=0);
52    if ( indices!=0 ) os << indices;
53    else os << std::endl;
54
55    os << os.PROPERTY("Binding"); writeAttributeBinding(os, (array!=0) ? static_cast<osg::Geometry::AttributeBinding>(array->getBinding()) : osg::Geometry::BIND_OFF); os << std::endl;
56    os << os.PROPERTY("Normalize") << ((array!=0 && array->getNormalize()) ? 1:0) << std::endl;
57}
58
59#define ADD_ARRAYDATA_FUNCTIONS( ORIGINAL_PROP, PROP ) \
60    static bool check##ORIGINAL_PROP( const osg::Geometry& geom ) \
61    { return geom.get##PROP()!=0; } \
62    static bool read##ORIGINAL_PROP( osgDB::InputStream& is, osg::Geometry& geom ) { \
63        is >> is.BEGIN_BRACKET; \
64        osg::Array* array = readArray(is); \
65        geom.set##PROP(array); \
66        is >> is.END_BRACKET; \
67        return true; \
68    } \
69    static bool write##ORIGINAL_PROP( osgDB::OutputStream& os, const osg::Geometry& geom ) { \
70        os << os.BEGIN_BRACKET << std::endl; \
71        writeArray(os, geom.get##PROP()); \
72        os << os.END_BRACKET << std::endl; \
73        return true; \
74    }
75
76ADD_ARRAYDATA_FUNCTIONS( VertexData, VertexArray )
77ADD_ARRAYDATA_FUNCTIONS( NormalData, NormalArray )
78ADD_ARRAYDATA_FUNCTIONS( ColorData, ColorArray )
79ADD_ARRAYDATA_FUNCTIONS( SecondaryColorData, SecondaryColorArray )
80ADD_ARRAYDATA_FUNCTIONS( FogCoordData, FogCoordArray )
81
82#define ADD_ARRAYLIST_FUNCTIONS( ORIGINAL_PROP, PROP, LISTNAME ) \
83    static bool check##ORIGINAL_PROP( const osg::Geometry& geom ) \
84    { return geom.get##LISTNAME().size()>0; } \
85    static bool read##ORIGINAL_PROP( osgDB::InputStream& is, osg::Geometry& geom ) { \
86        unsigned int size = is.readSize(); is >> is.BEGIN_BRACKET; \
87        for ( unsigned int i=0; i<size; ++i ) { \
88            is >> is.PROPERTY("Data") >> is.BEGIN_BRACKET; \
89            osg::Array* array = readArray(is); \
90            geom.set##PROP(i, array); \
91            is >> is.END_BRACKET; } \
92        is >> is.END_BRACKET; \
93        return true; \
94    } \
95    static bool write##ORIGINAL_PROP( osgDB::OutputStream& os, const osg::Geometry& geom ) { \
96        const osg::Geometry::ArrayList& LISTNAME = geom.get##LISTNAME(); \
97        os.writeSize(LISTNAME.size()); os << os.BEGIN_BRACKET << std::endl; \
98        for ( osg::Geometry::ArrayList::const_iterator itr=LISTNAME.begin(); \
99              itr!=LISTNAME.end(); ++itr ) { \
100            os << os.PROPERTY("Data") << os.BEGIN_BRACKET << std::endl; \
101            writeArray(os, itr->get()); os << os.END_BRACKET << std::endl; \
102        } \
103        os << os.END_BRACKET << std::endl; \
104        return true; \
105    }
106
107ADD_ARRAYLIST_FUNCTIONS( TexCoordData, TexCoordArray, TexCoordArrayList )
108ADD_ARRAYLIST_FUNCTIONS( VertexAttribData, VertexAttribArray, VertexAttribArrayList )
109
110struct GeometryFinishedObjectReadCallback : public osgDB::FinishedObjectReadCallback
111{
112    virtual void objectRead(osgDB::InputStream&, osg::Object& obj)
113    {
114        osg::Geometry& geometry = static_cast<osg::Geometry&>(obj);
115        if (geometry.getUseVertexBufferObjects())
116        {
117            geometry.setUseVertexBufferObjects(false);
118            geometry.setUseVertexBufferObjects(true);
119        }
120    }
121};
122
123// implement backwards compatibility with reading/writing the FastPathHint
124static bool checkFastPathHint( const osg::Geometry& geom ) { return false; }
125static bool readFastPathHint( osgDB::InputStream& is, osg::Geometry& geom )
126{
127    bool value = false;
128    if ( is.isBinary() )
129    {
130        is >> value;
131    }
132    else if ( is.matchString("FastPathHint") )
133    {
134        is >> value;
135    }
136    return true;
137}
138static bool writeFastPathHint( osgDB::OutputStream& os, const osg::Geometry& geom )
139{
140    return true;
141}
142
143REGISTER_OBJECT_WRAPPER( Geometry,
144                         new osg::Geometry,
145                         osg::Geometry,
146                         "osg::Object osg::Drawable osg::Geometry" )
147{
148    ADD_LIST_SERIALIZER( PrimitiveSetList, osg::Geometry::PrimitiveSetList );  // _primitives
149    ADD_USER_SERIALIZER( VertexData );  // _vertexData
150    ADD_USER_SERIALIZER( NormalData );  // _normalData
151    ADD_USER_SERIALIZER( ColorData );  // _colorData
152    ADD_USER_SERIALIZER( SecondaryColorData );  // _secondaryColorData
153    ADD_USER_SERIALIZER( FogCoordData );  // _fogCoordData
154    ADD_USER_SERIALIZER( TexCoordData );  // _texCoordList
155    ADD_USER_SERIALIZER( VertexAttribData );  // _vertexAttribList
156
157    ADD_USER_SERIALIZER( FastPathHint );  // _fastPathHint
158
159    wrapper->addFinishedObjectReadCallback( new GeometryFinishedObjectReadCallback() );
160}
Note: See TracBrowser for help on using the browser.