Added Vector serialization and support in lua plugin top enable script users to set/get vector properties such as osg::Array, osg::PrimitiveSet and children lists.

This commit is contained in:
Robert Osfield
2014-02-24 10:19:48 +00:00
parent 6d68718fba
commit 4ef5d9eb5f
15 changed files with 1566 additions and 103 deletions

View File

@@ -189,6 +189,22 @@ const Array* Geometry::getTexCoordArray(unsigned int index) const
else return 0;
}
void Geometry::setTexCoordArrayList(const ArrayList& arrayList)
{
_texCoordList = arrayList;
dirtyDisplayList();
if (_useVertexBufferObjects)
{
for(ArrayList::iterator itr = _texCoordList.begin();
itr != _texCoordList.end();
++itr)
{
addVertexBufferObjectIfRequired(itr->get());
}
}
}
void Geometry::setVertexAttribArray(unsigned int index, Array* array, osg::Array::Binding binding)
{
@@ -216,6 +232,23 @@ const Array *Geometry::getVertexAttribArray(unsigned int index) const
else return 0;
}
void Geometry::setVertexAttribArrayList(const ArrayList& arrayList)
{
_vertexAttribList = arrayList;
dirtyDisplayList();
if (_useVertexBufferObjects)
{
for(ArrayList::iterator itr = _vertexAttribList.begin();
itr != _vertexAttribList.end();
++itr)
{
addVertexBufferObjectIfRequired(itr->get());
}
}
}
bool Geometry::addPrimitiveSet(PrimitiveSet* primitiveset)
{
@@ -712,7 +745,7 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
// draw the primitives themselves.
//
drawPrimitivesImplementation(renderInfo);
// unbind the VBO's if any are used.
state.unbindVertexBufferObject();
state.unbindElementBufferObject();

View File

@@ -12,7 +12,6 @@
*/
// Written by Wang Rui, (C) 2010
#include <osg/Version>
#include <osg/Notify>
#include <osgDB/FileUtils>
#include <osgDB/WriteFile>

View File

@@ -239,6 +239,9 @@ PropertyInterface::PropertyInterface():
TYPENAME(VEC4US)
TYPENAME(VEC4I)
TYPENAME(VEC4UI)
TYPENAME(LIST)
TYPENAME(VECTOR)
}
@@ -371,7 +374,7 @@ bool PropertyInterface::copyPropertyDataToObject(osg::Object* object, const std:
}
else
{
OSG_NOTICE<<"PropertyInterface::copyPropertyDataToObject() Types are not compatible, valueType = "<<valueType<<", destinationType="<<destinationType<<std::endl;
OSG_NOTICE<<"PropertyInterface::copyPropertyDataToObject() Types are not compatible, valueType = "<<valueType<<" ["<<getTypeName(valueType)<<"] , destinationType="<<destinationType<<" ["<<getTypeName(destinationType)<<"]"<<std::endl;
return false;
}
}
@@ -394,7 +397,7 @@ bool PropertyInterface::copyPropertyObjectFromObject(const osg::Object* object,
}
else
{
OSG_NOTICE<<"PropertyInterface::copyPropertyObjectFromObject() Types are not compatible, valueType = "<<valueType<<", destinationType="<<sourceType<<std::endl;
OSG_NOTICE<<"PropertyInterface::copyPropertyObjectFromObject() Types are not compatible, valueType = "<<valueType<<" ["<<getTypeName(valueType)<<"] , sourceType="<<sourceType<<" ["<<getTypeName(sourceType)<<"]"<<std::endl;
return false;
}
}
@@ -636,6 +639,7 @@ bool PropertyInterface::hasMethod(const osg::Object* object, const std::string&
return hasMethod(object->getCompoundClassName(), methodName);
}
} // end of osgDB namespace

File diff suppressed because it is too large Load Diff

View File

@@ -29,6 +29,56 @@ namespace lua
// forward declare
class LuaScriptEngine;
struct SerializerScratchPad : public osg::Referenced
{
SerializerScratchPad(unsigned int s=256):dataType(osgDB::BaseSerializer::RW_UNDEFINED),dataSize(0) { maxDataSize = s; data = new char[s]; }
unsigned int maxDataSize;
char* data;
osgDB::BaseSerializer::Type dataType;
unsigned int dataSize;
void reset()
{
dataType = osgDB::BaseSerializer::RW_UNDEFINED;
dataSize = 0;
}
template<typename T>
bool set(const T& t)
{
if (sizeof(T)<=maxDataSize)
{
*(reinterpret_cast<T*>(data)) = t;
dataType = osgDB::getTypeEnum<T>();
dataSize = sizeof(T);
return true;
}
else
{
dataSize = 0;
dataType = osgDB::BaseSerializer::RW_UNDEFINED;
return false;
}
}
template<typename T>
bool get(T& t) const
{
if (sizeof(T)==dataSize && dataType == osgDB::getTypeEnum<T>())
{
t = *(reinterpret_cast<T*>(data));
return true;
}
else
{
return false;
}
}
};
class LuaScriptEngine : public osg::ScriptEngine
{
public:
@@ -47,6 +97,9 @@ class LuaScriptEngine : public osg::ScriptEngine
osgDB::PropertyInterface& getPropertyInterface() const { return _pi; }
int pushDataToStack(SerializerScratchPad* ssp) const;
int popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSerializer::Type type) const;
int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const;
int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const;
@@ -109,6 +162,7 @@ class LuaScriptEngine : public osg::ScriptEngine
bool popParameter(osg::Object* object) const;
osg::Object* popParameterObject() const;
void pushContainer(osg::Object* object, const std::string& propertyName) const;
void createAndPushObject(const std::string& compoundName) const;
void pushObject(osg::Object* object) const;
@@ -133,6 +187,24 @@ class LuaScriptEngine : public osg::ScriptEngine
else return 0;
}
std::string getStringFromTable(int pos, const std::string& field) const
{
std::string result;
if (lua_type(_lua, pos)==LUA_TTABLE)
{
lua_pushstring(_lua, field.c_str());
lua_rawget(_lua, pos);
if (lua_type(_lua, -1)==LUA_TSTRING)
{
result = lua_tostring(_lua, -1);
}
lua_pop(_lua,1);
}
return result;
}
std::string getObjectCompoundClassName(int pos) const
{
if (lua_type(_lua, pos)==LUA_TTABLE)

View File

@@ -6,6 +6,7 @@
namespace ArrayWrappers {
#if 0
struct ResizeArray : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
@@ -28,14 +29,14 @@ struct ResizeArray : public osgDB::MethodObject
return true;
}
};
#endif
REGISTER_OBJECT_WRAPPER( Array,
0,
osg::Array,
"osg::Object osg::Array" )
{
#if 0
BEGIN_ENUM_SERIALIZER_NO_SET( Type, ArrayType );
ADD_ENUM_VALUE( ArrayType );
@@ -94,6 +95,8 @@ REGISTER_OBJECT_WRAPPER( Array,
ADD_UINT_SERIALIZER_NO_SET( TotalDataSize, 0);
ADD_UINT_SERIALIZER_NO_SET( NumElements, 0);
ADD_METHOD_OBJECT( "resizeArray", ResizeArray );
#endif
BEGIN_ENUM_SERIALIZER( Binding, BIND_UNDEFINED );
ADD_ENUM_VALUE( BIND_UNDEFINED );
ADD_ENUM_VALUE( BIND_OFF );
@@ -105,50 +108,54 @@ REGISTER_OBJECT_WRAPPER( Array,
ADD_BOOL_SERIALIZER(Normalize, false);
ADD_BOOL_SERIALIZER(PreserveDataType, false);
ADD_METHOD_OBJECT( "resizeArray", ResizeArray );
}
}
#define ARRAY_WRAPPERS( ARRAY ) \
namespace Wrappers##ARRAY { REGISTER_OBJECT_WRAPPER( ARRAY, new osg::ARRAY, osg::ARRAY, "osg::Object osg::Array osg::"#ARRAY ) {} }
#define ARRAY_WRAPPERS( ARRAY, ELEMENTTYPE, NUMELEMENTSONROW ) \
namespace Wrappers##ARRAY { \
REGISTER_OBJECT_WRAPPER( ARRAY, new osg::ARRAY, osg::ARRAY, "osg::Object osg::Array osg::"#ARRAY) \
{ \
ADD_ISAVECTOR_SERIALIZER( vector, osgDB::BaseSerializer::ELEMENTTYPE, NUMELEMENTSONROW ); \
} \
}
ARRAY_WRAPPERS(FloatArray)
ARRAY_WRAPPERS(Vec2Array)
ARRAY_WRAPPERS(Vec3Array)
ARRAY_WRAPPERS(Vec4Array)
ARRAY_WRAPPERS(FloatArray, RW_FLOAT, 4)
ARRAY_WRAPPERS(Vec2Array, RW_VEC2F, 1)
ARRAY_WRAPPERS(Vec3Array, RW_VEC3F, 1)
ARRAY_WRAPPERS(Vec4Array, RW_VEC4F, 1)
ARRAY_WRAPPERS(DoubleArray)
ARRAY_WRAPPERS(Vec2dArray)
ARRAY_WRAPPERS(Vec3dArray)
ARRAY_WRAPPERS(Vec4dArray)
ARRAY_WRAPPERS(DoubleArray, RW_DOUBLE, 4)
ARRAY_WRAPPERS(Vec2dArray, RW_VEC2D, 1)
ARRAY_WRAPPERS(Vec3dArray, RW_VEC3D, 1)
ARRAY_WRAPPERS(Vec4dArray, RW_VEC4D, 1)
ARRAY_WRAPPERS(ByteArray)
ARRAY_WRAPPERS(Vec2bArray)
ARRAY_WRAPPERS(Vec3bArray)
ARRAY_WRAPPERS(Vec4bArray)
ARRAY_WRAPPERS(ByteArray, RW_CHAR, 4)
ARRAY_WRAPPERS(Vec2bArray, RW_VEC2B, 1)
ARRAY_WRAPPERS(Vec3bArray, RW_VEC3B, 1)
ARRAY_WRAPPERS(Vec4bArray, RW_VEC4B, 1)
ARRAY_WRAPPERS(UByteArray)
ARRAY_WRAPPERS(Vec2ubArray)
ARRAY_WRAPPERS(Vec3ubArray)
ARRAY_WRAPPERS(Vec4ubArray)
ARRAY_WRAPPERS(UByteArray, RW_UCHAR, 4)
ARRAY_WRAPPERS(Vec2ubArray, RW_VEC2UB, 1)
ARRAY_WRAPPERS(Vec3ubArray, RW_VEC3UB, 1)
ARRAY_WRAPPERS(Vec4ubArray, RW_VEC4UB, 1)
ARRAY_WRAPPERS(ShortArray)
ARRAY_WRAPPERS(Vec2sArray)
ARRAY_WRAPPERS(Vec3sArray)
ARRAY_WRAPPERS(Vec4sArray)
ARRAY_WRAPPERS(ShortArray, RW_SHORT, 4)
ARRAY_WRAPPERS(Vec2sArray, RW_VEC2S, 1)
ARRAY_WRAPPERS(Vec3sArray, RW_VEC3S, 1)
ARRAY_WRAPPERS(Vec4sArray, RW_VEC4S, 1)
ARRAY_WRAPPERS(UShortArray)
ARRAY_WRAPPERS(Vec2usArray)
ARRAY_WRAPPERS(Vec3usArray)
ARRAY_WRAPPERS(Vec4usArray)
ARRAY_WRAPPERS(UShortArray, RW_USHORT, 4)
ARRAY_WRAPPERS(Vec2usArray, RW_VEC2US, 1)
ARRAY_WRAPPERS(Vec3usArray, RW_VEC3US, 1)
ARRAY_WRAPPERS(Vec4usArray, RW_VEC4US, 1)
ARRAY_WRAPPERS(IntArray)
ARRAY_WRAPPERS(Vec2iArray)
ARRAY_WRAPPERS(Vec3iArray)
ARRAY_WRAPPERS(Vec4iArray)
ARRAY_WRAPPERS(IntArray, RW_INT, 4)
ARRAY_WRAPPERS(Vec2iArray, RW_VEC2I, 1)
ARRAY_WRAPPERS(Vec3iArray, RW_VEC3I, 1)
ARRAY_WRAPPERS(Vec4iArray, RW_VEC4I, 1)
ARRAY_WRAPPERS(UIntArray)
ARRAY_WRAPPERS(Vec2uiArray)
ARRAY_WRAPPERS(Vec3uiArray)
ARRAY_WRAPPERS(Vec4uiArray)
ARRAY_WRAPPERS(UIntArray, RW_UINT, 4)
ARRAY_WRAPPERS(Vec2uiArray, RW_VEC2UI, 1)
ARRAY_WRAPPERS(Vec3uiArray, RW_VEC3UI, 1)
ARRAY_WRAPPERS(Vec4uiArray, RW_VEC4UI, 1)

View File

@@ -146,7 +146,9 @@ REGISTER_OBJECT_WRAPPER( Geometry,
osg::Geometry,
"osg::Object osg::Drawable osg::Geometry" )
{
ADD_LIST_SERIALIZER( PrimitiveSetList, osg::Geometry::PrimitiveSetList ); // _primitives
//ADD_LIST_SERIALIZER( PrimitiveSetList, osg::Geometry::PrimitiveSetList ); // _primitives
ADD_VECTOR_SERIALIZER( PrimitiveSetList, osg::Geometry::PrimitiveSetList, osgDB::BaseSerializer::RW_OBJECT, 0 );
ADD_USER_SERIALIZER( VertexData ); // _vertexData
ADD_USER_SERIALIZER( NormalData ); // _normalData
ADD_USER_SERIALIZER( ColorData ); // _colorData
@@ -157,5 +159,27 @@ REGISTER_OBJECT_WRAPPER( Geometry,
ADD_USER_SERIALIZER( FastPathHint ); // _fastPathHint
{
UPDATE_TO_VERSION_SCOPED( 112 )
REMOVE_SERIALIZER( VertexData );
REMOVE_SERIALIZER( NormalData );
REMOVE_SERIALIZER( ColorData );
REMOVE_SERIALIZER( SecondaryColorData );
REMOVE_SERIALIZER( FogCoordData );
REMOVE_SERIALIZER( TexCoordData );
REMOVE_SERIALIZER( VertexAttribData );
REMOVE_SERIALIZER( FastPathHint );
ADD_OBJECT_SERIALIZER( VertexArray, osg::Array, NULL );
ADD_OBJECT_SERIALIZER( NormalArray, osg::Array, NULL );
ADD_OBJECT_SERIALIZER( ColorArray, osg::Array, NULL );
ADD_OBJECT_SERIALIZER( SecondaryColorArray, osg::Array, NULL );
ADD_OBJECT_SERIALIZER( FogCoordArray, osg::Array, NULL );
ADD_VECTOR_SERIALIZER( TexCoordArrayList, osg::Geometry::ArrayList, osgDB::BaseSerializer::RW_OBJECT, 0 );
ADD_VECTOR_SERIALIZER( VertexAttribArrayList, osg::Geometry::ArrayList, osgDB::BaseSerializer::RW_OBJECT, 0 );
}
wrapper->addFinishedObjectReadCallback( new GeometryFinishedObjectReadCallback() );
}

View File

@@ -6,12 +6,54 @@
namespace PrimitiveSetWrapper {
#define CUSTOM_BEGIN_ENUM_SERIALIZER(PROP, DEF) \
{ typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, void> MySerializer; \
osg::ref_ptr<MySerializer> serializer = new MySerializer( \
#PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP)
REGISTER_OBJECT_WRAPPER( PrimitiveSet,
0,
osg::PrimitiveSet,
"osg::Object osg::PrimitiveSet" )
{
ADD_INT_SERIALIZER( NumInstances, 0);
#if 1
{
typedef osgDB::EnumSerializer<MyClass, MyClass::Mode, void> MySerializer;
typedef osg::PrimitiveSet::Mode (osg::PrimitiveSet::*Getter)() const;
typedef void (osg::PrimitiveSet::*Setter)( osg::PrimitiveSet::Mode );
osg::ref_ptr<MySerializer> serializer = new MySerializer( "Mode", osg::PrimitiveSet::POINTS,
reinterpret_cast<Getter>(&osg::PrimitiveSet::getMode),
reinterpret_cast<Setter>(&osg::PrimitiveSet::setMode));
ADD_ENUM_VALUE( POINTS );
ADD_ENUM_VALUE( LINES );
ADD_ENUM_VALUE( LINE_STRIP );
ADD_ENUM_VALUE( LINE_LOOP );
ADD_ENUM_VALUE( TRIANGLES );
ADD_ENUM_VALUE( TRIANGLE_STRIP );
ADD_ENUM_VALUE( TRIANGLE_FAN );
ADD_ENUM_VALUE( QUADS );
ADD_ENUM_VALUE( QUAD_STRIP );
ADD_ENUM_VALUE( POLYGON );
ADD_ENUM_VALUE( LINES_ADJACENCY );
ADD_ENUM_VALUE( LINE_STRIP_ADJACENCY );
ADD_ENUM_VALUE( TRIANGLES_ADJACENCY );
ADD_ENUM_VALUE( TRIANGLE_STRIP_ADJACENCY );
ADD_ENUM_VALUE( PATCHES );
wrapper->addSerializer(serializer.get(), osgDB::BaseSerializer::RW_ENUM);
}
#else
ADD_GLENUM_SERIALIZER( Mode, GLenum, GL_NONE );
#endif
#if 0
BEGIN_ENUM_SERIALIZER_NO_SET( Type, PrimitiveType );
ADD_ENUM_VALUE( PrimitiveType );
ADD_ENUM_VALUE( DrawArraysPrimitiveType );
@@ -21,17 +63,14 @@ REGISTER_OBJECT_WRAPPER( PrimitiveSet,
ADD_ENUM_VALUE( DrawElementsUIntPrimitiveType );
END_ENUM_SERIALIZER();
ADD_INT_SERIALIZER( NumInstances, 0);
ADD_GLENUM_SERIALIZER( Mode, GLenum, GL_NONE );
ADD_UINT_SERIALIZER_NO_SET( TotalDataSize, 0);
ADD_UINT_SERIALIZER_NO_SET( NumPrimitives, 0);
ADD_UINT_SERIALIZER_NO_SET( NumIndices, 0);
wrapper->addSerializer(
new osgDB::PropByValSerializer< osg::PrimitiveSet, bool > ("supportsBufferObject", false, &osg::PrimitiveSet::supportsBufferObject, 0, osgDB::BaseSerializer::RW_BOOL )
);
#endif
}
}
@@ -49,6 +88,20 @@ REGISTER_OBJECT_WRAPPER( DrawArrays,
}
namespace DrawArrayLengthsWrapper {
REGISTER_OBJECT_WRAPPER( DrawArrayLengths,
new osg::DrawArrayLengths,
osg::DrawArrayLengths,
"osg::Object osg::PrimitiveSet osg::DrawArrayLengths" )
{
ADD_GLINT_SERIALIZER( First, 0);
ADD_ISAVECTOR_SERIALIZER( vector, osgDB::BaseSerializer::RW_INT, 4 );
}
}
#if 0
namespace DrawElementsWrapper {
struct ResizeDrawElements : public osgDB::MethodObject
@@ -74,7 +127,6 @@ struct ResizeDrawElements : public osgDB::MethodObject
}
};
REGISTER_OBJECT_WRAPPER( DrawElements,
0,
osg::DrawElements,
@@ -84,10 +136,16 @@ REGISTER_OBJECT_WRAPPER( DrawElements,
}
}
#endif
#define DRAW_ELEMENTS_WRAPPER( DRAWELEMENTS ) \
namespace Wrapper##DRAWELEMENTS { REGISTER_OBJECT_WRAPPER( DRAWELEMENTS, new osg::DRAWELEMENTS, osg::DRAWELEMENTS, "osg::Object osg::PrimitiveSet osg::DrawElements "#DRAWELEMENTS) {} }
#define DRAW_ELEMENTS_WRAPPER( DRAWELEMENTS, ELEMENTTYPE ) \
namespace Wrapper##DRAWELEMENTS { \
REGISTER_OBJECT_WRAPPER( DRAWELEMENTS, new osg::DRAWELEMENTS, osg::DRAWELEMENTS, "osg::Object osg::PrimitiveSet osg::"#DRAWELEMENTS) \
{ \
ADD_ISAVECTOR_SERIALIZER( vector, osgDB::BaseSerializer::ELEMENTTYPE, 4 ); \
} \
}
DRAW_ELEMENTS_WRAPPER( DrawElementsUByte )
DRAW_ELEMENTS_WRAPPER( DrawElementsUShort )
DRAW_ELEMENTS_WRAPPER( DrawElementsUInt )
DRAW_ELEMENTS_WRAPPER( DrawElementsUByte, RW_UCHAR )
DRAW_ELEMENTS_WRAPPER( DrawElementsUShort, RW_USHORT )
DRAW_ELEMENTS_WRAPPER( DrawElementsUInt, RW_UINT )