From 835ee7aa8e1537b4a4f862d8db7b4f52fb29b0e6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 7 Aug 2013 17:08:38 +0000 Subject: [PATCH] Added osgpresentation example as a test bed for new osgPresentation object model. First cut of example test bed is to test how easy it is to build against Lua, V8 and Python for purposes of running embedded scripts. --- CMakeModules/FindV8.cmake | 45 ++++ examples/CMakeLists.txt | 1 + examples/osgpresentation/CMakeLists.txt | 46 ++++ examples/osgpresentation/osgpresentation.cpp | 208 +++++++++++++++++++ 4 files changed, 300 insertions(+) create mode 100644 CMakeModules/FindV8.cmake create mode 100644 examples/osgpresentation/CMakeLists.txt create mode 100644 examples/osgpresentation/osgpresentation.cpp diff --git a/CMakeModules/FindV8.cmake b/CMakeModules/FindV8.cmake new file mode 100644 index 000000000..63d2a57a4 --- /dev/null +++ b/CMakeModules/FindV8.cmake @@ -0,0 +1,45 @@ +# Locate V8 +# This module defines +# V8_LIBRARY +# V8_FOUND, if false, do not try to link to gdal +# V8_INCLUDE_DIR, where to find the headers +# +# $V8_DIR is an environment variable that would +# correspond to the ./configure --prefix=$V8_DIR +# +# Created by Robert Osfield (based on FindFLTK.cmake) + +FIND_PATH(V8_INCLUDE_DIR v8.h + $ENV{V8_DIR}/include + $ENV{V8_DIR} + ~/Library/Frameworks + /Library/Frameworks + /usr/local/include + /usr/include + /sw/include # Fink + /opt/local/include # DarwinPorts + /opt/csw/include # Blastwave + /opt/include + /usr/freeware/include +) + +FIND_LIBRARY(V8_LIBRARY + NAMES v8 libv8 + PATHS + $ENV{V8_DIR}/lib + $ENV{V8_DIR} + ~/Library/Frameworks + /Library/Frameworks + /usr/local/lib + /usr/lib + /sw/lib + /opt/local/lib + /opt/csw/lib + /opt/lib + /usr/freeware/lib64 +) + +SET(V8_FOUND "NO") +IF(V8_LIBRARY AND V8_INCLUDE_DIR) + SET(V8_FOUND "YES") +ENDIF() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 22b58b7d4..135478a4c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -93,6 +93,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgprecipitation) ADD_SUBDIRECTORY(osgprerender) ADD_SUBDIRECTORY(osgprerendercubemap) + ADD_SUBDIRECTORY(osgpresentation) ADD_SUBDIRECTORY(osgreflect) ADD_SUBDIRECTORY(osgrobot) ADD_SUBDIRECTORY(osgscalarbar) diff --git a/examples/osgpresentation/CMakeLists.txt b/examples/osgpresentation/CMakeLists.txt new file mode 100644 index 000000000..471c84150 --- /dev/null +++ b/examples/osgpresentation/CMakeLists.txt @@ -0,0 +1,46 @@ +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 ### +SETUP_EXAMPLE(osgpresentation) diff --git a/examples/osgpresentation/osgpresentation.cpp b/examples/osgpresentation/osgpresentation.cpp new file mode 100644 index 000000000..a3b2c818f --- /dev/null +++ b/examples/osgpresentation/osgpresentation.cpp @@ -0,0 +1,208 @@ +/* Copyright Robert Osfield, Licensed under the GPL + * + * Experimental base for refactor of Present3D + * +*/ + +#include +#include + +#include +#include + +#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) +{ + osg::ArgumentParser arguments(&argc, argv); + + osgViewer::Viewer viewer(arguments); + + std::string str; + +#ifdef USE_LUA + while (arguments.read("--lua",str)) + { + runLua(str); + } +#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; + + viewer.setSceneData( model.get() ); + return viewer.run(); +}