diff --git a/CMakeLists.txt b/CMakeLists.txt index 6117e04..e69a2e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,36 @@ cmake_minimum_required(VERSION 3.0) + +# If the EMSDK environment variable is set, configure the toolchain file. this has to be done before the project(demo) line below +if(DEFINED ENV{EMSDK}) + set(CMAKE_TOOLCHAIN_FILE "$ENV{EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" CACHE STRING "Emscripten toolchain file") + message(STATUS "WebAssembly build enabled") + message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'") +else() + message(STATUS "WebAssembly build NOT enabled. EMSDK environment variable is not set. Falling back to native build. If you want to compile for WebAssembly, first `source emsdk_env.sh` in the emsdk directory") +endif() + project(demo) add_definitions(-std=c++14) set(CMAKE_CXX_STANDARD 14) + +if(NOT DEFINED ENV{EMSDK}) + set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build GLFW examples" FORCE) + find_package(glfw3 REQUIRED) # Locate the system-installed GLFW3 + find_package(OpenGL REQUIRED) # OpenGL is needed for GLFW +endif() + +# Source files set(DEMO_SOURCE src/demo.cpp) -message(STATUS "WebAssembly build enabled") -message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'") -if(DEFINED ENV{EMSCRIPTEN}) - message(STATUS "emscripten environment is loaded") +# Build target +add_executable(demoapp ${DEMO_SOURCE}) + +if(DEFINED ENV{EMSDK}) + set_target_properties(demoapp + PROPERTIES SUFFIX ".html" + LINK_FLAGS "-Os -s USE_WEBGL2=1 -s FULL_ES3=1 -s USE_GLFW=3 -s WASM=1") else() - message(FATAL_ERROR "emscripten environment is NOT loaded") + # Link against GLFW and OpenGL for native builds + target_link_libraries(demoapp glfw OpenGL::GL) endif() - -add_executable(wasm ${DEMO_SOURCE}) -set_target_properties(wasm - PROPERTIES SUFFIX ".html" - LINK_FLAGS "-Os -s USE_WEBGL2=1 -s FULL_ES3=1 -s USE_GLFW=3 -s WASM=1") -em_link_js_library(wasm ${libraryJsFiles}) diff --git a/Makefile b/Makefile deleted file mode 100644 index f611e04..0000000 --- a/Makefile +++ /dev/null @@ -1,32 +0,0 @@ -# Make as a task manager to run cmake to run make. -SRC=src/demo.cpp -WASM=build/wasm.html -NATIVE=build-native/native - -.PHONY: all -all: $(WASM) $(NATIVE) - -# Build WebAssembly and load in Python Webserver -.PHONY: wasm -wasm: $(WASM) - cd build && python -m SimpleHTTPServer 8080 - -# Build Native and execute -.PHONY: native -native: $(NATIVE) - $(NATIVE) - -$(WASM): $(SRC) - mkdir -p build - cd build && cmake .. -DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN}/cmake/Modules/Platform/Emscripten.cmake - cd build && make - -$(NATIVE): $(SRC) - mkdir -p build-native - cd build-native && cmake ../native - cd build-native && make - -.PHONY: clean -clean: - cd build && make clean - cd build-native && make clean diff --git a/README.md b/README.md index 8cc068b..b56b67a 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,12 @@ A starting point for a new web and native cross-platform 3D project. -Builds with CMake. Supports [GLFW](http://www.glfw.org/). +Builds with CMake. Uses [GLFW](http://www.glfw.org/). ## Details -* Requires `Make` and `CMake` installed. -* `CMake` configures `Make` to build for both environments. -* The `Makefile` in the root is only a task runner. -* [Libraries available in emscripten][emsdklib]. -* Displays an empty off-white window. Nothing else. - -[emsdklib]:https://github.com/kripken/emscripten/tree/incoming/system/include +* Requires `CMake` and `GLFW` installed. +* Displays an empty green window. Nothing else. ## Building @@ -20,38 +15,32 @@ Builds with CMake. Supports [GLFW](http://www.glfw.org/). * [Download or Compile the WASM Toolchain][wasm-toolchain]. * Setup the WASM toolchain + * `git clone https://github.com/emscripten-core/emsdk.git` * `cd emsdk` * `./emsdk install latest` * `./emsdk activate latest` * `source ./emsdk_env.sh` * Build/Run the example * `cd starter-wasm-webgl-opengl` - * `make wasm` - * Available at: http://localhost:8080/wasm.html + * `mkdir buildwasm && cd buildwasm` + * `cmake ..` + * `cmake --build .` + * `python -m SimpleHTTPServer 8080 #or other webserver` + * Available at: http://localhost:8080/demoapp.html [wasm-toolchain]:http://webassembly.org/getting-started/developers-guide/ ### Native Linux -Tested on Ubuntu 16.04. +Tested on Debian 11. -* Install development tools X11 and OpenGL: `sudo apt install xorg-dev libgl1-mesa-dev` -* Install GLFW: http://www.glfw.org/download.html - * `wget https://github.com/glfw/glfw/releases/download/3.2.1/glfw-3.2.1.zip` - * `unzip glfw-3.2.1.zip` - * `cd glfw-3.2.1` - * `cmake .` - * `sudo make install` +* Install development tools X11 and OpenGL: `sudo apt install xorg-dev libgl1-mesa-dev libglfw3-dev` * Build/Run the example * `cd starter-wasm-webgl-opengl` - * `make native` - -[glfw-dl]:http://www.glfw.org/download.html - -## Notes - -I was hoping to build both in the same CMake tree, but the `CMAKE_TOOLCHAIN_FILE` change -for emscripten breaks the native build. + * `mkdir buildnative && cd buildnative` + * `cmake ..` + * `cmake --build .` + * `./demoapp` Thanks to: diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt deleted file mode 100644 index 0d683ce..0000000 --- a/native/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -cmake_minimum_required(VERSION 3.0) -project(demo) - -set(CMAKE_CXX_STANDARD 11) -set(DEMO_SOURCE ../src/demo.cpp) - -message(STATUS ${CMAKE_MODULE_PATH}) - -message(STATUS "Native build enabled") - -find_package(OpenGL REQUIRED) -find_package(glfw3 REQUIRED) - -include_directories(${OPENGL_INCLUDE_DIR}) - -add_executable(native ${DEMO_SOURCE}) -target_link_libraries(native ${OPENGL_gl_LIBRARY} glfw) diff --git a/src/demo.cpp b/src/demo.cpp index e0287c2..66e843c 100644 --- a/src/demo.cpp +++ b/src/demo.cpp @@ -66,7 +66,7 @@ int main() { glfwMakeContextCurrent(window); // Near white background - glClearColor(0.9f, 0.9f, 0.9f, 0.0f); + glClearColor(0.0f, 1.0f, 0.0f, 0.0f); // Run the loop correctly for the target environment #ifdef __EMSCRIPTEN__