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:
- Ray casting: unproject the cursor into a 3D ray and intersect it with the geometry on the CPU. This is independent of rendering, but it must traverse the geometry and can disagree with what is actually displayed (hidden objects, transforms, clipping).
- GPU rasterization ("primitive-ID" or "color coding" picking): re-render the scene off screen, but instead of shading colors each primitive writes an encoded identifier into the color target. The depth test stays enabled, so the ID stored at a pixel belongs to the nearest visible primitive. Reading back that single pixel answers the query without any CPU geometry traversal, and it automatically respects visibility, model transforms, and clipping — because it uses the very same pipeline that produced the visible image. Easy3D's Viewer::pick() implements the GPU approach for the whole scene in one call: it returns the nearest model, drawable, and primitive under a pixel, together with the depth and the world-space surface point. For model-level selection we only need the model field of the result; the primitive/face fields are used in Tutorial_402_FacePicker.
Easy3D API
- Viewer: the interactive viewer; subclass it and override mouse_press_event() to react to clicks. Unknown events are forwarded to the base class so navigation keeps working.
- Viewer::pick() / Viewer::PickResult: the scene-wide GPU picking query and its result (model, drawable, primitive, face index, position, depth).
- Renderer::set_selected(): marks a model as selected; TrianglesDrawable::set_uniform_coloring() paints the picked model red for visual feedback.
- Camera: the view is set up to look straight at the front of the logo.
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.
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
90int main(
int argc,
char **argv) {
95 TutorialModelPicker viewer(EXAMPLE_TITLE);
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");
108 if (viewer.models().empty()) {
109 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_MODEL_PICKER_VIEWER_H
28#define EASY3D_TUTORIAL_MODEL_PICKER_VIEWER_H
30#include <easy3d/viewer/viewer.h>
50 explicit TutorialModelPicker(
const std::string &title);
54 bool mouse_press_event(
int x,
int y,
int button,
int modifiers)
override;
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:
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>
41TutorialModelPicker::TutorialModelPicker(
const std::string &title) :
Viewer(title) {
43 camera()->set_view_direction(
vec3(0, 0, -1));
44 camera()->set_up_vector(
vec3(0, 1, 0));
47 hint_ =
"Press the left button to pick/unpick a model";
52bool TutorialModelPicker::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
57 const auto hit = (button == BUTTON_LEFT && modifiers == 0) ? pick(x, y) : PickResult{};
58 auto *model = hit.model.get();
62 return Viewer::mouse_press_event(x, y, button, modifiers);
68 for (
auto m : models()) {
71 m->renderer()->set_selected(!m->renderer()->is_selected());
75 auto faces = m->renderer()->get_triangles_drawable(
"faces");
79 if (m->renderer()->is_selected())
80 faces->set_uniform_coloring(
vec4(1, 0, 0, 1.0f));
82 faces->set_uniform_coloring(setting::surface_mesh_faces_color);
84 std::cout <<
"picked model: " << file_system::simple_name(model->name()) << std::endl;
- See also
- Viewer, Renderer, Tutorial_402_FacePicker, Tutorial_404_ObjectManipulation