diff --git a/CMakeLists.txt b/CMakeLists.txt index ade08eaac..b7dbc7bea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -539,6 +539,9 @@ ELSE() FIND_PACKAGE(Asio) ENDIF() FIND_PACKAGE(ZeroConf) + FIND_PACKAGE(Lua51) + FIND_PACKAGE(V8) + FIND_PACKAGE(PythonLibs) ENDIF() IF(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 8) diff --git a/examples/osgpresentation/CMakeLists.txt b/examples/osgpresentation/CMakeLists.txt index 471c84150..c3d112c03 100644 --- a/examples/osgpresentation/CMakeLists.txt +++ b/examples/osgpresentation/CMakeLists.txt @@ -1,45 +1,3 @@ -FIND_PACKAGE(Lua51) -FIND_PACKAGE(V8) -FIND_PACKAGE(PythonLibs) - -IF (LUA_LIBRARIES AND LUA_INCLUDE_DIR) - SET(LUA_FOUND True) -ENDIF() - - -IF (V8_FOUND) - ADD_DEFINITIONS(-DUSE_V8) - - SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${V8_LIBRARY}) - INCLUDE_DIRECTORIES(${V8_INCLUDE_DIR} ) - - MESSAGE("We have found V8") -ELSE() - MESSAGE("We have NOT found V8") -ENDIF() - -IF (LUA_FOUND) - ADD_DEFINITIONS(-DUSE_LUA) - - SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${LUA_LIBRARY}) - INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR} ) - - MESSAGE("We have found Lua") -ELSE() - MESSAGE("We have NOT found Lua") -ENDIF() - -IF (PYTHONLIBS_FOUND) - MESSAGE("We have found Python") - - SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${PYTHON_LIBRARY}) - INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIR} ) - - ADD_DEFINITIONS(-DUSE_PYTHON) -ELSE() - MESSAGE("We have NOT found Python") -ENDIF() - SET(TARGET_SRC osgpresentation.cpp ) #### end var setup ### diff --git a/examples/osgpresentation/osgpresentation.cpp b/examples/osgpresentation/osgpresentation.cpp index a3b2c818f..a0531e1cc 100644 --- a/examples/osgpresentation/osgpresentation.cpp +++ b/examples/osgpresentation/osgpresentation.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include #include @@ -13,162 +15,6 @@ #include #include -// hacky experiment with create lua, v8 and python to run scripts. - -#ifdef USE_LUA - -extern "C" { -#include -#include -#include -} - -void runLua(const std::string& str) -{ - OSG_NOTICE< - -void runPython(const std::string& str) -{ - OSG_NOTICE< - -void runV8(const std::string& str) -{ - OSG_NOTICE< global = v8::ObjectTemplate::New(); - v8::Persistent globalContext = v8::Context::New(NULL, global); - v8::Persistent globalTemplate = v8::Persistent::New(global); - - std::string filePath = str; - if(osgDB::fileExists(filePath)) - { - std::string js_source; - std::ifstream fin(filePath.c_str()); - if (fin) - { - // read file in the crude way. - while(fin) - { - int c = fin.get(); - if (c>=0 && c<=255) - { - js_source.push_back(c); - } - } - - // Create a nested handle scope - v8::HandleScope local_handle_scope; - - // Enter the global context - v8::Context::Scope context_scope(globalContext); - - // Create a string containing the JavaScript source code. - v8::Handle source = v8::String::New(js_source.c_str()); - - // Compile the source code. - v8::Handle script = v8::Script::Compile(source); - - // Run the script to get the result. - v8::Handle result = script->Run(); - - // Convert the result to an ASCII string and print it. - v8::String::AsciiValue ascii(result); - printf("%s\n", *ascii); - } - } - - globalTemplate.Dispose(); - globalContext.Dispose(); - } - isolate->Dispose(); - -} -#endif int main(int argc, char** argv) { @@ -176,33 +22,77 @@ int main(int argc, char** argv) osgViewer::Viewer viewer(arguments); - std::string str; + typedef std::list< osg::ref_ptr > Scripts; + Scripts scripts; -#ifdef USE_LUA - while (arguments.read("--lua",str)) + std::string filename; + while(arguments.read("--script",filename)) { - runLua(str); + osg::ref_ptr script = osgDB::readFile(filename); + if (script.valid()) scripts.push_back(script.get()); } -#endif - -#ifdef USE_V8 - while (arguments.read("--js",str)) - { - runV8(str); - } -#endif - -#ifdef USE_PYTHON - while (arguments.read("--python",str)) - { - runPython(str); - } -#endif // create the model osg::ref_ptr model = osgDB::readNodeFiles(arguments); if (!model) return 1; + // assgin script engine to scene graphs + model->getOrCreateUserDataContainer()->addUserObject(osgDB::readFile("ScriptEngine.lua")); + model->getOrCreateUserDataContainer()->addUserObject(osgDB::readFile("ScriptEngine.python")); + model->getOrCreateUserDataContainer()->addUserObject(osgDB::readFile("ScriptEngine.js")); + + // assign scripts to scene graph + for(Scripts::iterator itr = scripts.begin(); + itr != scripts.end(); + ++itr) + { + model->addUpdateCallback(new osg::ScriptCallback(itr->get())); + } + +#if 0 + std::string str; + osg::ref_ptr luaScriptEngine = osgDB::readFile("ScriptEngine.lua"); + if (luaScriptEngine.valid()) + { + while (arguments.read("--lua", str)) + { + osg::ref_ptr script = osgDB::readFile(str); + if (script.valid()) + { + luaScriptEngine->run(script.get()); + } + } + } + + osg::ref_ptr v8ScriptEngine = osgDB::readFile("ScriptEngine.V8"); + if (v8ScriptEngine.valid()) + { + while (arguments.read("--js",str)) + { + osg::ref_ptr script = osgDB::readFile(str); + if (script.valid()) + { + v8ScriptEngine->run(script.get()); + } + } + } + + + osg::ref_ptr pythonScriptEngine = osgDB::readFile("ScriptEngine.python"); + if (pythonScriptEngine.valid()) + { + while (arguments.read("--python",str)) + { + osg::ref_ptr script = osgDB::readFile(str); + if (script.valid()) + { + pythonScriptEngine->run(script.get()); + } + } + } +#endif + + viewer.setSceneData( model.get() ); return viewer.run(); } diff --git a/include/osg/CameraNode b/include/osg/CameraNode deleted file mode 100644 index 7bd132de3..000000000 --- a/include/osg/CameraNode +++ /dev/null @@ -1,27 +0,0 @@ -/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield - * - * This library is open source and may be redistributed and/or modified under - * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or - * (at your option) any later version. The full license is in LICENSE file - * included with this distribution, and on the openscenegraph.org website. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * OpenSceneGraph Public License for more details. -*/ - -#ifndef OSG_CAMERANODE -#define OSG_CAMERANODE 1 - -#include - -namespace osg { - -#ifdef USE_DEPRECATED_API - typedef osg::Camera CameraNode; -#endif - -} - -#endif diff --git a/include/osg/ScriptEngine b/include/osg/ScriptEngine new file mode 100644 index 000000000..f9fb2b8aa --- /dev/null +++ b/include/osg/ScriptEngine @@ -0,0 +1,108 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSG_SCRIPTENGINE +#define OSG_SCRIPTENGINE 1 + +#include +#include +#include + +namespace osg +{ + +// forward declare +class ScriptEngine; + +/* Script class for wrapping a script and the language used in the script.*/ +class Script : public osg::Object +{ + public: + Script():_modifiedCount(0) {} + Script(const std::string& language, const std::string& str): _language(language), _script(str), _modifiedCount(0) {} + Script(const Script& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): _language(rhs._language), _script(rhs._script), _modifiedCount(0) {} + + META_Object(osg, Script) + + void setLanguage(const std::string& language) { _language = language; dirty(); } + const std::string& getLanguage() { return _language; } + + void setScript(const std::string& str) { _script = str; dirty(); } + const std::string& getScript() const { return _script; } + + void dirty() { ++_modifiedCount; } + unsigned int getModifiedCount() const { return _modifiedCount; } + + protected: + + virtual ~Script() {} + + std::string _language; + std::string _script; + unsigned int _modifiedCount; +}; + +/** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/ +class OSG_EXPORT ScriptCallback : public osg::NodeCallback +{ + public: + ScriptCallback(Script* script=0) : _script(script) {} + ScriptCallback(const ScriptCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::NodeCallback(rhs,copyop), _script(rhs._script) {} + + META_Object(osg, ScriptCallback) + + /** Set the script to call.*/ + void setScript(osg::Script* script) { _script = script; } + + /** Get the script to call.*/ + osg::Script* getScript() { return _script.get(); } + + /** Get the script to call.*/ + const osg::Script* getScript() const { return _script.get(); } + + /** find the ScriptEngine from looking at the UserDataContainers of nodes in scene graph above the ScriptCallback.*/ + osg::ScriptEngine* getScriptEngine(osg::NodePath& nodePath); + + /** NodeCallback method, calls the Script.*/ + virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); + + protected: + + virtual ~ScriptCallback() {} + + osg::ref_ptr