Interactive 3D slides

9. Two demos side by side

This example shows the same demo twice in a slide, to compare two methods: a plane deformed in its vertex shader, on the left with the exact normals of the deformed surface, on the right with the normals of the flat plane. The shape is the same, the lighting is not. One slider of the slide sets the amplitude of both.

Get this example

Download: c9-cgp-compare.zip. The zip contains the C++ program, its slide in reveal.js (slides.html) and in lhtml-site (lhtml-site/), the web build of the demo (ready to show), the bridge and the build script. The CGP project is needed only to recompile.

Try it: unzip it, and in the unzipped folder run python3 -m http.server 8080; then open http://localhost:8080/examples/c9-cgp-compare/slides.html, the slide with its demo. To change the program: build and run.

Graphics: a deformation in a vertex shaderInterface: two frames of one slide, driven together
Exact normals
Normals of the plane
Amplitude
Live: move the slider; drag in a frame to turn its camera.

What is new

  • One build, two addresses: both frames load the same demo; ?normals=exact and ?normals=plane make the difference.
  • Controls that name both frames: data-demo="#left, #right" (a CSS selector) on a button or a slider of the slide sets both demos at once. The values shown come from the first one.
  • A slider in the slide: <input type="range" data-demo-input="ampl"> sets the parameter while it moves, and follows its value.
  • Two clocks: each copy runs its own time. "Restart both" sets them to the same time, so that the two ripples stay in phase.

Rem. Two demos are two WebGL contexts: the browser limits their number in a page (see On the day of the talk).

Build and run

As example 3, with c9-cgp-compare.

The files

src/scene.cpp

scene.cpp

Show the full file: scene.cpp (81 lines)
#include "scene.hpp"
#include "slide_bridge.hpp"

using namespace cgp;


void scene_structure::initialize()
{
	camera_control.initialize(inputs, window);
	camera_control.set_rotation_axis_z();
	camera_control.look_at({2.2f, 1.5f, 1.5f}, {0, 0, -0.1f}, {0, 0, 1});

	// A grid of 200 x 200 vertices, deformed in the vertex shader (CGP How-to: deform a mesh in a shader)
	surface.initialize_data_on_gpu(mesh_primitive_grid({-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0}, 200, 200));
	surface.material.color = {0.8f, 0.95f, 1.0f};
	surface.material.texture_settings.two_sided = true;
	surface.shader.load(project::path + "shaders/mesh_deformation/mesh_deformation.vert.glsl",
	                    project::path + "shaders/mesh_deformation/mesh_deformation.frag.glsl");

	// The parameters of the demo. "normals" is the difference between the two copies of the slide
	// (?normals=plane in the address of one frame); the others are set on both at once by the slide.
	slide::param("normals", parameters.normals, {{"exact", "Exact normals"}, {"plane", "Normals of the plane"}});
	slide::param("ampl", parameters.amplitude, 0.0f, 0.15f, 0.005f).with_label("amplitude", "<i>A</i>");
	slide::param("freq", parameters.frequency, 5.0f, 40.0f, 0.5f).with_label("frequency", "<i>k</i>");
	slide::param("play", parameters.play).with_label("Play");
	slide::param("gui", parameters.gui).with_label("ImGui");
	// the two copies run their own clock: "restart" sets both to the same time
	slide::action("restart", [this] { t = 0; }).with_label("Restart");
}


void scene_structure::display_frame()
{
	environment.camera_view = camera_control.camera().matrix_view();
	environment.light = camera_control.camera_model.position();

	if (parameters.play)
		t += inputs.time_interval;

	// the uniforms of the shader, read at the next draw
	environment.uniform_generic.uniform_float["time"] = t;
	environment.uniform_generic.uniform_float["amplitude"] = parameters.amplitude;
	environment.uniform_generic.uniform_float["frequency"] = parameters.frequency;
	environment.uniform_generic.uniform_int["exact_normals"] = parameters.normals == "exact" ? 1 : 0;
	draw(surface, environment);
}


void scene_structure::display_gui()
{
	// The ImGui window of the template, hidden in a slide unless gui=1 (see example c3)
	ImGui::GetStyle().Alpha = parameters.gui ? 1.0f : 0.0f;
	if (!parameters.gui) {
		ImGui::SetWindowCollapsed(true);
		ImGui::SetWindowPos(ImVec2(-10000, -10000));
		return;
	}
	ImGui::SetWindowCollapsed(false);
	ImGui::SetWindowPos(ImVec2(10, 10));
	ImGui::SliderFloat("amplitude", &parameters.amplitude, 0.0f, 0.15f);
	ImGui::SliderFloat("frequency", &parameters.frequency, 5.0f, 40.0f);
}


void scene_structure::mouse_move_event()
{
	if (!inputs.keyboard.shift)
		camera_control.action_mouse_move();
}
void scene_structure::mouse_click_event()
{
	camera_control.action_mouse_click();
}
void scene_structure::keyboard_event()
{
	camera_control.action_keyboard();
}
void scene_structure::idle_frame()
{
	camera_control.idle_frame();
}

shaders/mesh_deformation/mesh_deformation.vert.glsl

mesh_deformation.vert.glsl

Show the full file: mesh_deformation.vert.glsl (61 lines)
#version 330 core // Header for OpenGL 3.3 (CGP replaces it by the one of WebGL 2 on the web)

// Vertex shader: a plane deformed by a ripple, z = A cos(k r - 3 t), computed on the GPU.
// The normal is either the exact normal of the deformed surface (exact_normals = 1), or the normal of
// the plane before the deformation (exact_normals = 0): the shape is the same, but the lighting is wrong.

layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec3 vertex_normal;
layout (location = 2) in vec3 vertex_color;
layout (location = 3) in vec2 vertex_uv;

out struct fragment_data
{
    vec3 position;
    vec3 normal;
    vec3 color;
    vec2 uv;
} fragment;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

// Set by the program at each frame, from the parameters of the demo
uniform float time;
uniform float amplitude;      // A
uniform float frequency;      // k
uniform int exact_normals;    // 1: exact normals, 0: the normals of the plane

vec3 deformer(vec3 p0)
{
	float d = sqrt(p0.x*p0.x + p0.y*p0.y);
	return vec3(p0.x, p0.y, amplitude * cos(frequency*d - 3.0*time));
}

vec3 deformer_normal(vec3 p0)
{
	float d = max(sqrt(p0.x*p0.x + p0.y*p0.y), 1e-5);
	float omega = frequency*d - 3.0*time;
	// the partial derivatives of the deformed surface
	vec3 dpdx = vec3(1.0, 0.0, -frequency*p0.x/d*amplitude*sin(omega));
	vec3 dpdy = vec3(0.0, 1.0, -frequency*p0.y/d*amplitude*sin(omega));
	return normalize(cross(dpdx, dpdy));
}

void main()
{
	vec3 p_deformed = deformer(vertex_position);
	vec3 n_deformed = exact_normals == 1 ? deformer_normal(vertex_position) : vertex_normal;

	vec4 position = model * vec4(p_deformed, 1.0);
	mat4 modelNormal = transpose(inverse(model));
	vec4 normal = modelNormal * vec4(n_deformed, 0.0);

	fragment.position = position.xyz;
	fragment.normal   = normal.xyz;
	fragment.color = vertex_color;
	fragment.uv = vertex_uv;

	gl_Position = projection * view * position;
}

src/scene.hpp

scene.hpp

Show the full file: scene.hpp (44 lines)
#pragma once

#include "cgp/cgp.hpp"
#include "environment.hpp"

#include <string>

using cgp::mesh_drawable;


// The parameters of the demo, declared to the bridge in initialize(). Two copies of the demo, side by
// side in a slide, differ by "normals" (given in the address of each frame) and share the others
// (set by the same buttons and slider of the slide).
struct deformation_parameters {
	std::string normals = "exact"; // exact: the normals of the deformed surface; plane: those of the flat plane
	float amplitude = 0.06f;       // A
	float frequency = 20.0f;       // k
	bool play = true;
	bool gui = false;              // the ImGui window of the template (hidden in a slide)
};


struct scene_structure : cgp::scene_inputs_generic {

	void initialize();
	void display_frame();
	void display_gui();

	environment_structure environment;
	window_structure window;
	input_devices inputs;

	camera_controller_orbit_euler camera_control;
	camera_projection_perspective camera_projection;

	mesh_drawable surface;   // a grid, deformed in the vertex shader
	deformation_parameters parameters;
	float t = 0;             // the time of the deformation, restarted by "restart"

	void mouse_move_event();
	void mouse_click_event();
	void keyboard_event();
	void idle_frame();
};

slides.html

slides.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two demos side by side</title>
<!--
  A slide of reveal.js with the demo of this example. Open it through a web server, from the root of
  the repository: python3 -m http.server 8080, then http://localhost:8080/examples/c9-cgp-compare/slides.html
  reveal.js comes from a CDN: for a talk without network, download it next to this file.
-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@5.1.0/dist/reveal.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@5.1.0/dist/theme/white.css">
<style>
  .reveal iframe.demo { width: 100%; height: 540px; border: 0; }
  .reveal .row { font-size: 0.6em; margin-top: 12px; }
  .reveal .row button { font: inherit; padding: 4px 14px; margin: 0 4px; border-radius: 8px;
                        border: 2px solid #999; background: white; cursor: pointer; }
  .reveal .row button.on { background: #333; color: white; border-color: #333; }
  .reveal .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
  .reveal .pair iframe.demo { height: 460px; }
  .reveal .pair p { font-size: 0.55em; margin: 4px 0 0; }
  .reveal .row input[type=range] { width: 260px; vertical-align: middle; }
</style>
</head>
<body>
<div class="reveal"><div class="slides">
<section>
  <h3>Two demos side by side</h3>
  <div class="pair">
    <div><iframe class="demo" id="left" data-src="web/index.html?normals=exact"></iframe><p>Exact normals</p></div>
    <div><iframe class="demo" id="right" data-src="web/index.html?normals=plane"></iframe><p>Normals of the plane</p></div>
  </div>
  <div class="row">
    Amplitude <input type="range" data-demo="#left, #right" data-demo-input="ampl" min="0" max="0.15" step="0.005">
    <span data-demo="#left" data-demo-value="ampl" data-digits="3"></span>
    <button data-demo="#left, #right" data-demo-set="restart">Restart both</button>
  </div>
</section>
</div></div>
<script src="https://cdn.jsdelivr.net/npm/reveal.js@5.1.0/dist/reveal.js"></script>
<script src="../../bridge/slide-controls.js"></script>
<script>Reveal.initialize({hash: true});</script>
</body>
</html>

lhtml-site/

The same slide in lhtml-site (open it), organized as in example 1; only the slide changes, src/01_compare/index.html.j2:

= Two demos side by side

<div class="pair">
<div>
demo::../_demo/index.html?normals=exact(.live #left)
img::../_demo/poster.png(.still)
<p>Exact normals</p>
</div>
<div>
demo::../_demo/index.html?normals=plane(.live #right)
img::../_demo/poster.png(.still)
<p>Normals of the plane</p>
</div>
</div>

<div class="controls">
<p>Amplitude <input type="range" data-demo="#left, #right" data-demo-input="ampl" min="0" max="0.15" step="0.005">
<span class="value" data-demo="#left" data-demo-value="ampl" data-digits="3"></span>
<button data-demo="#left, #right" data-demo-set="restart">Restart both</button></p>
</div>

Rem. The deformation comes from a CGP example of epita_ani3d_lab_code-2027.

Next: All the files: the zips of all the examples.