Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_401_ModelPicker

Selecting (and deselecting) one of several models by clicking on it with the mouse.

Overview

This tutorial loads six letter meshes (spelling "easy3d") and lets the user click a letter to toggle its selection, shown by coloring it red. It is the first of the interaction tutorials: the pattern is to subclass Viewer, override a mouse event handler, and ask the viewer what lies under the cursor. The same pattern is refined in Tutorial_402_FacePicker (picking a single face) and reused in Tutorial_404_ObjectManipulation (picking, then moving the picked model).

The technique

"Picking" means mapping a 2D cursor position back to the 3D content the user sees. The two classic approaches are:

Easy3D API

Running it

Left-click a letter to select it (it turns red and its name is printed to the console); click it again to deselect. All other mouse actions (drag to rotate, etc.) still control the camera because the handler forwards unhandled events to Viewer.

1
27#include "viewer.h"
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
88using namespace easy3d;
89
90int main(int argc, char **argv) {
91 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
92 initialize();
93
94 // Our custom viewer: a Viewer subclass that turns left clicks into selection toggles.
95 TutorialModelPicker viewer(EXAMPLE_TITLE);
96
97 // Load the six letters of the "easy3d" logo. Each add_model() call also creates the
98 // model's renderer with the default drawables ("faces", "edges", ...), which is what
99 // the GPU picking pass later rasterizes to identify the model under the cursor.
100 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_e.ply");
101 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_a.ply");
102 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_s.ply");
103 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_y.ply");
104 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_3.ply");
105 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_d.ply");
106
107 // add_model() returns nullptr on failure; if nothing loaded, there is nothing to pick.
108 if (viewer.models().empty()) {
109 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
110 return EXIT_FAILURE;
111 }
112
113 // Enter the interactive loop (returns when the window is closed).
114 return viewer.run();
115}
Definition collider.cpp:182

The header file of the viewer class:

1
27#ifndef EASY3D_TUTORIAL_MODEL_PICKER_VIEWER_H
28#define EASY3D_TUTORIAL_MODEL_PICKER_VIEWER_H
29
30#include <easy3d/viewer/viewer.h>
31
32// This class demonstrates how to pick a model using the mouse.
33
34// Forward declaration only: the header just needs the Model name for the mark() signature,
35// so we avoid pulling the full model header into every includer.
36namespace easy3d {
37 class Model;
38}
39
47class TutorialModelPicker : public easy3d::Viewer {
48public:
49 // Sets up a front view of the logo and installs the on-screen usage hint.
50 explicit TutorialModelPicker(const std::string &title);
51
52protected:
53 // Intercepts plain left clicks to pick a model; forwards everything else to Viewer.
54 bool mouse_press_event(int x, int y, int button, int modifiers) override;
55
56private:
57 // Toggles the selection of the picked model and refreshes the colors of all models.
58 void mark(easy3d::Model *model);
59};
60
61#endif // EASY3D_TUTORIAL_MODEL_PICKER_VIEWER_H
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143

The source file of the viewer class:

1
27#include "viewer.h"
28#include <easy3d/renderer/camera.h>
29#include <easy3d/core/model.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/util/file_system.h>
33#include <easy3d/util/setting.h>
34
35using namespace easy3d;
36
37// The implementation is reproduced on the example page via \include in main.cpp, so we hide
38// it from Doxygen's separate symbol documentation to avoid documenting it twice.
39// \cond
40
41TutorialModelPicker::TutorialModelPicker(const std::string &title) : Viewer(title) {
42 // We always want to look at the front of the easy3d logo.
43 camera()->set_view_direction(vec3(0, 0, -1));
44 camera()->set_up_vector(vec3(0, 1, 0));
45
46 // hint_ is shown in the viewer HUD; it tells the user how to interact.
47 hint_ = "Press the left button to pick/unpick a model";
48}
49
50// A left click without modifiers is interpreted as a pick; every other event (including
51// modifier-clicks and drags) is forwarded to the base Viewer so navigation still works.
52bool TutorialModelPicker::mouse_press_event(int x, int y, int button, int modifiers) {
53 // Mouse hooks and pick() both use logical window coordinates on both backends
54 // (DPI-independent), so the cursor can be passed straight through.
55 // pick() re-rasterizes the scene off screen with primitive IDs and reads back the pixel
56 // under the cursor. hit.model is null when the click landed on the background.
57 const auto hit = (button == BUTTON_LEFT && modifiers == 0) ? pick(x, y) : PickResult{};
58 auto *model = hit.model.get();
59 if (model)
60 mark(model);
61
62 return Viewer::mouse_press_event(x, y, button, modifiers);
63}
64
65// Visual feedback for the pick: toggle the clicked model's selection flag, then recolor
66// every model according to its (possibly updated) selection state.
67void TutorialModelPicker::mark(easy3d::Model *model) {
68 for (auto m : models()) {
69 // Only the clicked model changes state: selected becomes unselected and vice versa.
70 if (m.get() == model)
71 m->renderer()->set_selected(!m->renderer()->is_selected());
72
73 // "faces" is the standard triangles drawable of a surface mesh. Point clouds or
74 // graphs may not have one, so skip those.
75 auto faces = m->renderer()->get_triangles_drawable("faces");
76 if (!faces)
77 continue;
78 // Selected models are painted solid red; the others get the default surface color.
79 if (m->renderer()->is_selected())
80 faces->set_uniform_coloring(vec4(1, 0, 0, 1.0f));
81 else
82 faces->set_uniform_coloring(setting::surface_mesh_faces_color);
83 }
84 std::cout << "picked model: " << file_system::simple_name(model->name()) << std::endl;
85 // Request a redraw so the new colors appear immediately.
86 update();
87}
88
89// \endcond
See also
Viewer, Renderer, Tutorial_402_FacePicker, Tutorial_404_ObjectManipulation