Interactive 3D slides

Before you start

The tutorial is organized as a series of complete examples, from the simplest to the most advanced. Each example is a folder that can be downloaded, built, and shown in a slide as it is, and each page displays all its files. The examples can be followed in order, and you can stop once you have what you need.

The principle

A slide displays interactive 3D in the same way as a browser does: as a web page, inside a frame (<iframe>).

1. Your program, for the web

Your C++ OpenGL program, compiled to WebAssembly by Emscripten: project.js, project.wasm.

2. The page of the demo

slide-frame.html: it runs the program in the frame, and passes it the settings of its address.

3. The slide

A frame shows that page; buttons in the slide can drive it, and the arrow keys still go to the next slide.

The slides of the tutorial are made with reveal.js (each example comes with a slide, slides.html), and the same demos are in a talk made with lhtml-site. Both are HTML, where the frame is native.

A few words used in the tutorial:

  • The demo: your program, compiled for the web, running in the frame of a slide.
  • The page of the demo: slide-frame.html, copied next to the build of the program as index.html (this is the web/index.html of each example).
  • The bridge: the small amount of code that connects the program and the slide: the page of the demo, and two C++ files (slide_bridge.hpp/.cpp) that read the settings in the program.
  • The panel: the sliders and buttons that the page of the demo draws over it, from the settings that the program declares (example 2).
  • The poster: an image of the demo, used in the PDF of the slides, where a program cannot run.
  • data-src (reveal.js) and demo:: (lhtml-site): how each tool writes the frame of a demo (below).

What you need

To see the demos in their slides, only Python 3 is needed (step Get the code). To recompile the examples 1 and 2, add a C++ compiler, CMake, GLFW and Emscripten; for the examples 3 to 6, add the CGP library.

  • A C++ compiler and CMake, and GLFW for the desktop version of the examples (brew install glfw on macOS, sudo apt install libglfw3-dev on Ubuntu).

  • The Emscripten SDK, for the web version:

    git clone https://github.com/emscripten-core/emsdk.git
    cd emsdk
    ./emsdk install latest
    ./emsdk activate latest
    source ./emsdk_env.sh        # in each new terminal, before building for the web
    

    (see Download and install). Emscripten then provides emcmake, which runs the usual cmake command so that it compiles for the web.

  • Python 3, for a local web server: a browser refuses to load project.wasm from a page opened as a file.

  • lhtml-site, only for the slides made with lhtml-site: pipx install lhtml-site (see lhtml-site).

  • For the examples 3 to 6: the CGP library, a C++ library made for teaching computer graphics, needed only to recompile them (git clone https://github.com/drohmer/cgp-2027.git). The two first examples need no library but GLFW.

On Windows (click to show the commands)

The commands of the tutorial are written for macOS and Linux. In PowerShell on Windows:

  • python (or py) instead of python3: python -m http.server 8080.
  • The Emscripten SDK: .\emsdk install latest and .\emsdk activate latest, then .\emsdk_env.ps1 in each new terminal (emsdk_env.bat in cmd). emcmake also needs Ninja: winget install Ninja-build.Ninja.
  • GLFW, for the desktop version of the examples 1 and 2: vcpkg install glfw3 (see vcpkg), then add -DCMAKE_TOOLCHAIN_FILE=<vcpkg>\scripts\buildsystems\vcpkg.cmake to the first cmake command. After cmake --build build --config Release, the program is build\Release\project.exe.
  • A variable given before a command (CGP_DIR=../cgp-2027 python3 ...) is set on its own line: $env:CGP_DIR = "..\cgp-2027", then python scripts\build_examples.py ....

The README.md at the top of the zip of each example gives these commands for that example. (These commands have not been tested on Windows.)

Get the code

Each example page has a zip of the example and of what it needs. Or take everything at once: interactive-3d-slides.zip (the bridge, the nine examples with their web builds, the tests). Every file can also be opened alone: all the files.

Each zip unpacks into a folder of its name, with a README.md at its top that gives the steps. The web builds are in the zips (examples/<example>/web/): you can show a demo before compiling anything. In the unzipped folder:

python3 -m http.server 8080

then open http://localhost:8080/examples/a1-opengl-cube/slides.html.

In reveal.js or in lhtml-site

reveal.js: a frame in a <section>. data-src loads it when its slide comes near, and unloads it after:

<section>
  <iframe class="demo" data-src="web/index.html?speed=0.2"
          style="width: 100%; height: 540px; border: 0"></iframe>
</section>

lhtml-site: the macro demo:: writes the frame and sizes it in the layout of the slide (the web build of the demo is in the folder src/_demo/ of the presentation):

media::
demo::../_demo/index.html?speed=0.2
::

The settings of the demo go in its address (?speed=0.2): one build serves several slides. The example talk in reveal.js and the one in lhtml-site show the same demos.

The frame takes the keyboard

Once you click in a demo, the keyboard goes to the frame: the arrow keys and the remote control stop changing the slide. This is how browsers work. The page of the demo (slide-frame.html) sends the keys back to the slides, in both tools.

Next: 1. Rotating cube, the smallest example.