6. Values sent back to the slide
In this example, the control points of a curve are dragged with the mouse. The curve is either a Bezier curve, which approaches its points, or a Catmull-Rom spline, which goes through them. The program computes the length of the curve, and the slide displays it while the points are moved: the value goes from the demo to the slide.
Download: c7-cgp-drag-points.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/c7-cgp-drag-points/slides.html, the slide with its demo. To change the program: build and run.
What is new
- A value computed by the program: declared read only,
slide::readout("length", ...). The panel can show it (controls=length), and the slide with<span data-demo-value="length">. - The program tells the page: when the length changes (a drag, another curve),
slide::notify(); the page shows the new values and sends them to the slide, at most once a frame. - Picking: at the click,
picking_spheresfinds the point under the mouse; while the button is down, the point follows the mouse in the plane facing the camera, and the camera stays still.
Most browsers draw WebGL lines one pixel wide, whatever glLineWidth asks: the curve is thin on a projector. A curve that must stay visible is better drawn as a tube, a mesh.
Build and run
As example 3, with c7-cgp-drag-points.
The files
src/scene.cpp
Show the full file: scene.cpp (166 lines)
// 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
Show the full file: scene.hpp (54 lines)
#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: the same as in example 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/
The same slide in lhtml-site (open it), organized as in
example 1; only the slide changes, 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)
::
Next: Complete presentations: these demos in two complete talks. Three advanced examples, 7 to 9, show files to load, keys of the demo, and two demos side by side.