Improvements to lua/OSG integration

This commit is contained in:
Robert Osfield
2017-12-01 12:07:28 +00:00
parent 2108141b4e
commit 9944d949fc
3 changed files with 114 additions and 0 deletions

View File

@@ -1798,6 +1798,24 @@ static int readImageFile(lua_State * _lua)
return 0;
}
static int readShaderFile(lua_State * _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
if (n==1 && lua_type(_lua, 1)==LUA_TSTRING)
{
std::string filename = lua_tostring(_lua, 1);
osg::ref_ptr<osg::Shader> shader = osgDB::readRefShaderFile(filename);
if (shader.valid())
{
lse->pushObject(shader.get());
return 1;
}
}
return 0;
}
static int readNodeFile(lua_State * _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
@@ -1916,6 +1934,13 @@ void LuaScriptEngine::initialize()
lua_setglobal(_lua, "readImageFile");
}
// provide global new method for read Images
{
lua_pushlightuserdata(_lua, this);
lua_pushcclosure(_lua, readShaderFile, 1);
lua_setglobal(_lua, "readShaderFile");
}
// provide global new method for read Images
{
lua_pushlightuserdata(_lua, this);

View File

@@ -64,6 +64,17 @@ static bool writeDescriptions( osgDB::OutputStream& os, const osg::Node& node )
return true;
}
struct NodeGetOrCreateStateSet : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
osg::Node* node = reinterpret_cast<osg::Node*>(objectPtr);
outputParameters.push_back(node->getOrCreateStateSet());
return true;
}
};
REGISTER_OBJECT_WRAPPER( Node,
new osg::Node,
osg::Node,
@@ -85,4 +96,6 @@ REGISTER_OBJECT_WRAPPER( Node,
}
ADD_OBJECT_SERIALIZER( StateSet, osg::StateSet, NULL ); // _stateset
ADD_METHOD_OBJECT( "getOrCreateStateSet", NodeGetOrCreateStateSet );
}

View File

@@ -174,6 +174,75 @@ static bool writeBindUniformBlock( osgDB::OutputStream& os, const osg::Program&
return true;
}
struct ProgramGetNumShaders : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
osg::Program* program = reinterpret_cast<osg::Program*>(objectPtr);
outputParameters.push_back(new osg::UIntValueObject("return", program->getNumShaders()));
return true;
}
};
struct ProgramGetShader : 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::Program* program = reinterpret_cast<osg::Program*>(objectPtr);
outputParameters.push_back(program->getShader(index));
return true;
}
};
struct ProgramAddShader : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Shader* shader = dynamic_cast<osg::Shader*>(inputParameters[0].get());
if (!shader) return false;
osg::Program* program = reinterpret_cast<osg::Program*>(objectPtr);
program->addShader(shader);
return true;
}
};
struct ProgramRemoveShader : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Shader* shader = dynamic_cast<osg::Shader*>(inputParameters[0].get());
if (!shader) return false;
osg::Program* program = reinterpret_cast<osg::Program*>(objectPtr);
program->removeShader(shader);
return true;
}
};
REGISTER_OBJECT_WRAPPER( Program,
new osg::Program,
osg::Program,
@@ -205,4 +274,11 @@ REGISTER_OBJECT_WRAPPER( Program,
UPDATE_TO_VERSION_SCOPED( 150 )
ADD_USER_SERIALIZER( BindUniformBlock );
}
ADD_METHOD_OBJECT( "getNumShaders", ProgramGetNumShaders );
ADD_METHOD_OBJECT( "getShader", ProgramGetShader );
ADD_METHOD_OBJECT( "addShader", ProgramAddShader );
ADD_METHOD_OBJECT( "removeShader", ProgramRemoveShader );
}