diff --git a/examples/osgpresentation/osgpresentation.cpp b/examples/osgpresentation/osgpresentation.cpp index 392db927b..fd3883a68 100644 --- a/examples/osgpresentation/osgpresentation.cpp +++ b/examples/osgpresentation/osgpresentation.cpp @@ -428,6 +428,9 @@ int main(int argc, char** argv) osg::Vec3f pos(1.5,3.0,4.5); presentation->setProperty("position",pos); + osg::Vec2f texcoord(0.5f,0.20f); + presentation->setProperty("texcoord",texcoord); + osg::ref_ptr luaScriptEngine = osgDB::readFile("ScriptEngine.lua"); if (luaScriptEngine.valid()) { diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index 96269a5bc..24a4e9c4b 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -605,63 +605,40 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string } case(osgDB::BaseSerializer::RW_VEC2F): { - if (lua_istable(_lua, -1)) + osg::Vec2f value; + if (getValue(value)) { - lua_getfield(_lua, -1, "x"); - lua_getfield(_lua, -2, "y"); - - if (lua_isnumber(_lua, -2) && - lua_isnumber(_lua, -1)) - { - _pi.setProperty(object, propertyName, osg::Vec2f(lua_tonumber(_lua, -2), lua_tonumber(_lua, -1))); - } - - lua_pop(_lua, 2); - + _pi.setProperty(object, propertyName, value); return 0; } break; } case(osgDB::BaseSerializer::RW_VEC3F): { - if (lua_istable(_lua, -1)) + osg::Vec3f value; + if (getValue(value)) { - lua_getfield(_lua, -1, "x"); - lua_getfield(_lua, -2, "y"); - lua_getfield(_lua, -3, "z"); - - if (lua_isnumber(_lua, -3) && - lua_isnumber(_lua, -2) && - lua_isnumber(_lua, -1)) - { - _pi.setProperty(object, propertyName, osg::Vec3f(lua_tonumber(_lua, -3), lua_tonumber(_lua, -2), lua_tonumber(_lua, -1))); - } - - lua_pop(_lua, 3); - + _pi.setProperty(object, propertyName, value); return 0; } break; } case(osgDB::BaseSerializer::RW_VEC4F): { - if (lua_istable(_lua, -1)) + osg::Vec4f value; + if (getValue(value)) { - lua_getfield(_lua, -1, "x"); - lua_getfield(_lua, -2, "y"); - lua_getfield(_lua, -3, "z"); - lua_getfield(_lua, -4, "w"); - - if (lua_isnumber(_lua, -4) && - lua_isnumber(_lua, -3) && - lua_isnumber(_lua, -2) && - lua_isnumber(_lua, -1)) - { - _pi.setProperty(object, propertyName, osg::Vec4f(lua_tonumber(_lua, -4), lua_tonumber(_lua, -3), lua_tonumber(_lua, -2), lua_tonumber(_lua, -1))); - } - - lua_pop(_lua, 4); - + _pi.setProperty(object, propertyName, value); + return 0; + } + break; + } + case(osgDB::BaseSerializer::RW_MATRIXF): + { + osg::Matrixd value; + if (getValue(value)) + { + _pi.setProperty(object, propertyName, value); return 0; } break; @@ -669,6 +646,12 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string case(osgDB::BaseSerializer::RW_MATRIX): case(osgDB::BaseSerializer::RW_MATRIXD): { + osg::Matrixd value; + if (getValue(value)) + { + _pi.setProperty(object, propertyName, value); + return 0; + } break; } default: @@ -679,6 +662,182 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string } + +bool LuaScriptEngine::getfields(const char* f1, const char* f2, int type) const +{ + lua_getfield(_lua, -1, f1); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 1); return false; } + + lua_getfield(_lua, -2, f2); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 2); return false; } + + return true; +} + +bool LuaScriptEngine::getfields(const char* f1, const char* f2, const char* f3, int type) const +{ + lua_getfield(_lua, -1, f1); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 1); return false; } + + lua_getfield(_lua, -2, f2); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 2); return false; } + + lua_getfield(_lua, -3, f3); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 3); return false; } + + return true; +} + +bool LuaScriptEngine::getfields(const char* f1, const char* f2, const char* f3, const char* f4, int type) const +{ + lua_getfield(_lua, -1, f1); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 1); return false; } + + lua_getfield(_lua, -2, f2); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 2); return false; } + + lua_getfield(_lua, -3, f3); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 3); return false; } + + lua_getfield(_lua, -4, f4); + if (lua_type(_lua, -1)!=type) { lua_pop(_lua, 4); return false; } + + return true; +} + +bool LuaScriptEngine::getelements(int numElements, int type) const +{ + int abs_pos = lua_gettop(_lua); + for(int i=0; i(object); diff --git a/src/osgPlugins/lua/LuaScriptEngine.h b/src/osgPlugins/lua/LuaScriptEngine.h index 610e08610..f1e5326a8 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.h +++ b/src/osgPlugins/lua/LuaScriptEngine.h @@ -53,6 +53,26 @@ class LuaScriptEngine : public osg::ScriptEngine bool loadScript(osg::Script* script); + bool isType(int pos, osgDB::BaseSerializer::Type type) const; + osgDB::BaseSerializer::Type getType(int pos) const; + + 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 getelements(int numElements, int type) const; + + bool getValue(osg::Vec2f& value) const; + bool getValue(osg::Vec3f& value) const; + bool getValue(osg::Vec4f& value) const; + bool getValue(osg::Matrixf& value) const; + bool getValue(osg::Matrixd& value) const; + + void pushValue(const osg::Vec2f& value) const; + void pushValue(const osg::Vec3f& value) const; + void pushValue(const osg::Vec4f& value) const; + void pushValue(const osg::Matrixf& value) const; + void pushValue(const osg::Matrixd& value) const; + bool pushParameter(osg::Object* object); bool popParameter(osg::Object* object);