6. Valeurs renvoyées vers la slide
Dans cet exemple, les points de contrôle d'une courbe sont déplacés à la souris. La courbe est soit une courbe de Bézier, qui approche ses points, soit une spline de Catmull-Rom, qui passe par eux. Le programme calcule la longueur de la courbe, et la slide l'affiche pendant le déplacement des points : la valeur va de la démo vers la slide.
Télécharger : c7-cgp-drag-points.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/c7-cgp-drag-points/slides.html, la slide avec sa démo. Pour modifier le programme : compiler et lancer.
Ce qui est nouveau
- Une valeur calculée par le programme : déclarée en lecture seule,
slide::readout("length", ...). Le panneau peut l'afficher (controls=length), et la slide avec<span data-demo-value="length">. - Le programme prévient la page : quand la longueur change (un déplacement, une autre courbe),
slide::notify(); la page affiche les nouvelles valeurs et les envoie à la slide, au plus une fois par image. - La sélection : au clic,
picking_spherestrouve le point sous la souris ; tant que le bouton est enfoncé, le point suit la souris dans le plan face à la caméra, et la caméra reste immobile.
La plupart des navigateurs dessinent les lignes WebGL sur un pixel de large, quoi que demande glLineWidth : la
courbe est fine au projecteur. Une courbe qui doit rester visible se dessine mieux en tube, un maillage.
Compiler et lancer
Comme l'exemple 3, avec c7-cgp-drag-points.
Les fichiers
src/scene.cpp
Afficher le fichier complet : scene.cpp (166 lignes)
// Select and drag the control points of a curve: direct manipulation in a slide. // The picking comes from the How-to "Select and drag with the mouse" of the CGP documentation. #include "scene.hpp" #include "slide_bridge.hpp" #include <cmath> using namespace cgp; static int const sample_count = 200; static float const point_radius = 0.07f; void scene_structure::initialize() { camera_control.initialize(inputs, window); camera_control.set_rotation_axis_z(); camera_control.look_at({0.0f, -1.6f, 2.4f}, {0, 0.2f, 0}, {0, 0, 1}); ball.initialize_data_on_gpu(mesh_primitive_sphere(point_radius)); // The settings of the curve, and a value computed by the program (read only): // the slide shows the length while the points are dragged slide::param("curve", settings.type, {{"bezier", "Bézier"}, {"spline", "Spline"}}); slide::param("points", settings.points, 3, 7).with_label("points").on_change([this] { reset_points(); }); slide::param("polygon", settings.polygon).with_label("Polygon"); slide::param("gui", settings.gui).with_label("ImGui"); slide::action("reset", [this] { reset_points(); }).with_label("Reset"); slide::readout("length", [this] { return length; }).with_label("length"); reset_points(); samples.resize(sample_count); update_curve(); curve.initialize_data_on_gpu(samples); curve.color = {0.85f, 0.25f, 0.15f}; } // Control points along a zigzag void scene_structure::reset_points() { int const n = settings.points; control.resize(n); for (int k = 0; k < n; ++k) control[k] = {-1.0f + 2.0f * k / (n - 1.0f), (k % 2 == 0) ? -0.2f : 0.7f, 0.0f}; picked = -1; polygon.clear(); polygon.initialize_data_on_gpu(control); polygon.color = {0.6f, 0.6f, 0.6f}; } // Bezier curve of any degree: de Casteljau static vec3 bezier(numarray<vec3> const& p, float t) { numarray<vec3> q = p; for (size_t level = 1; level < q.size(); ++level) for (size_t k = 0; k + level < q.size(); ++k) q[k] = (1 - t) * q[k] + t * q[k + 1]; return q[0]; } // Catmull-Rom spline through all the points (end points repeated) static vec3 spline(numarray<vec3> const& p, float t) { int const n = int(p.size()); float const u = t * (n - 1); int const i = std::min(int(u), n - 2); float const s = u - i; vec3 const p0 = p[std::max(i - 1, 0)], p1 = p[i], p2 = p[i + 1], p3 = p[std::min(i + 2, n - 1)]; return 0.5f * (2.0f * p1 + (-p0 + p2) * s + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * s * s + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * s * s * s); } void scene_structure::update_curve() { length = 0; for (int k = 0; k < sample_count; ++k) { float const t = k / (sample_count - 1.0f); samples[k] = (settings.type == "spline") ? spline(control, t) : bezier(control, t); if (k > 0) length += norm(samples[k] - samples[k - 1]); } } void scene_structure::display_frame() { environment.camera_view = camera_control.camera().matrix_view(); environment.light = camera_control.camera_model.position(); update_curve(); curve.vbo_position.update(samples); draw(curve, environment); if (settings.polygon) { polygon.vbo_position.update(control); draw(polygon, environment); } for (int k = 0; k < int(control.size()); ++k) { ball.model.translation = control[k]; ball.material.color = (k == picked) ? vec3{1.0f, 0.55f, 0.0f} : vec3{0.25f, 0.45f, 0.85f}; draw(ball, environment); } // The program changed a value by itself: tell the page and the slide if (std::abs(length - notified_length) > 1e-4f) { notified_length = length; slide::notify(); } } void scene_structure::display_gui() { // The template always draws its ImGui window: hidden in a slide unless gui=1 // (collapsed, out of sight and transparent: ImGui keeps a corner of a window on screen) ImGui::GetStyle().Alpha = settings.gui ? 1.0f : 0.0f; if (!settings.gui) { ImGui::SetWindowCollapsed(true); ImGui::SetWindowPos(ImVec2(-10000, -10000)); return; } ImGui::SetWindowCollapsed(false); ImGui::SetWindowPos(ImVec2(10, 10)); ImGui::Text("Dragged point: %d", picked); ImGui::Text("Length: %.3f", length); } void scene_structure::mouse_move_event() { // While a point is dragged, it follows the mouse in the plane facing the camera if (picked != -1) { intersection_structure const I = picking_plane_orthogonal_to_camera( inputs.mouse.position.current, control[picked], camera_control.camera_model, camera_projection); if (I.valid) control[picked] = I.position; } if (!inputs.keyboard.shift) camera_control.action_mouse_move(); // does nothing while is_active is false } void scene_structure::mouse_click_event() { if (inputs.mouse.click.last_action == last_mouse_cursor_action::click_left) { picking_structure const picking = picking_spheres(inputs.mouse.position.current, control, point_radius, camera_control.camera_model, camera_projection); if (picking.active) picked = picking.index; } if (inputs.mouse.click.last_action == last_mouse_cursor_action::release_left) picked = -1; camera_control.is_active = (picked == -1); // no camera motion while a point is dragged camera_control.action_mouse_click(); } void scene_structure::keyboard_event() { camera_control.action_keyboard(); } void scene_structure::idle_frame() { camera_control.idle_frame(); }
src/scene.hpp
Afficher le fichier complet : scene.hpp (54 lignes)
#pragma once #include "cgp/cgp.hpp" #include "environment.hpp" #include <string> using cgp::mesh_drawable; using cgp::curve_drawable; using cgp::numarray; using cgp::vec3; // Settings of the curve, declared to the bridge in initialize() struct curve_parameters { std::string type = "bezier"; // bezier (approaches the points) or spline (Catmull-Rom, goes through them) int points = 4; // number of control points bool polygon = true; // the control polygon 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; curve_parameters settings; numarray<vec3> control; // the control points, moved with the mouse int picked = -1; // index of the dragged point, -1 if none mesh_drawable ball; // one sphere, drawn at each control point curve_drawable curve; // the curve, sampled curve_drawable polygon; // the control polygon numarray<vec3> samples; float length = 0; // length of the curve, shown by the slide float notified_length = -1; void reset_points(); void update_curve(); void mouse_move_event(); void mouse_click_event(); void keyboard_event(); void idle_frame(); };
src/main.cpp
main.cpp : le même que dans l'exemple 4.
// CGP library #include "cgp/cgp.hpp" // Running application #include "application.hpp" // Custom scene of this code #include "scene.hpp" // interactive-3d-slides: the parameters of the demo, set from the slide #include "slide_bridge.hpp" int main(int argc, char* argv[]) { // The only change to the main() of the template: keep the arguments // (--curve spline ..., given by the address of the iframe) slide::parse(argc, argv); scene_structure scene; application_structure app; app.initialize(argv[0], &scene); app.start_loop(); return 0; }
slides.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Drag the points</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/c7-cgp-drag-points/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; } </style> </head> <body> <div class="reveal"><div class="slides"> <section> <h3>Drag the points</h3> <iframe class="demo" id="demo" data-src="web/index.html?controls=points,polygon,reset&panel=br"></iframe> <div class="row"> <button data-demo-set="curve=bezier">Bézier</button> <button data-demo-set="curve=spline">Spline</button> Length of the curve: <span data-demo-value="length" data-digits="2"></span> </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_drag-points/index.html.j2 :
{% set layout = 'side' %}
= Drag the points
Drag a blue point: the program computes the length of the curve, and the slide shows it.
<div class="controls">
<p>
<button data-demo-set="curve=bezier">Bézier</button>
<button data-demo-set="curve=spline">Spline</button>
</p>
<p>Length of the curve: <span class="value" data-demo-value="length" data-digits="2"></span></p>
</div>
media::
demo::../_demo/index.html?controls=points,polygon,reset&panel=br(.live)
img::../_demo/poster.png(.still)
::
Ensuite : Présentations complètes : ces démos dans deux exposés complets. Trois exemples avancés, 7 à 9, montrent des fichiers à charger, des touches propres à la démo, et deux démos côte à côte.