9. Deux démos côte à côte
Cet exemple montre la même démo deux fois dans une slide, pour comparer deux méthodes : un plan déformé dans son vertex shader, à gauche avec les normales exactes de la surface déformée, à droite avec les normales du plan non déformé. La forme est la même, l'éclairage ne l'est pas. Un seul curseur de la slide règle l'amplitude des deux.
Télécharger : c9-cgp-compare.zip. Le zip contient le programme C++, sa slide en reveal.js (slides.html) et en lhtml-site (lhtml-site/), la compilation web de la démo (prête à montrer), le pont et le script de compilation. Le projet CGP n'est nécessaire que pour recompiler.
L'essayer : décompressez-le, et dans le dossier décompressé lancez python3 -m http.server 8080 ; puis ouvrez http://localhost:8080/examples/c9-cgp-compare/slides.html, la slide avec sa démo. Pour modifier le programme : compiler et lancer.
Ce qui est nouveau
- Une compilation, deux adresses : les deux cadres chargent la même démo ;
?normals=exactet?normals=planefont la différence. - Des contrôles qui nomment les deux cadres :
data-demo="#left, #right"(un sélecteur CSS) sur un bouton ou un curseur de la slide règle les deux démos à la fois. Les valeurs affichées viennent de la première. - Un curseur dans la slide :
<input type="range" data-demo-input="ampl">règle le paramètre pendant qu'il se déplace, et suit sa valeur. - Deux horloges : chaque copie a son propre temps. "Relancer les deux" les remet au même instant, pour que les deux ondulations restent en phase.
Rem. Deux démos sont deux contextes WebGL : le navigateur en limite le nombre dans une page (voir Le jour de l'exposé).
Compiler et lancer
Comme l'exemple 3, avec c9-cgp-compare.
Les fichiers
src/scene.cpp
Afficher le fichier complet : scene.cpp (81 lignes)
#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", ¶meters.amplitude, 0.0f, 0.15f); ImGui::SliderFloat("frequency", ¶meters.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
Afficher le fichier complet : mesh_deformation.vert.glsl (61 lignes)
#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
Afficher le fichier complet : scene.hpp (44 lignes)
#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
<!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/
La même slide en lhtml-site (l'ouvrir), organisée comme dans
l'exemple 1 ; seule la slide change, 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. La déformation provient d'un exemple CGP de epita_ani3d_lab_code-2027.
Ensuite : Tous les fichiers : les zips de tous les exemples.