Added support for getting Vec* and Matrix objects from Lua stack and assigning them as OSG object properties.

This commit is contained in:
Robert Osfield 2013-09-27 13:04:04 +00:00
parent 43cb1b3a41
commit ff925f1d86
3 changed files with 224 additions and 42 deletions

View File

@ -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<osg::ScriptEngine> luaScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.lua");
if (luaScriptEngine.valid())
{

View File

@ -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)));
_pi.setProperty(object, propertyName, value);
return 0;
}
lua_pop(_lua, 4);
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<numElements; ++i)
{
lua_pushinteger(_lua, i);
lua_gettable(_lua, abs_pos);
if (lua_type(_lua, -1)!=type) { lua_pop(_lua, i+1); return false; }
}
return true;
}
bool LuaScriptEngine::isType(int pos, osgDB::BaseSerializer::Type type) const
{
return false;
}
osgDB::BaseSerializer::Type LuaScriptEngine::getType(int pos) const
{
return osgDB::BaseSerializer::RW_UNDEFINED;
}
bool LuaScriptEngine::getValue(osg::Vec2f& value) const
{
if (lua_istable(_lua, -1))
{
if (getfields("x", "y", LUA_TNUMBER) ||
getfields("s", "t", LUA_TNUMBER) ||
getelements(2, LUA_TNUMBER))
{
value.set(lua_tonumber(_lua, -2), lua_tonumber(_lua, -1));
lua_pop(_lua, 2);
return true;
}
}
return false;
}
bool LuaScriptEngine::getValue(osg::Vec3f& value) const
{
if (lua_istable(_lua, -1))
{
if (getfields("x", "y", "z", LUA_TNUMBER) ||
getfields("r", "g", "b", LUA_TNUMBER) ||
getfields("red", "green", "blue", LUA_TNUMBER) ||
getfields("s", "t", "r", LUA_TNUMBER) ||
getelements(3, LUA_TNUMBER))
{
value.set(lua_tonumber(_lua, -3), lua_tonumber(_lua, -2), lua_tonumber(_lua, -1));
lua_pop(_lua, 3);
return true;
}
}
return false;
}
bool LuaScriptEngine::getValue(osg::Vec4f& value) const
{
if (lua_istable(_lua, -1))
{
if (getfields("x", "y", "z", LUA_TNUMBER) ||
getfields("r", "g", "b", "a", LUA_TNUMBER) ||
getfields("red", "green", "blue", "alpha", LUA_TNUMBER) ||
getfields("s", "t", "r", "q", LUA_TNUMBER) ||
getelements(4, LUA_TNUMBER))
{
value.set(lua_tonumber(_lua, -4), lua_tonumber(_lua, -3), lua_tonumber(_lua, -2), lua_tonumber(_lua, -1));
lua_pop(_lua, 4);
return true;
}
}
return false;
}
bool LuaScriptEngine::getValue(osg::Matrixf& value) const
{
if (lua_istable(_lua, -1))
{
if (getelements(16,LUA_TNUMBER))
{
for(int r=0; r<4; ++r)
{
for(int c=0; c<4; ++c)
{
value(r,c) = lua_tonumber(_lua, -16+(r*4+c));
}
}
return true;
}
}
return false;
}
bool LuaScriptEngine::getValue(osg::Matrixd& value) const
{
if (lua_istable(_lua, -1))
{
if (getelements(16,LUA_TNUMBER))
{
for(int r=0; r<4; ++r)
{
for(int c=0; c<4; ++c)
{
value(r,c) = lua_tonumber(_lua, -16+(r*4+c));
}
}
return true;
}
}
return false;
}
void LuaScriptEngine::pushValue(const osg::Vec2f& value) const
{
}
void LuaScriptEngine::pushValue(const osg::Vec3f& value) const
{
}
void LuaScriptEngine::pushValue(const osg::Vec4f& value) const
{
}
void LuaScriptEngine::pushValue(const osg::Matrixf& value) const
{
}
void LuaScriptEngine::pushValue(const osg::Matrixd& value) const
{
}
bool LuaScriptEngine::pushParameter(osg::Object* object)
{
osg::ValueObject* vo = dynamic_cast<osg::ValueObject*>(object);

View File

@ -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);