Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
32 changes: 0 additions & 32 deletions Makefile

This file was deleted.

41 changes: 15 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,45 @@

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

### WebAssembly

* [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:

Expand Down
17 changes: 0 additions & 17 deletions native/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down