diff --git a/include/osg/TransferFunction b/include/osg/TransferFunction index 85cab77de..f5456d11b 100644 --- a/include/osg/TransferFunction +++ b/include/osg/TransferFunction @@ -104,6 +104,9 @@ class OSG_EXPORT TransferFunction1D : public osg::TransferFunction typedef std::map ColorMap; + /** set the color map and automatically update the image to make sure they are in sync.*/ + void setColorMap(const ColorMap& vcm) { assign(vcm); } + /** Get the color map that stores the mapping between the the transfer function value and the colour it maps to.*/ ColorMap& getColorMap() { return _colorMap; } diff --git a/include/osg/UserDataContainer b/include/osg/UserDataContainer index 7f30f75fb..9da6c174e 100644 --- a/include/osg/UserDataContainer +++ b/include/osg/UserDataContainer @@ -195,6 +195,13 @@ inline Object* getUserObject(osg::Object* object, const std::string& name) return udc ? udc->getUserObject(name) : 0; } +/** Convinience function for getting the User Object associated with specificed name from an Object's UserDataContainer.*/ +inline const Object* getUserObject(const osg::Object* object, const std::string& name) +{ + const osg::UserDataContainer* udc = object->getUserDataContainer(); + return udc ? udc->getUserObject(name) : 0; +} + } diff --git a/include/osgDB/PropertyInterface b/include/osgDB/PropertyInterface index e275b6dae..d8d94ea5c 100644 --- a/include/osgDB/PropertyInterface +++ b/include/osgDB/PropertyInterface @@ -249,11 +249,11 @@ inline bool PropertyInterface::getProperty(const osg::Object* object, const std: if (copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum())) return true; else { - OSG_NOTICE<<"PropertyInterface::getProperty("<getUserDataContainer(); if (udc) { - OSG_NOTICE<<" Checking UserDataContainer for object ptr"<getUserObject(propertyName); if (ptr) { @@ -279,13 +279,13 @@ inline bool PropertyInterface::setProperty(osg::Object* object, const std::strin const osg::Object* outgoingObject = udc->getUserObject(objectIndex); if (outgoingObject==value) return true; - OSG_NOTICE<<"PropertyInterface::setProperty("<className()<<") replace object on UserDataContainer"<className()<<") replace object on UserDataContainer"<setName(propertyName); udc->setUserObject(objectIndex, value); } else { - OSG_NOTICE<<"PropertyInterface::setProperty("<className()<<") Adding object to UserDataContainer"<className()<<") Adding object to UserDataContainer"<setName(propertyName); udc->addUserObject(value); } diff --git a/include/osgDB/Serializer b/include/osgDB/Serializer index 73f380ccb..6a5562a37 100644 --- a/include/osgDB/Serializer +++ b/include/osgDB/Serializer @@ -136,7 +136,7 @@ public: RW_VEC4B, RW_VEC4UB, RW_VEC4S, RW_VEC4US, RW_VEC4I, RW_VEC4UI, RW_BOUNDINGBOXF, RW_BOUNDINGBOXD, RW_BOUNDINGSPHEREF, RW_BOUNDINGSPHERED, - RW_VECTOR + RW_VECTOR, RW_MAP }; BaseSerializer() : _firstVersion(0), _lastVersion(INT_MAX) {} @@ -789,9 +789,11 @@ class VectorBaseSerializer : public BaseSerializer { public: - VectorBaseSerializer(BaseSerializer::Type elementType):_elementType(elementType) {} + VectorBaseSerializer(BaseSerializer::Type elementType, unsigned int elementSize): + _elementType(elementType),_elementSize(elementSize) {} Type getElementType() const { return _elementType; } + unsigned int getElementSize() const { return _elementSize; } virtual unsigned int size(const osg::Object& obj) const { return 0; } virtual void resize(osg::Object& obj, unsigned int numElements) const {} @@ -804,7 +806,8 @@ public: virtual const void* getElement(const osg::Object& obj, unsigned int index) const { return 0; } protected: - Type _elementType; + Type _elementType; + unsigned int _elementSize; }; @@ -819,7 +822,7 @@ public: typedef void (C::*Setter)( const P& ); VectorSerializer( const char* name, ConstGetter cgf, Getter gf, Setter sf, BaseSerializer::Type elementType, unsigned int numElementsOnRow): - VectorBaseSerializer(elementType), + VectorBaseSerializer(elementType, sizeof(ValueType)), _name(name), _constgetter(cgf), _getter(gf), _setter(sf), _numElementsOnRow(numElementsOnRow) {} @@ -984,7 +987,7 @@ public: typedef typename C::const_iterator ConstIterator; IsAVectorSerializer( const char* name, BaseSerializer::Type elementType, unsigned int numElementsOnRow) : - VectorBaseSerializer(elementType), + VectorBaseSerializer(elementType, sizeof(ValueType)), _name(name), _numElementsOnRow(numElementsOnRow) {} @@ -1124,6 +1127,153 @@ public: unsigned int _numElementsOnRow; }; +class MapBaseSerializer : public BaseSerializer +{ +public: + + MapBaseSerializer(BaseSerializer::Type keyType, unsigned int keySize, BaseSerializer::Type elementType, unsigned int elementSize): + _keyType(keyType), _keySize(keySize), + _elementType(elementType),_elementSize(elementSize) {} + + Type getKeyType() const { return _keyType; } + unsigned int getKeySize() const { return _keySize; } + + Type getElementType() const { return _elementType; } + unsigned int getElementSize() const { return _elementSize; } + + virtual void clear(osg::Object& obj) const {} + virtual void setElement(osg::Object& obj, void* ptrKey, void* ptrValue) const {} + virtual void* getElement(osg::Object& obj, void* ptrKey) const { return 0; } + virtual const void* getElement(const osg::Object& obj, void* ptrKey) const { return 0; } + +protected: + Type _keyType; + unsigned int _keySize; + + Type _elementType; + unsigned int _elementSize; +}; + + +template +class MapSerializer : public MapBaseSerializer +{ +public: + typedef typename P::value_type ValueType; + typedef typename P::key_type KeyType; + typedef typename P::mapped_type ElementType; + typedef typename P::const_iterator ConstIterator; + typedef P& (C::*Getter)(); + typedef const P& (C::*ConstGetter)() const; + typedef void (C::*Setter)( const P& ); + + MapSerializer( const char* name, ConstGetter cgf, Getter gf, Setter sf, BaseSerializer::Type keyType, BaseSerializer::Type elementType): + MapBaseSerializer(keyType, sizeof(KeyType), elementType, sizeof(ElementType)), + _name(name), + _constgetter(cgf), _getter(gf), _setter(sf) {} + + virtual const std::string& getName() const { return _name; } + + virtual void clear(osg::Object& obj) const + { + C& object = OBJECT_CAST(obj); + P& map = (object.*_getter)(); + map.clear(); + } + + virtual void setElement(osg::Object& obj, void* ptrKey, void* ptrValue) const + { + C& object = OBJECT_CAST(obj); + P& map = (object.*_getter)(); + map[*reinterpret_cast(ptrKey)] = *reinterpret_cast(ptrValue); + } + + virtual void* getElement(osg::Object& obj, void* ptrKey) const + { + C& object = OBJECT_CAST(obj); + P& map = (object.*_getter)(); + return &(map[*reinterpret_cast(ptrKey)]); + } + + virtual const void* getElement(const osg::Object& obj, void* ptrKey) const + { + const C& object = OBJECT_CAST(obj); + const P& map = (object.*_constgetter)(); + ConstIterator itr = map.find(*reinterpret_cast(ptrKey)); + if (itr==map.end()) return 0; + else return &(itr->second); + } + + virtual bool read( InputStream& is, osg::Object& obj ) + { + C& object = OBJECT_CAST(obj); + unsigned int size = 0; + P map; + if ( is.isBinary() ) + { + is >> size; + for ( unsigned int i=0; i> key >> value; + map[key] = value; + } + (object.*_setter)( map ); + } + else if ( is.matchString(_name) ) + { + is >> size; + if ( size>0 ) + { + is >> is.BEGIN_BRACKET; + for ( unsigned int i=0; i> key >> value; + map[key] = value; + } + is >> is.END_BRACKET; + } + (object.*_setter)( map ); + } + return true; + } + + virtual bool write( OutputStream& os, const osg::Object& obj ) + { + const C& object = OBJECT_CAST(obj); + const P& map = (object.*_constgetter)(); + unsigned int size = (unsigned int)map.size(); + if ( os.isBinary() ) + { + os << size; + for ( ConstIterator itr=map.begin(); + itr!=map.end(); ++itr ) + { + os << itr->first << itr->second; + } + } + else if ( size>0 ) + { + os << os.PROPERTY((_name).c_str()) << size << os.BEGIN_BRACKET << std::endl; + for ( ConstIterator itr=map.begin(); itr!=map.end(); ++itr ) + { + os << itr->first << itr->second; os << std::endl; + } + os << os.END_BRACKET << std::endl; + } + return true; + } + +public: + std::string _name; + ConstGetter _constgetter; + Getter _getter; + Setter _setter; +}; + // ADDING MANIPULATORS #define ADD_SERIALIZER(S) \ wrapper->addSerializer( (S) ) diff --git a/src/osgDB/PropertyInterface.cpp b/src/osgDB/PropertyInterface.cpp index a1e63fcee..4d93f096f 100644 --- a/src/osgDB/PropertyInterface.cpp +++ b/src/osgDB/PropertyInterface.cpp @@ -242,6 +242,7 @@ PropertyInterface::PropertyInterface(): TYPENAME(LIST) TYPENAME(VECTOR) + TYPENAME(MAP) } @@ -380,7 +381,7 @@ bool PropertyInterface::copyPropertyDataToObject(osg::Object* object, const std: } else { - OSG_NOTICE<<"PropertyInterface::copyPropertyDataFromObject() no serializer available."<getObjectFromTable(1); - OSG_NOTICE<<"getProperty()"<className()<<", propertyName="<pushPropertyToStack(object, propertyName); - } - else if (lua_type(_lua, 2)==LUA_TNUMBER) - { - double index = lua_tonumber(_lua, 2); - std::string propertyName = lua_tostring(_lua, 2); - osg::Object* object = lse->getObjectFromTable(1); - - OSG_NOTICE<<"getProperty()"<className()<<", propertyName="<className()<<", propertyName="<className()<<", propertyName="<(bs); if (vs) { - OSG_NOTICE<<"Need to implement copying and sizing of the vector type"<getElement(*object, index); + if (dataPtr) + { + SerializerScratchPad valuesp(vs->getElementType(), dataPtr, vs->getElementSize()); + return lse->pushDataToStack(&valuesp); + } + else + { + lua_pushnil(_lua); + return 1; + } } - - OSG_NOTICE<<"getContainerProperty()"<className()<<", index="<setPropertyFromStack(object, propertyName); } @@ -203,7 +188,7 @@ static int setContainerProperty(lua_State* _lua) if (vs) { SerializerScratchPad ssp; - lse->popDataFromStack(&ssp, vs->getElementType()); + lse->getDataFromStack(&ssp, vs->getElementType(), 3); { vs->setElement(*object, index, ssp.data); } @@ -321,7 +306,7 @@ static int getContainerAdd(lua_State* _lua) if (vs) { SerializerScratchPad ssp; - lse->popDataFromStack(&ssp, vs->getElementType()); + lse->getDataFromStack(&ssp, vs->getElementType(), 2); { vs->addElement(*object, ssp.data); } @@ -330,6 +315,144 @@ static int getContainerAdd(lua_State* _lua) return 0; } +////////////////////////////////////////////////////////////////////////////////////// +// +// Map container support +// +static int getMapProperty(lua_State * _lua) +{ + const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); + + int n = lua_gettop(_lua); /* number of arguments */ + if (n==2 && lua_type(_lua, 1)==LUA_TTABLE) + { + if (lua_type(_lua, 2)==LUA_TSTRING) + { + std::string propertyName = lua_tostring(_lua, 2); + osg::Object* object = lse->getObjectFromTable(1); + std::string containerPropertyName = lse->getStringFromTable(1,"containerPropertyName"); + + return lse->pushPropertyToStack(object, propertyName); + } + else + { + const osg::Object* object = lse->getObjectFromTable(1); + std::string containerPropertyName = lse->getStringFromTable(1,"containerPropertyName"); + + // check to see if Object "is a" vector + osgDB::BaseSerializer::Type type; + osgDB::BaseSerializer* bs = lse->getPropertyInterface().getSerializer(object, containerPropertyName, type); + osgDB::MapBaseSerializer* ms = dynamic_cast(bs); + + if (ms) + { + SerializerScratchPad keysp; + lse->getDataFromStack(&keysp, ms->getKeyType(),2); + if (keysp.dataType==ms->getKeyType()) + { + const void* dataPtr = ms->getElement(*object, keysp.data); + if (dataPtr) + { + SerializerScratchPad valuesp(ms->getElementType(), dataPtr, ms->getElementSize()); + return lse->pushDataToStack(&valuesp); + } + else + { + lua_pushnil(_lua); + return 1; + } + + return 0; + } + } + } + } + + OSG_NOTICE<<"Warning: Lua getMapProperty() not matched"<(lua_topointer(_lua, lua_upvalueindex(1))); + + int n = lua_gettop(_lua); /* number of arguments */ + + if (n==3 && lua_type(_lua, 1)==LUA_TTABLE) + { + if (lua_type(_lua, 2)==LUA_TSTRING) + { + std::string propertyName = lua_tostring(_lua, 2); + osg::Object* object = lse->getObjectFromTable(1); + std::string containerPropertyName = lse->getStringFromTable(1,"containerPropertyName"); + + return lse->setPropertyFromStack(object, propertyName); + } + else + { + osg::Object* object = lse->getObjectFromTable(1); + std::string containerPropertyName = lse->getStringFromTable(1,"containerPropertyName"); + + // check to see if Object "is a" vector + osgDB::BaseSerializer::Type type; + osgDB::BaseSerializer* bs = lse->getPropertyInterface().getSerializer(object, containerPropertyName, type); + osgDB::MapBaseSerializer* ms = dynamic_cast(bs); + + if (ms) + { + SerializerScratchPad keysp, valuesp; + lse->getDataFromStack(&keysp, ms->getKeyType(),2); + lse->getDataFromStack(&valuesp, ms->getElementType(),3); + if (keysp.dataType==ms->getKeyType() && ms->getElementType()==valuesp.dataType) + { + ms->setElement(*object, keysp.data, valuesp.data); + return 0; + } + else + { + OSG_NOTICE<<"Warning: Lua setMapProperty() : Failed to matched map element "<(lua_topointer(_lua, lua_upvalueindex(1))); + int n = lua_gettop(_lua); /* number of arguments */ + if (n<1 || lua_type(_lua, 1)!=LUA_TTABLE) return 0; + + osg::Object* object = lse->getObjectFromTable(1); + std::string containerPropertyName = lse->getStringFromTable(1,"containerPropertyName"); + + // check to see if Object "is a" vector + osgDB::BaseSerializer::Type type; + osgDB::BaseSerializer* bs = lse->getPropertyInterface().getSerializer(object, containerPropertyName, type); + osgDB::MapBaseSerializer* ms = dynamic_cast(bs); + if (ms) + { + OSG_NOTICE<<"Doing map clear"<clear(*object); + return 0; + } + + return 0; +} + +////////////////////////////////////////////////////////////////////////////////////// +// +// Method calling support +// static int callClassMethod(lua_State* _lua) { const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); @@ -582,6 +705,23 @@ void LuaScriptEngine::initialize() lua_pop(_lua,1); } + // Set up the __newindex and __index methods for looking up implementations of Object properties + { + luaL_newmetatable(_lua, "LuaScriptEngine.Map"); + + lua_pushstring(_lua, "__index"); + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, getMapProperty, 1); + lua_settable(_lua, -3); + + lua_pushstring(_lua, "__newindex"); + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, setMapProperty, 1); + lua_settable(_lua, -3); + + lua_pop(_lua,1); + } + // Set up the __gc methods for looking up implementations of Object pointer to do the unref when the associated Lua object is destroyed. { luaL_newmetatable(_lua, "LuaScriptEngine.UnrefObject"); @@ -739,26 +879,24 @@ public: virtual void apply(float& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } } virtual void apply(double& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } } virtual void apply(std::string& value) { if (lua_isstring(_lua, _index)) { value = std::string(lua_tostring(_lua, _index), lua_rawlen(_lua, _index)); _numberToPop = 1; } } - virtual void apply(osg::Vec2f& value) { _lsg->getValue(value); _numberToPop = 2;} - virtual void apply(osg::Vec3f& value) { _lsg->getValue(value); _numberToPop = 2; } - virtual void apply(osg::Vec4f& value) { _lsg->getValue(value); _numberToPop = 4; } - virtual void apply(osg::Vec2d& value) { _lsg->getValue(value); _numberToPop = 2; } - virtual void apply(osg::Vec3d& value) { _lsg->getValue(value); _numberToPop = 3; } - virtual void apply(osg::Vec4d& value) { _lsg->getValue(value); _numberToPop = 4; } - virtual void apply(osg::Quat& value) { _lsg->getValue(value); _numberToPop = 4; } - virtual void apply(osg::Plane& value) { _lsg->getValue(value); _numberToPop = 4; } - virtual void apply(osg::Matrixf& value) { _lsg->getValue(value); } - virtual void apply(osg::Matrixd& value) { _lsg->getValue(value); } - virtual void apply(osg::BoundingBoxf& value) { _lsg->getValue(value); } - virtual void apply(osg::BoundingBoxd& value) { _lsg->getValue(value); } - virtual void apply(osg::BoundingSpheref& value) { _lsg->getValue(value); } - virtual void apply(osg::BoundingSphered& value) { _lsg->getValue(value); } + virtual void apply(osg::Vec2f& value) { _lsg->getValue(_index, value); _numberToPop = 2;} + virtual void apply(osg::Vec3f& value) { _lsg->getValue(_index, value); _numberToPop = 2; } + virtual void apply(osg::Vec4f& value) { _lsg->getValue(_index, value); _numberToPop = 4; } + virtual void apply(osg::Vec2d& value) { _lsg->getValue(_index, value); _numberToPop = 2; } + virtual void apply(osg::Vec3d& value) { _lsg->getValue(_index, value); _numberToPop = 3; } + virtual void apply(osg::Vec4d& value) { _lsg->getValue(_index, value); _numberToPop = 4; } + virtual void apply(osg::Quat& value) { _lsg->getValue(_index, value); _numberToPop = 4; } + virtual void apply(osg::Plane& value) { _lsg->getValue(_index, value); _numberToPop = 4; } + virtual void apply(osg::Matrixf& value) { _lsg->getValue(_index, value); } + virtual void apply(osg::Matrixd& value) { _lsg->getValue(_index, value); } + virtual void apply(osg::BoundingBoxf& value) { _lsg->getValue(_index, value); } + virtual void apply(osg::BoundingBoxd& value) { _lsg->getValue(_index, value); } + virtual void apply(osg::BoundingSpheref& value) { _lsg->getValue(_index, value); } + virtual void apply(osg::BoundingSphered& value) { _lsg->getValue(_index, value); } }; int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& propertyName) const { - // OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } - else if (lua_isstring(_lua, -1)) + else if (lua_isstring(_lua, pos)) { - const char* enumString = lua_tostring(_lua, -1); + const char* enumString = lua_tostring(_lua, pos); GLenum value = lookUpGLenumValue(enumString); //getValue("GL",enumString); - OSG_NOTICE<<"Checking enumString="<set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } - else if (lua_isstring(_lua, -1)) + else if (lua_isstring(_lua, pos)) { - OSG_NOTICE<<"LuaScriptEngine::popDataFromStack() osgDB::BaseSerializer::RW_ENUM Failed to convert string"<set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } break; } case(osgDB::BaseSerializer::RW_USHORT): { - if (lua_isnumber(_lua, -1)) + if (lua_isnumber(_lua, pos)) { - ssp->set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } break; } case(osgDB::BaseSerializer::RW_INT): { - if (lua_isnumber(_lua, -1)) + if (lua_isnumber(_lua, pos)) { - ssp->set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } break; } case(osgDB::BaseSerializer::RW_UINT): { - if (lua_isnumber(_lua, -1)) + if (lua_isnumber(_lua, pos)) { - ssp->set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } break; } case(osgDB::BaseSerializer::RW_FLOAT): { - if (lua_isnumber(_lua, -1)) + if (lua_isnumber(_lua, pos)) { - ssp->set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } break; } case(osgDB::BaseSerializer::RW_DOUBLE): { - if (lua_isnumber(_lua, -1)) + if (lua_isnumber(_lua, pos)) { - ssp->set(static_cast(lua_tonumber(_lua, -1))); + ssp->set(static_cast(lua_tonumber(_lua, pos))); return 0; } break; @@ -1485,7 +1620,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_VEC2F): { osg::Vec2f value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1495,7 +1630,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_VEC3F): { osg::Vec3f value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1505,7 +1640,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_VEC4F): { osg::Vec4f value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1515,7 +1650,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_VEC2D): { osg::Vec2d value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1525,7 +1660,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_VEC3D): { osg::Vec3d value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1535,7 +1670,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_VEC4D): { osg::Vec4d value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1545,7 +1680,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_QUAT): { osg::Quat value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1555,7 +1690,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_PLANE): { osg::Plane value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1568,7 +1703,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_MATRIXF): { osg::Matrixd value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1581,7 +1716,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_MATRIXD): { osg::Matrixd value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1591,7 +1726,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_BOUNDINGBOXF): { osg::BoundingBoxf value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1601,7 +1736,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_BOUNDINGBOXD): { osg::BoundingBoxd value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1611,7 +1746,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_BOUNDINGSPHEREF): { osg::BoundingSpheref value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1621,7 +1756,7 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_BOUNDINGSPHERED): { osg::BoundingSphered value; - if (getValue(value)) + if (getValue(pos, value)) { ssp->set(value); return 0; @@ -1636,12 +1771,12 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri case(osgDB::BaseSerializer::RW_IMAGE): case(osgDB::BaseSerializer::RW_OBJECT): { - if (lua_istable(_lua, -1)) + if (lua_istable(_lua, pos)) { osg::Object* value = 0; lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, -2); - if (lua_type(_lua, -1)==LUA_TUSERDATA) value = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); + lua_rawget(_lua, pos); + if (lua_type(_lua, pos)==LUA_TUSERDATA) value = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); lua_pop(_lua, 1); if (value) @@ -1651,10 +1786,10 @@ int LuaScriptEngine::popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSeri } else { - OSG_NOTICE<<"Error: lua type '"<(lua_tonumber(_lua, -1))); @@ -1732,8 +1866,6 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string const char* enumString = lua_tostring(_lua, -1); GLenum value = lookUpGLenumValue(enumString); //getValue("GL",enumString); - OSG_NOTICE<<"Checking enumString="< object = 0; - osgDB::BaseSerializer::Type type = getType(); + osgDB::BaseSerializer::Type type = getType(-1); switch(type) { case(osgDB::BaseSerializer::RW_BOOL): @@ -2590,19 +2715,19 @@ osg::Object* LuaScriptEngine::popParameterObject() const case(osgDB::BaseSerializer::RW_VEC2F): { osg::Vec2f value; - if (getValue(value)) object = new osg::Vec2fValueObject("", value); + if (getValue(-1, value)) object = new osg::Vec2fValueObject("", value); break; } case(osgDB::BaseSerializer::RW_VEC3F): { osg::Vec3f value; - if (getValue(value)) object = new osg::Vec3fValueObject("", value); + if (getValue(-1, value)) object = new osg::Vec3fValueObject("", value); break; } case(osgDB::BaseSerializer::RW_VEC4F): { osg::Vec4f value; - if (getValue(value)) object = new osg::Vec4fValueObject("", value); + if (getValue(-1, value)) object = new osg::Vec4fValueObject("", value); break; } #ifdef OSG_USE_FLOAT_MATRIX @@ -2611,37 +2736,37 @@ osg::Object* LuaScriptEngine::popParameterObject() const case(osgDB::BaseSerializer::RW_MATRIXF): { osg::Matrixf value; - if (getValue(value)) object = new osg::MatrixfValueObject("", value); + if (getValue(-1, value)) object = new osg::MatrixfValueObject("", value); break; } case(osgDB::BaseSerializer::RW_VEC2D): { osg::Vec2d value; - if (getValue(value)) object = new osg::Vec2dValueObject("", value); + if (getValue(-1, value)) object = new osg::Vec2dValueObject("", value); break; } case(osgDB::BaseSerializer::RW_VEC3D): { osg::Vec3d value; - if (getValue(value)) object = new osg::Vec3dValueObject("", value); + if (getValue(-1, value)) object = new osg::Vec3dValueObject("", value); break; } case(osgDB::BaseSerializer::RW_VEC4D): { osg::Vec4d value; - if (getValue(value)) object = new osg::Vec4dValueObject("", value); + if (getValue(-1, value)) object = new osg::Vec4dValueObject("", value); break; } case(osgDB::BaseSerializer::RW_QUAT): { osg::Quat value; - if (getValue(value)) object = new osg::QuatValueObject("", value); + if (getValue(-1, value)) object = new osg::QuatValueObject("", value); break; } case(osgDB::BaseSerializer::RW_PLANE): { osg::Plane value; - if (getValue(value)) object = new osg::PlaneValueObject("", value); + if (getValue(-1, value)) object = new osg::PlaneValueObject("", value); break; } #ifndef OSG_USE_FLOAT_MATRIX @@ -2650,31 +2775,31 @@ osg::Object* LuaScriptEngine::popParameterObject() const case(osgDB::BaseSerializer::RW_MATRIXD): { osg::Matrixd value; - if (getValue(value)) object = new osg::MatrixdValueObject("", value); + if (getValue(-1, value)) object = new osg::MatrixdValueObject("", value); break; } case(osgDB::BaseSerializer::RW_BOUNDINGBOXF): { osg::BoundingBoxf value; - if (getValue(value)) object = new osg::BoundingBoxfValueObject("", value); + if (getValue(-1, value)) object = new osg::BoundingBoxfValueObject("", value); break; } case(osgDB::BaseSerializer::RW_BOUNDINGBOXD): { osg::BoundingBoxd value; - if (getValue(value)) object = new osg::BoundingBoxdValueObject("", value); + if (getValue(-1, value)) object = new osg::BoundingBoxdValueObject("", value); break; } case(osgDB::BaseSerializer::RW_BOUNDINGSPHEREF): { osg::BoundingSpheref value; - if (getValue(value)) object = new osg::BoundingSpherefValueObject("", value); + if (getValue(-1, value)) object = new osg::BoundingSpherefValueObject("", value); break; } case(osgDB::BaseSerializer::RW_BOUNDINGSPHERED): { osg::BoundingSphered value; - if (getValue(value)) object = new osg::BoundingSpheredValueObject("", value); + if (getValue(-1, value)) object = new osg::BoundingSpheredValueObject("", value); break; } case(osgDB::BaseSerializer::RW_LIST): @@ -2743,14 +2868,33 @@ void LuaScriptEngine::pushContainer(osg::Object* object, const std::string& prop lua_pushstring(_lua, "containerPropertyName"); lua_pushstring(_lua, propertyName.c_str()); lua_settable(_lua, -3); - assignClosure("size", getContainerSize); - assignClosure("clear", getContainerClear); - assignClosure("resize", getContainerResize); - assignClosure("reserve", getContainerReserve); - assignClosure("add", getContainerAdd); + osgDB::BaseSerializer::Type type; + osgDB::BaseSerializer* bs = _pi.getSerializer(object, propertyName, type); + osgDB::VectorBaseSerializer* vs = dynamic_cast(bs); + osgDB::MapBaseSerializer* ms = dynamic_cast(bs); + if (vs) + { + assignClosure("size", getContainerSize); + assignClosure("clear", getContainerClear); + assignClosure("resize", getContainerResize); + assignClosure("reserve", getContainerReserve); + assignClosure("add", getContainerAdd); - luaL_getmetatable(_lua, "LuaScriptEngine.Container"); - lua_setmetatable(_lua, -2); + luaL_getmetatable(_lua, "LuaScriptEngine.Container"); + lua_setmetatable(_lua, -2); + } + else if (ms) + { + OSG_NOTICE<<"Need to set up map object"<(reinterpret_cast(ptr))), dataType(type),dataSize(s) {} + virtual ~SerializerScratchPad() { if (deleteData && data) delete [] data; } + bool deleteData; unsigned int maxDataSize; char* data; @@ -98,46 +101,50 @@ 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 getDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSerializer::Type type, int pos) const; int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const; int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const; bool loadScript(osg::Script* script); - osgDB::BaseSerializer::Type getType() const; + int getAbsolutePos(int pos) const { return (pos<0) ? (lua_gettop(_lua)+pos+1) : pos; } - bool getfields(const char* f1, const char* f2, int type) const; - bool getfields(const char* f1, const char* f2, const char* f3, int type) const; - bool getfields(const char* f1, const char* f2, const char* f3, const char* f4, int type) const; - bool getfields(const char* f1, const char* f2, const char* f3, const char* f4, const char* f5, const char* f6, int type) const; - bool getelements(int numElements, int type) const; + osgDB::BaseSerializer::Type getType(int pos) const; - bool getvec2() const; - bool getvec3() const; - bool getvec4() const; - bool getmatrix() const; - bool getboundingbox() const; - bool getboundingsphere() const; + bool getfields(int pos, const char* f1, const char* f2, int type) const; + bool getfields(int pos, const char* f1, const char* f2, const char* f3, int type) const; + bool getfields(int pos, const char* f1, const char* f2, const char* f3, const char* f4, int type) const; + bool getfields(int pos, const char* f1, const char* f2, const char* f3, const char* f4, const char* f5, const char* f6, int type) const; + bool getelements(int pos, int numElements, int type) const; - bool getValue(osg::Vec2f& value) const; - bool getValue(osg::Vec3f& value) const; - bool getValue(osg::Vec4f& value) const; + bool getvec2(int pos) const; + bool getvec3(int pos) const; + bool getvec4(int pos) const; + bool getmatrix(int pos) const; + bool getboundingbox(int pos) const; + bool getboundingsphere(int pos) const; - bool getValue(osg::Vec2d& value) const; - bool getValue(osg::Vec3d& value) const; - bool getValue(osg::Vec4d& value) const; - bool getValue(osg::Quat& value) const; - bool getValue(osg::Plane& value) const; + bool getValue(int pos, osg::Vec2f& value) const; + bool getValue(int pos, osg::Vec3f& value) const; + bool getValue(int pos, osg::Vec4f& value) const; - bool getValue(osg::Matrixf& value) const; - bool getValue(osg::Matrixd& value) const; + bool getValue(int pos, osg::Vec2d& value) const; + bool getValue(int pos, osg::Vec3d& value) const; + bool getValue(int pos, osg::Vec4d& value) const; + bool getValue(int pos, osg::Quat& value) const; + bool getValue(int pos, osg::Plane& value) const; - bool getValue(osg::BoundingBoxf& value) const; - bool getValue(osg::BoundingBoxd& value) const; + bool getValue(int pos, osg::Matrixf& value) const; + bool getValue(int pos, osg::Matrixd& value) const; - bool getValue(osg::BoundingSpheref& value) const; - bool getValue(osg::BoundingSphered& value) const; + bool getValue(int pos, osg::BoundingBoxf& value) const; + bool getValue(int pos, osg::BoundingBoxd& value) const; + + bool getValue(int pos, osg::BoundingSpheref& value) const; + bool getValue(int pos, osg::BoundingSphered& value) const; + + void pushValue(osgDB::BaseSerializer::Type type, const void* ptr) const; void pushValue(const osg::Vec2f& value) const; void pushValue(const osg::Vec3f& value) const; diff --git a/src/osgWrappers/serializers/osg/TransferFunction1D.cpp b/src/osgWrappers/serializers/osg/TransferFunction1D.cpp index 86729390c..75b3c8c23 100644 --- a/src/osgWrappers/serializers/osg/TransferFunction1D.cpp +++ b/src/osgWrappers/serializers/osg/TransferFunction1D.cpp @@ -3,6 +3,8 @@ #include #include +#if 0 + static bool checkColorMap( const osg::TransferFunction1D& func ) { return func.getColorMap().size()>0; @@ -34,11 +36,21 @@ static bool writeColorMap( osgDB::OutputStream& os, const osg::TransferFunction1 os << os.END_BRACKET << std::endl; return true; } +#endif + +#define ADD_MAP_SERIALIZER(PROP, TYPE, KEYTYPE, ELEMENTTYPE) \ + wrapper->addSerializer( new osgDB::MapSerializer< MyClass, TYPE >( \ + #PROP, &MyClass::get##PROP, &MyClass::get##PROP, &MyClass::set##PROP, KEYTYPE, ELEMENTTYPE), osgDB::BaseSerializer::RW_MAP ) + REGISTER_OBJECT_WRAPPER( TransferFunction1D, new osg::TransferFunction1D, osg::TransferFunction1D, "osg::Object osg::TransferFunction osg::TransferFunction1D" ) { +#if 0 ADD_USER_SERIALIZER( ColorMap ); // _colorMap +#else + ADD_MAP_SERIALIZER(ColorMap, osg::TransferFunction1D::ColorMap, osgDB::BaseSerializer::RW_FLOAT, osgDB::BaseSerializer::RW_VEC4F); +#endif }