Permalink
Browse files

Added initial support for wrapping osg::Array in a way that can be us…

…ed via scripting.
  • Loading branch information...
1 parent 0d6fd5b commit 015ee8d98d9940dba84c09f0476197e51b41d58a @osg-training-guest osg-training-guest committed Feb 12, 2014
Showing with 183 additions and 17 deletions.
  1. +33 −16 include/osgDB/Serializer
  2. +31 −1 src/osgPlugins/lua/LuaScriptEngine.cpp
  3. +119 −0 src/osgWrappers/serializers/osg/Array.cpp
View
@@ -244,14 +244,14 @@ public:
if ( is.isBinary() )
{
is >> value;
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
else if ( is.matchString(ParentType::_name) )
{
if ( _useHex ) is >> std::hex;
is >> value;
if ( _useHex ) is >> std::dec;
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
return true;
}
@@ -302,12 +302,12 @@ public:
if ( is.isBinary() )
{
is >> value;
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
else if ( is.matchString(ParentType::_name) )
{
is >> value;
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
return true;
}
@@ -350,12 +350,12 @@ public:
if ( is.isBinary() )
{
readMatrixImplementation( is, value );
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
else if ( is.matchString(ParentType::_name) )
{
readMatrixImplementation( is, value );
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
return true;
}
@@ -417,12 +417,12 @@ public:
if ( is.isBinary() )
{
GLenum value; is >> value;
- (object.*_setter)( static_cast<P>(value) );
+ if (_setter!=0) (object.*_setter)( static_cast<P>(value) );
}
else if ( is.matchString(ParentType::_name) )
{
DEF_GLENUM(value); is >> value;
- (object.*_setter)( static_cast<P>(value.get()) );
+ if (_setter!=0) (object.*_setter)( static_cast<P>(value.get()) );
}
return true;
}
@@ -465,12 +465,12 @@ public:
if ( is.isBinary() )
{
is >> value;
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
else if ( is.matchString(ParentType::_name) )
{
is.readWrappedString( value );
- if ( !value.empty() )
+ if ( !value.empty() && (_setter!=0) )
(object.*_setter)( value );
}
return true;
@@ -522,7 +522,7 @@ public:
if ( hasObject )
{
P* value = dynamic_cast<P*>( is.readObject() );
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
}
else if ( is.matchString(ParentType::_name) )
@@ -532,7 +532,7 @@ public:
{
is >> is.BEGIN_BRACKET;
P* value = dynamic_cast<P*>( is.readObject() );
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
is >> is.END_BRACKET;
}
}
@@ -592,7 +592,7 @@ public:
if ( hasObject )
{
P* value = dynamic_cast<P*>( is.readImage() );
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
}
}
else if ( is.matchString(ParentType::_name) )
@@ -602,7 +602,7 @@ public:
{
is >> is.BEGIN_BRACKET;
P* value = dynamic_cast<P*>( is.readImage() );
- (object.*_setter)( value );
+ if (_setter!=0) (object.*_setter)( value );
is >> is.END_BRACKET;
}
}
@@ -667,12 +667,12 @@ public:
if ( is.isBinary() )
{
is >> value;
- (object.*_setter)( static_cast<P>(value) );
+ if (_setter!=0) (object.*_setter)( static_cast<P>(value) );
}
else if ( is.matchString(ParentType::_name) )
{
std::string str; is >> str;
- (object.*_setter)( getValue(str.c_str()) );
+ if (_setter!=0) (object.*_setter)( getValue(str.c_str()) );
}
return true;
}
@@ -820,10 +820,18 @@ public:
wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, int >( \
#PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_INT )
+#define ADD_INT_SERIALIZER_NO_SET(PROP, DEF) \
+ wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, int >( \
+ #PROP, DEF, &MyClass::get##PROP, 0), osgDB::BaseSerializer::RW_INT )
+
#define ADD_UINT_SERIALIZER(PROP, DEF) \
wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned int >( \
#PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_UINT )
+#define ADD_UINT_SERIALIZER_NO_SET(PROP, DEF) \
+ wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned int >( \
+ #PROP, DEF, &MyClass::get##PROP, 0), osgDB::BaseSerializer::RW_UINT )
+
#define ADD_GLINT_SERIALIZER(PROP, DEF) \
wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, GLint >( \
#PROP, ((int)(DEF)), &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_INT )
@@ -1034,6 +1042,10 @@ public:
wrapper->addSerializer( new osgDB::GLenumSerializer< MyClass, TYPE >( \
#PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_GLENUM )
+#define ADD_GLENUM_SERIALIZER_NO_SET(PROP, TYPE, DEF) \
+ wrapper->addSerializer( new osgDB::GLenumSerializer< MyClass, TYPE >( \
+ #PROP, DEF, &MyClass::get##PROP, 0), osgDB::BaseSerializer::RW_GLENUM )
+
#define ADD_STRING_SERIALIZER(PROP, DEF) \
wrapper->addSerializer( new osgDB::StringSerializer< MyClass >( \
#PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_STRING )
@@ -1070,6 +1082,11 @@ public:
osg::ref_ptr<MySerializer> serializer = new MySerializer( \
#PROP, PROPERTIES_CLASS::DEF, &MyClass::get##PROP, &MyClass::set##PROP)
+#define BEGIN_ENUM_SERIALIZER_NO_SET(PROP, DEF) \
+ { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, void> MySerializer; \
+ osg::ref_ptr<MySerializer> serializer = new MySerializer( \
+ #PROP, MyClass::DEF, &MyClass::get##PROP, 0)
+
#define ADD_ENUM_VALUE(VALUE) \
serializer->add(#VALUE, MyClass::VALUE)
@@ -537,7 +537,7 @@ class GetStackValueVisitor : public osg::ValueObject::SetValueVisitor
int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& propertyName) const
{
- OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<")"<<std::endl;
+ // OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<")"<<std::endl;
osgDB::BaseSerializer::Type type;
if (!_pi.getPropertyType(object, propertyName, type))
@@ -590,6 +590,18 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string&
}
break;
}
+ case(osgDB::BaseSerializer::RW_GLENUM):
+ {
+ GLenum value;
+ if (_pi.getProperty(object, propertyName, value))
+ {
+ osgDB::ObjectWrapperManager* ow = osgDB::Registry::instance()->getObjectWrapperManager();
+ std::string enumString = ow->getString("GL",value);
+ lua_pushstring(_lua, enumString.c_str());
+ return 1;
+ }
+ break;
+ }
case(osgDB::BaseSerializer::RW_ENUM):
{
int value;
@@ -842,6 +854,23 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string
}
break;
}
+ case(osgDB::BaseSerializer::RW_GLENUM):
+ {
+ if (lua_isnumber(_lua, -1))
+ {
+ _pi.setProperty(object, propertyName, static_cast<int>(lua_tonumber(_lua, -1)));
+ return 0;
+ }
+ else if (lua_isstring(_lua, -1))
+ {
+ const char* enumString = lua_tostring(_lua, -1);
+ osgDB::ObjectWrapperManager* ow = osgDB::Registry::instance()->getObjectWrapperManager();
+
+ int value = ow->getValue("GL",enumString);
+ _pi.setProperty(object, propertyName, value);
+ }
+ break;
+ }
case(osgDB::BaseSerializer::RW_ENUM):
{
if (lua_isnumber(_lua, -1))
@@ -1640,6 +1669,7 @@ osg::Object* LuaScriptEngine::popParameterObject() const
if (lua_isstring(_lua, -1)) object = new osg::StringValueObject("", lua_tostring(_lua, -1));
break;
}
+ case(osgDB::BaseSerializer::RW_GLENUM):
case(osgDB::BaseSerializer::RW_ENUM):
if (lua_isstring(_lua, -1))
{
@@ -0,0 +1,119 @@
+#include <osg/Array>
+#include <osg/ValueObject>
+#include <osgDB/ObjectWrapper>
+#include <osgDB/InputStream>
+#include <osgDB/OutputStream>
+
+namespace ArrayWrappers {
+
+struct ResizeArray : public osgDB::MethodObject
+{
+ virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
+ {
+ if (inputParameters.empty()) return false;
+
+ osg::Object* indexObject = inputParameters[0].get();
+
+ unsigned int index = 0;
+ osg::DoubleValueObject* dvo = dynamic_cast<osg::DoubleValueObject*>(indexObject);
+ if (dvo) index = static_cast<unsigned int>(dvo->getValue());
+ else
+ {
+ osg::UIntValueObject* uivo = dynamic_cast<osg::UIntValueObject*>(indexObject);
+ if (uivo) index = uivo->getValue();
+ }
+ osg::Array* array = reinterpret_cast<osg::Array*>(objectPtr);
+ array->resizeArray(index);
+
+ return true;
+ }
+};
+
+
+REGISTER_OBJECT_WRAPPER( Array,
+ 0,
+ osg::Array,
+ "osg::Object osg::Array" )
+{
+
+ BEGIN_ENUM_SERIALIZER_NO_SET( Type, ArrayType );
+ ADD_ENUM_VALUE( ArrayType );
+
+ ADD_ENUM_VALUE( ByteArrayType );
+ ADD_ENUM_VALUE( ShortArrayType );
+ ADD_ENUM_VALUE( IntArrayType );
+
+ ADD_ENUM_VALUE( UByteArrayType );
+ ADD_ENUM_VALUE( UShortArrayType );
+ ADD_ENUM_VALUE( UIntArrayType );
+
+ ADD_ENUM_VALUE( FloatArrayType );
+ ADD_ENUM_VALUE( DoubleArrayType );
+
+ ADD_ENUM_VALUE( Vec2bArrayType );
+ ADD_ENUM_VALUE( Vec3bArrayType );
+ ADD_ENUM_VALUE( Vec4bArrayType );
+
+ ADD_ENUM_VALUE( Vec2sArrayType );
+ ADD_ENUM_VALUE( Vec3sArrayType );
+ ADD_ENUM_VALUE( Vec4sArrayType );
+
+ ADD_ENUM_VALUE( Vec2iArrayType );
+ ADD_ENUM_VALUE( Vec3iArrayType );
+ ADD_ENUM_VALUE( Vec4iArrayType );
+
+ ADD_ENUM_VALUE( Vec2ubArrayType );
+ ADD_ENUM_VALUE( Vec3ubArrayType );
+ ADD_ENUM_VALUE( Vec4ubArrayType );
+
+ ADD_ENUM_VALUE( Vec2usArrayType );
+ ADD_ENUM_VALUE( Vec3usArrayType );
+ ADD_ENUM_VALUE( Vec4usArrayType );
+
+ ADD_ENUM_VALUE( Vec2uiArrayType );
+ ADD_ENUM_VALUE( Vec3uiArrayType );
+ ADD_ENUM_VALUE( Vec4uiArrayType );
+
+ ADD_ENUM_VALUE( Vec2ArrayType );
+ ADD_ENUM_VALUE( Vec3ArrayType );
+ ADD_ENUM_VALUE( Vec4ArrayType );
+
+ ADD_ENUM_VALUE( Vec2dArrayType );
+ ADD_ENUM_VALUE( Vec3dArrayType );
+ ADD_ENUM_VALUE( Vec4dArrayType );
+
+ ADD_ENUM_VALUE( MatrixArrayType );
+ ADD_ENUM_VALUE( MatrixdArrayType );
+ END_ENUM_SERIALIZER();
+
+ ADD_INT_SERIALIZER_NO_SET( DataSize, 0);
+
+ ADD_GLENUM_SERIALIZER_NO_SET( DataType, GLenum, GL_NONE );
+
+ ADD_UINT_SERIALIZER_NO_SET( ElementSize, 0);
+ ADD_UINT_SERIALIZER_NO_SET( TotalDataSize, 0);
+ ADD_UINT_SERIALIZER_NO_SET( NumElements, 0);
+
+ BEGIN_ENUM_SERIALIZER( Binding, BIND_UNDEFINED );
+ ADD_ENUM_VALUE( BIND_UNDEFINED );
+ ADD_ENUM_VALUE( BIND_OFF );
+ ADD_ENUM_VALUE( BIND_OVERALL );
+ ADD_ENUM_VALUE( BIND_PER_PRIMITIVE_SET );
+ ADD_ENUM_VALUE( BIND_PER_VERTEX );
+ END_ENUM_SERIALIZER();
+
+ 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 "#ARRAY ) {} }
+
+ARRAY_WRAPPERS(Vec2Array)
+ARRAY_WRAPPERS(Vec3Array)
+ARRAY_WRAPPERS(Vec4Array)
+

0 comments on commit 015ee8d

Please sign in to comment.