From f992ec1440bc4914b1ace88a032f207f52492b1e Mon Sep 17 00:00:00 2001 From: Michael Kapelko Date: Thu, 22 Jun 2017 09:44:06 +0700 Subject: [PATCH] Add osgemscripten example --- examples/osgemscripten/CMakeLists.txt | 60 ++ examples/osgemscripten/README.md | 43 ++ examples/osgemscripten/box.osgt | 857 +++++++++++++++++++++++ examples/osgemscripten/functions.h | 76 ++ examples/osgemscripten/osgemscripten.cpp | 84 +++ examples/osgemscripten/osgemscripten.h | 180 +++++ src/osg/GLExtensions.cpp | 7 + 7 files changed, 1307 insertions(+) create mode 100644 examples/osgemscripten/CMakeLists.txt create mode 100644 examples/osgemscripten/README.md create mode 100644 examples/osgemscripten/box.osgt create mode 100644 examples/osgemscripten/functions.h create mode 100644 examples/osgemscripten/osgemscripten.cpp create mode 100644 examples/osgemscripten/osgemscripten.h diff --git a/examples/osgemscripten/CMakeLists.txt b/examples/osgemscripten/CMakeLists.txt new file mode 100644 index 000000000..6ba3e4449 --- /dev/null +++ b/examples/osgemscripten/CMakeLists.txt @@ -0,0 +1,60 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR) + +# Path to OpenSceneGraph +SET(OSG_DIR "${CMAKE_SOURCE_DIR}/../..") + +# Specify critical OpenSceneGraph build variables. +SET(BUILD_OSG_APPLICATIONS NO CACHE BOOL "Do not build applications") +SET(EGL_LIBRARY "GL" CACHE STRING "Suppress linkage error") +SET(OSG_GL1_AVAILABLE OFF CACHE BOOL "Unavailable under Emscripten") +SET(OSG_GL2_AVAILABLE OFF CACHE BOOL "Unavailable under Emscripten") +SET(OSG_GLES2_AVAILABLE ON CACHE BOOL "GLES2 is what Emscripten uses") +SET(DYNAMIC_OPENTHREADS OFF CACHE BOOL "Link OpenThreads statically") +SET(DYNAMIC_OPENSCENEGRAPH OFF CACHE BOOL "Link OpenSceneGraph statically") +# Reference SDL2 during build process. +# We use SDL2 to do the following: +# * OpenGL functions' address retrieval +# * OpenGL graphics context creation +SET(USE_SDL2 "-s USE_SDL=2") +SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${USE_SDL2}" CACHE STRING "Use SDL2" FORCE) + +# Reference OpenSceneGraph includes and libraries. +SET(OSG_SOURCE_DIR "${OSG_DIR}") +SET(OSG_BUILD_DIR "${OSG_DIR}/build/Emscripten") +INCLUDE_DIRECTORIES(${OSG_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${OSG_BUILD_DIR}/include) +LINK_DIRECTORIES(${OSG_BUILD_DIR}/lib) + +# Build OpenSceneGraph if osgViewer library is missing. +IF(EXISTS "${OSG_BUILD_DIR}/lib/libosgViewer.a") + MESSAGE("Skip building OpenSceneGraph") +ELSE() + MESSAGE("Build OpenSceneGraph") + FILE(MAKE_DIRECTORY ${OSG_BUILD_DIR}) + ADD_SUBDIRECTORY(${OSG_SOURCE_DIR} ${OSG_BUILD_DIR}) +ENDIF() + +# Build example. +ADD_EXECUTABLE(osgemscripten osgemscripten.cpp) +# Make Emscripten generate ready-to-open HTML page. +SET(CMAKE_EXECUTABLE_SUFFIX ".html") +# Make Emscripten preload the resource. +SET_TARGET_PROPERTIES(osgemscripten PROPERTIES LINK_FLAGS "--preload-file box.osgt") +# Copy the resource to the build directory. +CONFIGURE_FILE(box.osgt box.osgt COPYONLY) +# Libraries must be linked in the specified order. +# Otherwise you may get unsatisified linker errors. +TARGET_LINK_LIBRARIES( + osgemscripten + osgViewer + osgDB + # osgDB plugins start. + osgdb_osg + osgdb_serializers_osg + # osgDB plugins end. + osgGA + osgText + osgUtil + osg + OpenThreads +) diff --git a/examples/osgemscripten/README.md b/examples/osgemscripten/README.md new file mode 100644 index 000000000..25c0997c6 --- /dev/null +++ b/examples/osgemscripten/README.md @@ -0,0 +1,43 @@ +How to build osgemscripten example +================================== + +### 1. Install Emscripten + + Download and install [Emscripten portable SDK](http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html) + +### 2. Create build directory + + Run the following commands: + + `mkdir /path/to/build/dir` + + `cd /path/to/build/dir` + +### 3. Configure example + + Run the following command: + + `cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/emsdk-portable/emscripten//cmake/Modules/Platform/Emscripten.cmake /path/to/OpenSceneGraph/examples/osgemscripten` + + This also configures OpenSceneGraph as a dependency + +### 3. Build example + + Run the following command: + + `make` + + This also builds OpenSceneGraph under `/path/to/OpenSceneGraph/build/Emscripten` + if it has not yet been built. + +### 4. Launch example + + Firefox only: locate and open `osgemscripten.html` file. + + All browsers: + * go to the directory with `osgemscripten.html` + * run `python -m SimpleHTTPServer 8080` from there + * open `http://localhost:8080/osgemscripten.html` in any browser + + You should now see red cube in your web browser. + diff --git a/examples/osgemscripten/box.osgt b/examples/osgemscripten/box.osgt new file mode 100644 index 000000000..4d58efd35 --- /dev/null +++ b/examples/osgemscripten/box.osgt @@ -0,0 +1,857 @@ +#Ascii Scene +#Version 92 +#Generator osgexport 0.13.0 + +osg::Group { + UniqueID 4 + Name "Root" + Children 1 { + osg::MatrixTransform { + UniqueID 0 + Name "Cube" + Children 1 { + osg::Geode { + UniqueID 3 + Name "GeodeCube.001" + Drawables 1 { + osg::Geometry { + UniqueID 2 + Name "Cube" + PrimitiveSetList 1 { + DrawElementsUInt GL_TRIANGLES 576 { + 0 1 2 + 3 4 5 + 6 7 8 + 9 10 11 + 12 13 14 + 15 16 17 + 18 19 20 + 21 22 23 + 24 25 26 + 27 28 29 + 30 31 32 + 33 34 35 + 36 37 38 + 39 40 41 + 42 43 44 + 45 46 47 + 48 49 50 + 51 52 53 + 54 55 56 + 57 58 59 + 60 61 62 + 63 64 65 + 66 67 68 + 69 70 71 + 72 71 67 + 73 72 66 + 74 69 72 + 75 76 68 + 77 76 75 + 78 66 76 + 79 65 80 + 68 79 81 + 67 63 79 + 82 83 58 + 84 82 57 + 85 86 87 + 88 59 89 + 90 88 91 + 92 57 88 + 93 56 94 + 95 96 97 + 98 99 96 + 100 53 49 + 101 102 103 + 101 104 105 + 106 107 108 + 109 110 111 + 112 103 113 + 114 115 47 + 116 117 118 + 119 120 117 + 121 44 40 + 122 123 124 + 125 126 123 + 127 41 128 + 129 130 131 + 132 124 130 + 133 38 134 + 135 136 133 + 137 36 133 + 138 35 31 + 139 138 30 + 140 33 138 + 141 32 142 + 143 141 144 + 145 30 141 + 146 29 147 + 32 146 148 + 31 27 146 + 149 26 22 + 150 149 21 + 151 24 149 + 152 23 153 + 154 152 155 + 156 21 152 + 157 20 158 + 23 157 159 + 22 18 157 + 160 17 19 + 26 160 18 + 25 15 160 + 12 161 28 + 35 12 27 + 34 13 12 + 162 11 37 + 163 162 36 + 164 9 162 + 165 8 46 + 166 167 120 + 168 169 167 + 170 171 55 + 172 173 99 + 174 175 173 + 176 2 64 + 71 176 63 + 71 70 0 + 176 0 2 + 177 178 179 + 165 6 8 + 162 9 11 + 161 12 14 + 160 15 17 + 157 18 20 + 152 21 23 + 149 24 26 + 146 27 29 + 141 30 32 + 138 33 35 + 133 36 38 + 127 39 41 + 121 42 44 + 115 45 47 + 180 48 50 + 100 51 53 + 93 54 56 + 88 57 59 + 83 82 181 + 79 63 65 + 76 66 68 + 72 69 71 + 66 72 67 + 78 73 66 + 73 74 72 + 182 75 68 + 183 77 75 + 77 78 76 + 81 79 80 + 182 68 81 + 68 67 79 + 57 82 58 + 92 84 57 + 184 85 87 + 91 88 89 + 185 90 91 + 90 92 88 + 186 93 94 + 187 95 97 + 95 98 96 + 48 100 49 + 112 101 103 + 102 101 105 + 188 189 190 + 191 109 111 + 192 112 113 + 193 194 195 + 196 116 118 + 116 119 117 + 39 121 40 + 132 122 124 + 122 125 123 + 197 127 128 + 198 129 131 + 129 132 130 + 199 133 134 + 199 135 133 + 136 137 133 + 30 138 31 + 145 139 30 + 139 140 138 + 144 141 142 + 200 143 144 + 143 145 141 + 148 146 147 + 142 32 148 + 32 31 146 + 21 149 22 + 156 150 21 + 150 151 149 + 155 152 153 + 201 154 155 + 154 156 152 + 159 157 158 + 153 23 159 + 23 22 157 + 18 160 19 + 22 26 18 + 26 25 160 + 27 12 28 + 31 35 27 + 35 34 12 + 36 162 37 + 137 163 36 + 163 164 162 + 45 165 46 + 119 166 120 + 166 168 167 + 54 170 55 + 98 172 99 + 172 174 173 + 63 176 64 + 67 71 63 + 176 71 0 + } + } + VertexData { + Array TRUE ArrayID 0 Vec3fArray 202 { + -1.00000 0.71786 -0.50000 + -1.00000 1.00000 -1.00000 + -1.00000 0.50000 -1.00000 + 0.50000 0.71786 -0.50000 + 1.00000 0.71786 -0.50000 + 0.80513 0.86344 -1.00000 + 1.00000 -1.00000 -0.50000 + 1.00000 -1.00000 -1.00000 + 1.00000 -0.50000 -1.00000 + -1.00000 -1.00000 -0.50000 + -1.00000 -1.00000 -1.00000 + -0.50000 -1.00000 -1.00000 + 0.50000 0.50000 -1.00000 + 0.50000 1.00000 -1.00000 + 0.80513 0.86344 -1.00000 + -0.50000 1.00000 1.00000 + -1.00000 1.00000 1.00000 + -1.00000 0.50000 1.00000 + -0.50000 0.00000 1.00000 + -1.00000 0.00000 1.00000 + -1.00000 -0.50000 1.00000 + 0.50000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 -0.50000 1.00000 + 0.50000 1.00000 1.00000 + 0.00000 1.00000 1.00000 + 0.00000 0.50000 1.00000 + 0.50000 0.00000 -1.00000 + 1.00000 0.00000 -1.00000 + 1.00000 -0.50000 -1.00000 + -0.50000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 -0.50000 -1.00000 + -0.50000 1.00000 -1.00000 + 0.00000 1.00000 -1.00000 + 0.00000 0.50000 -1.00000 + 0.00000 -1.00000 -0.50000 + 0.00000 -1.00000 -1.00000 + 0.50000 -1.00000 -1.00000 + 0.00000 -0.69802 0.50000 + 0.00000 -1.00000 0.00000 + 0.50000 -1.00000 0.00000 + -1.00000 -0.69802 0.50000 + -1.00000 -1.00000 0.00000 + -0.50000 -1.00000 0.00000 + 1.00000 0.00000 -0.50000 + 1.00000 0.00000 -1.00000 + 1.00000 0.50000 -1.00000 + 1.00000 0.00000 0.50000 + 0.74690 0.00000 0.00000 + 0.74690 0.50000 0.00000 + 1.00000 -0.69802 0.50000 + 0.74690 -1.00000 0.00000 + 0.74690 -0.50000 0.00000 + 0.00000 0.71786 -0.50000 + 0.00000 1.00000 -1.00000 + -0.50000 1.00000 -1.00000 + 0.00000 1.00000 0.50000 + 0.00000 1.00000 0.00000 + -0.50000 1.00000 0.00000 + 0.50000 1.00000 0.50000 + 0.80513 0.86344 0.50000 + 0.74690 1.00000 0.00000 + -1.00000 0.00000 -0.50000 + -1.00000 0.00000 -1.00000 + -1.00000 -0.50000 -1.00000 + -1.00000 0.00000 0.50000 + -1.00000 0.00000 0.00000 + -1.00000 -0.50000 0.00000 + -1.00000 1.00000 0.50000 + -1.00000 1.00000 0.00000 + -1.00000 0.50000 0.00000 + -1.00000 0.50000 0.50000 + -1.00000 0.50000 1.00000 + -1.00000 1.00000 1.00000 + -1.00000 -0.69802 0.50000 + -1.00000 -0.50000 0.50000 + -1.00000 -0.50000 1.00000 + -1.00000 0.00000 1.00000 + -1.00000 -0.50000 -0.50000 + -1.00000 -1.00000 -1.00000 + -1.00000 -1.00000 -0.50000 + 0.50000 1.00000 0.50000 + 0.50000 1.00000 0.00000 + 0.50000 1.00000 1.00000 + 0.80513 0.86344 1.00000 + 0.80513 0.86344 0.50000 + 0.50000 1.00000 0.50000 + -0.50000 1.00000 0.50000 + -1.00000 1.00000 0.00000 + -0.50000 1.00000 1.00000 + -1.00000 1.00000 0.50000 + 0.00000 1.00000 1.00000 + -0.50000 0.71786 -0.50000 + -1.00000 1.00000 -1.00000 + -0.50000 1.00000 0.00000 + -0.50000 0.71786 -0.50000 + -1.00000 0.71786 -0.50000 + 0.00000 1.00000 0.00000 + 0.00000 0.71786 -0.50000 + 1.00000 -0.50000 0.50000 + 1.00000 -0.50000 1.00000 + 1.00000 -0.50000 0.50000 + 1.00000 0.00000 0.50000 + 1.00000 -1.00000 1.00000 + 1.00000 -0.69802 0.50000 + 0.80513 0.86344 0.50000 + 1.00000 0.50000 0.50000 + 0.74690 0.50000 0.00000 + 1.00000 0.50000 1.00000 + 1.00000 0.50000 0.50000 + 0.80513 0.86344 0.50000 + 1.00000 0.00000 1.00000 + 1.00000 0.50000 0.50000 + 1.00000 0.71786 -0.50000 + 1.00000 0.50000 -0.50000 + 0.74690 0.50000 0.00000 + 1.00000 0.50000 -0.50000 + 1.00000 0.71786 -0.50000 + 0.74690 0.00000 0.00000 + 1.00000 0.00000 -0.50000 + -0.50000 -0.69802 0.50000 + -0.50000 -1.00000 1.00000 + -0.50000 -0.69802 0.50000 + 0.00000 -0.69802 0.50000 + -1.00000 -1.00000 1.00000 + -1.00000 -0.69802 0.50000 + 0.50000 -0.69802 0.50000 + 0.74690 -1.00000 0.00000 + 0.50000 -1.00000 1.00000 + 0.50000 -0.69802 0.50000 + 1.00000 -0.69802 0.50000 + 0.00000 -1.00000 1.00000 + 0.50000 -1.00000 -0.50000 + 1.00000 -1.00000 -1.00000 + 0.74690 -1.00000 0.00000 + 0.50000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + -0.50000 0.50000 -1.00000 + -1.00000 0.50000 -1.00000 + -1.00000 1.00000 -1.00000 + -0.50000 -0.50000 -1.00000 + 0.00000 -1.00000 -1.00000 + -1.00000 -0.50000 -1.00000 + -0.50000 -1.00000 -1.00000 + -1.00000 0.00000 -1.00000 + 0.50000 -0.50000 -1.00000 + 1.00000 -1.00000 -1.00000 + 0.50000 -1.00000 -1.00000 + 0.50000 0.50000 1.00000 + 1.00000 0.50000 1.00000 + 0.80513 0.86344 1.00000 + 0.50000 -0.50000 1.00000 + 0.00000 -1.00000 1.00000 + 1.00000 -0.50000 1.00000 + 0.50000 -1.00000 1.00000 + 1.00000 0.00000 1.00000 + -0.50000 -0.50000 1.00000 + -1.00000 -1.00000 1.00000 + -0.50000 -1.00000 1.00000 + -0.50000 0.50000 1.00000 + 1.00000 0.50000 -1.00000 + -0.50000 -1.00000 -0.50000 + -0.50000 -1.00000 0.00000 + -1.00000 -1.00000 0.00000 + 1.00000 -0.50000 -0.50000 + 0.74690 -0.50000 0.00000 + 1.00000 -0.50000 -0.50000 + 0.74690 -1.00000 0.00000 + 1.00000 -1.00000 -0.50000 + 0.50000 0.71786 -0.50000 + 0.50000 1.00000 -1.00000 + 0.50000 1.00000 0.00000 + 0.50000 0.71786 -0.50000 + 0.74690 1.00000 0.00000 + 1.00000 0.71786 -0.50000 + -1.00000 0.50000 -0.50000 + 0.50000 1.00000 -1.00000 + 0.50000 0.71786 -0.50000 + 0.80513 0.86344 -1.00000 + 1.00000 0.50000 0.50000 + 0.74690 1.00000 0.00000 + -1.00000 -1.00000 0.00000 + -1.00000 -1.00000 1.00000 + 0.50000 1.00000 1.00000 + -1.00000 1.00000 1.00000 + -1.00000 0.71786 -0.50000 + -1.00000 1.00000 0.00000 + 0.74690 1.00000 0.00000 + 0.80513 0.86344 0.50000 + 0.74690 0.50000 0.00000 + 0.80513 0.86344 1.00000 + 1.00000 0.50000 1.00000 + 0.80513 0.86344 -1.00000 + 1.00000 0.71786 -0.50000 + 1.00000 0.50000 -1.00000 + 0.74690 1.00000 0.00000 + 1.00000 -0.69802 0.50000 + 1.00000 -1.00000 1.00000 + 1.00000 -1.00000 -0.50000 + -1.00000 -1.00000 -1.00000 + 1.00000 -1.00000 1.00000 + } + Indices FALSE + Binding BIND_PER_VERTEX + Normalize 0 + } + NormalData { + Array TRUE ArrayID 1 Vec3fArray 202 { + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + 0.00000 0.96013 0.27955 + 0.00000 0.96013 0.27955 + 0.00000 0.96013 0.27955 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + -0.00000 -0.00000 -1.00000 + -0.00000 -0.00000 -1.00000 + -0.00000 -0.00000 -1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 0.51699 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 0.89220 0.00000 -0.45164 + 0.89220 0.00000 -0.45164 + 0.89220 0.00000 -0.45164 + 0.89220 0.00000 -0.45164 + 0.89220 0.00000 -0.45164 + 0.89220 0.00000 -0.45164 + -0.00000 0.87091 0.49144 + -0.00000 0.87091 0.49144 + -0.00000 0.87091 0.49144 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.40044 0.89473 0.19774 + 0.40044 0.89473 0.19774 + 0.40044 0.89473 0.19774 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + -1.00000 0.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.40851 0.91275 -0.00000 + 0.40851 0.91275 -0.00000 + 0.40851 0.91275 -0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + 0.00000 1.00000 0.00000 + -0.00000 0.87091 0.49144 + -0.00000 0.87091 0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + 0.89220 0.00000 -0.45164 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 0.80484 0.43155 -0.40742 + 0.80484 0.43155 -0.40742 + 0.80484 0.43155 -0.40742 + 0.88130 0.47255 -0.00000 + 0.88130 0.47255 -0.00000 + 0.88130 0.47255 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 -0.00000 + 1.00000 0.00000 0.00000 + 1.00000 0.00000 0.00000 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + 0.00000 -0.85599 0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + -0.00000 -0.85599 -0.51699 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + -0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 1.00000 + 0.00000 0.00000 -1.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 0.00000 -1.00000 0.00000 + 1.00000 0.00000 -0.00000 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + 0.89220 0.00000 0.45164 + -0.00000 0.87091 0.49144 + -0.00000 0.87091 0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + 0.00000 0.87091 -0.49144 + -1.00000 0.00000 0.00000 + 0.36317 0.81145 0.45789 + 0.36317 0.81145 0.45789 + 0.36317 0.81145 0.45789 + 0.89220 0.00000 -0.45164 + 0.00000 1.00000 0.00000 + -1.00000 -0.00000 0.00000 + -1.00000 0.00000 0.00000 + 0.40851 0.91275 0.00000 + 0.00000 1.00000 -0.00000 + 0.00000 0.87091 0.49144 + 0.00000 0.87091 -0.49144 + 0.99329 0.00000 -0.11568 + 0.99329 0.00000 -0.11568 + 0.99329 0.00000 -0.11568 + 0.88130 0.47255 0.00000 + 1.00000 0.00000 -0.00000 + 0.86320 0.46284 -0.20167 + 0.86320 0.46284 -0.20167 + 0.86320 0.46284 -0.20167 + 0.89220 -0.00000 0.45164 + 0.00000 -0.85599 0.51699 + 0.00000 -0.85599 -0.51699 + 0.00000 -1.00000 0.00000 + -0.00000 -0.00000 -1.00000 + -0.00000 0.00000 1.00000 + } + Indices FALSE + Binding BIND_PER_VERTEX + Normalize 0 + } + TexCoordData 1 { + Data { + Array TRUE ArrayID 2 Vec2fArray 202 { + 0.24987 0.57156 + 0.33316 0.50121 + 0.33316 0.62589 + 0.58434 0.12457 + 0.58427 0.00000 + 0.66715 0.05008 + 0.08352 0.50121 + 0.00024 0.50121 + 0.00016 0.37667 + 0.74911 0.49874 + 0.66715 0.49874 + 0.66715 0.37405 + 0.75013 0.62595 + 0.66684 0.62595 + 0.68959 0.54986 + 0.66684 0.87532 + 0.66684 1.00000 + 0.58355 1.00000 + 0.50026 0.87532 + 0.50026 1.00000 + 0.41697 1.00000 + 0.50026 0.62595 + 0.50026 0.75063 + 0.41697 0.75063 + 0.66684 0.62595 + 0.66684 0.75063 + 0.58355 0.75063 + 0.83342 0.62595 + 0.83342 0.50126 + 0.91671 0.50126 + 0.83342 0.87532 + 0.83342 0.75063 + 0.91671 0.75063 + 0.66684 0.87532 + 0.66684 0.75063 + 0.75013 0.75063 + 0.74911 0.24937 + 0.66715 0.24937 + 0.66715 0.12468 + 0.92198 0.24937 + 0.83107 0.24937 + 0.83107 0.12468 + 0.92198 0.49874 + 0.83107 0.49874 + 0.83107 0.37405 + 0.08335 0.25213 + 0.00008 0.25213 + 0.00000 0.12759 + 0.24990 0.25213 + 0.16747 0.24908 + 0.16739 0.12454 + 0.25001 0.42599 + 0.16763 0.49816 + 0.16755 0.37362 + 0.58440 0.24915 + 0.66685 0.25211 + 0.66691 0.37669 + 0.41702 0.25211 + 0.50030 0.25211 + 0.50036 0.37669 + 0.41696 0.12754 + 0.41732 0.05008 + 0.50021 0.06602 + 0.24987 0.75058 + 0.33316 0.75058 + 0.33316 0.87526 + 0.08329 0.75058 + 0.16658 0.75058 + 0.16658 0.87526 + 0.08329 0.50121 + 0.16658 0.50121 + 0.16658 0.62589 + 0.08329 0.62589 + 0.00000 0.62589 + 0.00000 0.50121 + 0.08329 0.92464 + 0.08329 0.87526 + 0.00000 0.87526 + 0.00000 0.75058 + 0.24987 0.87526 + 0.33316 0.99995 + 0.24987 0.99995 + 0.41696 0.12754 + 0.50024 0.12754 + 0.33368 0.12754 + 0.33404 0.05008 + 0.41732 0.05008 + 0.41696 0.12754 + 0.41708 0.37669 + 0.50042 0.50126 + 0.33381 0.37669 + 0.41714 0.50126 + 0.33375 0.25211 + 0.58446 0.37372 + 0.66698 0.50126 + 0.50036 0.37669 + 0.58446 0.37372 + 0.58452 0.49830 + 0.50030 0.25211 + 0.58440 0.24915 + 0.24998 0.37667 + 0.33325 0.37667 + 0.24998 0.37667 + 0.24990 0.25213 + 0.33333 0.50121 + 0.25001 0.42599 + 0.25041 0.03472 + 0.24982 0.12759 + 0.16739 0.12454 + 0.33309 0.12759 + 0.24982 0.12759 + 0.25041 0.03472 + 0.33317 0.25213 + 0.24982 0.12759 + 0.08324 0.07332 + 0.08327 0.12759 + 0.16739 0.12454 + 0.08327 0.12759 + 0.08324 0.07332 + 0.16747 0.24908 + 0.08335 0.25213 + 0.92198 0.37405 + 0.99498 0.37405 + 0.92198 0.37405 + 0.92198 0.24937 + 0.99498 0.49874 + 0.92198 0.49874 + 0.92198 0.12468 + 0.83107 0.06312 + 0.99498 0.12468 + 0.92198 0.12468 + 0.92198 0.00000 + 0.99498 0.24937 + 0.74911 0.12468 + 0.66715 0.00000 + 0.83107 0.06312 + 0.83107 0.12468 + 0.83107 0.24937 + 0.75013 0.87532 + 0.75013 1.00000 + 0.66684 1.00000 + 0.91671 0.87532 + 1.00000 0.75063 + 0.91671 1.00000 + 1.00000 0.87532 + 0.83342 1.00000 + 0.91671 0.62595 + 1.00000 0.50126 + 1.00000 0.62595 + 0.58355 0.62595 + 0.58355 0.50126 + 0.64409 0.54986 + 0.41697 0.62595 + 0.33368 0.75063 + 0.41697 0.50126 + 0.33368 0.62595 + 0.50026 0.50126 + 0.41697 0.87532 + 0.33368 1.00000 + 0.33368 0.87532 + 0.58355 0.87532 + 0.75013 0.50126 + 0.74911 0.37405 + 0.83107 0.37405 + 0.83107 0.49874 + 0.08343 0.37667 + 0.16755 0.37362 + 0.08343 0.37667 + 0.16763 0.49816 + 0.08352 0.50121 + 0.58434 0.12457 + 0.66679 0.12754 + 0.50024 0.12754 + 0.58434 0.12457 + 0.50021 0.06602 + 0.58427 0.00000 + 0.24987 0.62589 + 0.66679 0.12754 + 0.58434 0.12457 + 0.66715 0.05008 + 0.24982 0.12759 + 0.50021 0.06602 + 0.16658 0.99995 + 0.00000 0.99995 + 0.33368 0.12754 + 0.33387 0.50126 + 0.58452 0.49830 + 0.50042 0.50126 + 0.16731 0.00000 + 0.25041 0.03472 + 0.16739 0.12454 + 0.33368 0.03472 + 0.33309 0.12759 + 0.00059 0.03472 + 0.08324 0.07332 + 0.00000 0.12759 + 0.16731 0.00000 + 0.92198 0.00000 + 0.99498 0.00000 + 0.74911 0.00000 + 1.00000 1.00000 + 0.33368 0.50126 + } + Indices FALSE + Binding BIND_PER_VERTEX + Normalize 0 + } + } + } + } + } + } + Matrix { + 1.00000 0.00000 0.00000 0.00000 + 0.00000 1.00000 0.00000 0.00000 + 0.00000 0.00000 1.00000 0.00000 + 0.00000 0.00000 0.00000 1.00000 + } + } + } +} diff --git a/examples/osgemscripten/functions.h b/examples/osgemscripten/functions.h new file mode 100644 index 000000000..5443d056e --- /dev/null +++ b/examples/osgemscripten/functions.h @@ -0,0 +1,76 @@ +/* OpenSceneGraph example, osgemscripten. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#ifndef OSGEMSCRIPTEN_FUNCTIONS_H +#define OSGEMSCRIPTEN_FUNCTIONS_H + +#include + +// Convert NotifySeverity enum value to string. +std::string logLevelToString(osg::NotifySeverity severity) +{ + switch (severity) + { + case osg::DEBUG_FP: + // Verbose. + return "V"; + case osg::DEBUG_INFO: + // Debug. + return "D"; + case osg::NOTICE: + case osg::INFO: + // Info. + return "I"; + case osg::WARN: + // Warning. + return "W"; + case osg::FATAL: + case osg::ALWAYS: + // Error. + return "E"; + } +} + +#include + +// Fragment shader to display everything in red colour. +static const char shaderFragment[] = + "void main() { \n" + " gl_FragColor = vec4(0.5, 0.3, 0.3, 1.0);\n" + "} \n"; +// Geometry shader to pass geometry vertices to fragment shader. +static const char shaderVertex[] = + "void main() { \n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n" + "} \n"; + +osg::Program *createShaderProgram( + const std::string &vertexShader, + const std::string &fragmentShader) +{ + // Load shaders. + osg::Shader *vs = new osg::Shader(osg::Shader::VERTEX, vertexShader); + osg::Shader *fs = new osg::Shader(osg::Shader::FRAGMENT, fragmentShader); + // Compile shaders and compose shader program. + osg::Program *prog = new osg::Program; + prog->addShader(vs); + prog->addShader(fs); + return prog; +} + +#endif // OSGEMSCRIPTEN_FUNCTIONS_H diff --git a/examples/osgemscripten/osgemscripten.cpp b/examples/osgemscripten/osgemscripten.cpp new file mode 100644 index 000000000..b4a281a8a --- /dev/null +++ b/examples/osgemscripten/osgemscripten.cpp @@ -0,0 +1,84 @@ +/* OpenSceneGraph example, emscripten. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include +#include + +#include "osgemscripten.h" + +// We use app global variable in loop() function. +Application *app = 0; + +// Stand alone function that is called by Emscripten to run the app. +void loop() +{ + SDL_Event e; + while (SDL_PollEvent(&e)) + { + if (app) + { + app->handleEvent(e); + } + } + if (app) + { + app->frame(); + } +} + +int main(int argc, char *argv[]) +{ + // Make sure SDL is working. + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("OSGWeb. Could not init SDL: '%s'\n", SDL_GetError()); + return 1; + } + // Clean SDL up at exit. + atexit(SDL_Quit); + // Configure rendering context. + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + // Create rendering window. + int width = 800; + int height = 600; + SDL_Window* window = + SDL_CreateWindow( + "OSGWeb", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + width, + height, + SDL_WINDOW_OPENGL); + if (!window) + { + printf("OSGWeb. Could not create window: '%s'\n", SDL_GetError()); + return 1; + } + SDL_GL_CreateContext(window); + // Create application. + app = new Application; + app->setupWindow(width, height); + app->loadScene("box.osgt"); + // Render asynchronously. + emscripten_set_main_loop(loop, -1, 0); + return 0; +} diff --git a/examples/osgemscripten/osgemscripten.h b/examples/osgemscripten/osgemscripten.h new file mode 100644 index 000000000..cc4b25c5a --- /dev/null +++ b/examples/osgemscripten/osgemscripten.h @@ -0,0 +1,180 @@ +/* OpenSceneGraph example, osgemscripten. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#ifndef OSGEMSCRIPTEN_OSGEMSCRIPTEN_H +#define OSGEMSCRIPTEN_OSGEMSCRIPTEN_H + +#include "functions.h" + +#include +#include + +#include + +// VBO setup visitor. +#include +#include +#include + +// Initialize OSG plugins statically. +USE_OSGPLUGIN(osg2) +USE_SERIALIZER_WRAPPER_LIBRARY(osg) + +// This class prints OpenSceneGraph notifications to console. +class Logger : public osg::NotifyHandler +{ + public: + Logger() { } + virtual ~Logger() { } + + // Override NotifyHandler::notify() to receive OpenSceneGraph notifications. + void notify(osg::NotifySeverity severity, const char *message) + { + printf("OSG/%s %s", logLevelToString(severity).c_str(), message); + } +}; + +// This class forces the use of VBO. +class VBOSetupVisitor : public osg::NodeVisitor +{ + public: + VBOSetupVisitor() : + osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) { } + virtual void apply(osg::Geode &geode) + { + for (unsigned int i = 0; i < geode.getNumDrawables(); ++i) + { + osg::Geometry *geom = + dynamic_cast(geode.getDrawable(i)); + if (geom) + { + geom->setUseVertexBufferObjects(true); + } + } + NodeVisitor::apply(geode); + } +}; + +class Application +{ + public: + Application() + { + setupLogging(); + setupRendering(); + } + ~Application() + { + tearDownLogging(); + tearDownRendering(); + } + + bool handleEvent(SDL_Event &e) + { + osgViewer::GraphicsWindow *gw = + dynamic_cast( + mViewer->getCamera()->getGraphicsContext()); + if (!gw) + { + return false; + } + osgGA::EventQueue &queue = *(gw->getEventQueue()); + switch (e.type) + { + case SDL_MOUSEMOTION: + queue.mouseMotion(e.motion.x, e.motion.y); + return true; + case SDL_MOUSEBUTTONDOWN: + queue.mouseButtonPress(e.button.x, e.button.y, e.button.button); + printf("OSGWeb. Application. Mouse button down\n"); + return true; + case SDL_MOUSEBUTTONUP: + queue.mouseButtonRelease(e.button.x, e.button.y, e.button.button); + printf("OSGWeb. Application. Mouse button up\n"); + return true; + default: + break; + } + return false; + } + void loadScene(const std::string &fileName) + { + // Load scene. + osg::Node *scene = osgDB::readNodeFile(fileName); + if (!scene) + { + printf("Could not load scene\n"); + return; + } + // Use VBO and EBO instead of display lists. CRITICAL for Emscripten + // to skip FULL_ES2 emulation flag. + VBOSetupVisitor vbo; + scene->accept(vbo); + // Load shaders. + osg::Program *prog = createShaderProgram(shaderVertex, shaderFragment); + // Apply shaders. + scene->getOrCreateStateSet()->setAttribute(prog); + // Set scene. + mViewer->setSceneData(scene); + } + void setupWindow(int width, int height) + { + mViewer->setUpViewerAsEmbeddedInWindow(0, 0, width, height); + } + void frame() + { + mViewer->frame(); + } + + private: + void setupLogging() + { + // Create custom logger. + mLogger = new Logger; + // Provide the logger to OpenSceneGraph. + osg::setNotifyHandler(mLogger); + // Only accept notifications of Info level or higher + // like warnings and errors. + osg::setNotifyLevel(osg::INFO); + } + void setupRendering() + { + // Create OpenSceneGraph viewer. + mViewer = new osgViewer::Viewer; + // Use single thread: CRITICAL for Emscripten. + mViewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded); + // Create manipulator: CRITICAL for Emscripten. + mViewer->setCameraManipulator(new osgGA::TrackballManipulator); + } + void tearDownLogging() + { + // Remove the logger from OpenSceneGraph. + // This also destroys the logger: no need to deallocate it manually. + osg::setNotifyHandler(0); + } + void tearDownRendering() + { + delete mViewer; + } + + private: + Logger *mLogger; + osgViewer::Viewer *mViewer; +}; + +#endif // OSGEMSCRIPTEN_OSGEMSCRIPTEN_H diff --git a/src/osg/GLExtensions.cpp b/src/osg/GLExtensions.cpp index a8f44170d..786cc6f8d 100644 --- a/src/osg/GLExtensions.cpp +++ b/src/osg/GLExtensions.cpp @@ -47,6 +47,9 @@ #else #include #endif +#elif defined(__EMSCRIPTEN__) + // Emscripten ships SDL, which we use to get OpenGL function addresses. + #include #else #include #endif @@ -407,6 +410,10 @@ OSG_INIT_SINGLETON_PROXY(GLExtensionDisableStringInitializationProxy, osg::getGL return dlsym(RTLD_DEFAULT, funcName); + #elif defined(__EMSCRIPTEN__) + // Use SDL to get OpenGL function address for Emscripten. + return SDL_GL_GetProcAddress(funcName); + #else // all other unixes return dlsym(0, funcName);