Skip to content

compiling_Emscripten

Bruno edited this page Apr 16, 2024 · 12 revisions

Compiling Geogram for Emscripten

Emscripten is a transpiler that transforms C++ code into webasm or javascript. It lets you convert C++ programs into webpages !

Some examples here.

How to compile

Prerequisites

  • Linux: apt-get install emscripten
  • Windows: see instructions here

Compiling under Linux

$ git clone --recurse-submodules https://github.com/BrunoLevy/geogram.git
$ cd geogram
$ configure.sh Emscripten-clang
$ cd build/Emscripten-clang-Release
$ make -j 4

For each application, this generates a .js and a .wasm file in the build/Emscripten-clang-Release/bin directory.

How to run

Running a command-line application

To run a command-line application, you can use nodejs with the .js file as an argument (which will load in turn the .wasm file), as follows:

$ nodejs build/Emscripten-clang-Release/bin/vorpalite.js

There is something special to know about files: normally Javascript applications cannot access files in the local file system without doing something special. Emscripten has a filesystem API, that can be used to mount directories and access them from the programs (then one can open, read, write files as usual, using the standard C and C++ APIs). Geogram uses the following mount points:

  • the current working directory is mounted on /working/
  • the root directory is mounted on /root/

Hence, to remesh my_mesh.obj and store the result in my_output.geogram, both in the current working directory, one needs to do:

$ nodejs build/Emscripten-clang-Release/bin/vorpalite.js working/my_mesh.obj working/my_output.geogram

Note: it is between 5x to 10x slower than with a natively compiled Geogram. But it is interesting because it is completely portable: you can ship vorpalite.js and vorpalite.wasm to somebody without knowing what's his OS, he'll be able to run it (as soon as he has nodejs).

It is interesting to compare the file size between vorpalite.js/.wasm and a statically linked vorpaline for Linux:

Version File size
vorpalite.js + .wasm 4 Mb
vorpalite (static Linux64) 9.4 Mb
vorpalite (static Linux64 stripped) 8 Mb

Emscripten folks did an excellent job ! (note: wasm is a 32-bits executable, whereas I'm comparing with a 64-bits Linux executable).

Running a graphic application

Each graphic application needs a webpage that encapsulates it. There is in geogram a script for creating this webpage automatically:

$ cd build/Emscripten-clang-Release/bin
$ ../../../tools/gen_emscripten_html.sh geobox

Testing an application locally

Unfortunately, you cannot just do $firefox geobox.html, because most webbrowser do not support directly loading local files (and geobox.html needs to load geobox.js that loads in turn geobox.wasm). In most case you'll need to launch a webserver for that ! But python has a builtin one that is easy to start:

$ python -m http.server &
$ firefox http://localhost:8000/geobox.html

Examples

Applications

  • GeoBox (geometry processing in your browser)
  • GeoCod (teaching how to code, for kids)
  • GeoShade (teaching GLSL, with examples from ShaderToy)

Note: you can load your own files in the application. For that, use the add file: Browse... button (top right). We cannot use an in-app menu to open a file load dialog box (browser's security rules forbids that). The output can be saved by the File/Save as... menu in the application (and then will be available from the browser as if it was downloaded from the web).

Basic demos

Final note: please remember that transpiled Geogram programs are between 5x and 10x slower than their native versions. If you want the best experience with Geogram, I highly recommend using a version compiled for your processor/OS (but these transpiled versions can be used to occasionaly repair/remesh small meshes).

Clone this wiki locally