Selecting an individual face of a surface mesh by clicking on it with the mouse.
Overview
This tutorial refines the model-level picking of Tutorial_401_ModelPicker down to a single face: clicking a face highlights it and prints its index. The loaded model (P.off) is a quad mesh, which makes the tutorial more interesting — the GPU can only rasterize triangles, so picking must map the hit triangle back to the original polygonal face.
The technique
Easy3D picks with a GPU primitive-ID pass (see Tutorial_401_ModelPicker): the scene is re-rendered off screen with each triangle writing an encoded index instead of a color, and the pixel under the cursor is read back. Viewer::pick() returns that triangle index and, when provenance is available, the original face index. The subtlety here is that a SurfaceMesh may contain non-triangular faces: for unified rendering, every polygon is internally triangulated, and the mesh stores a "f:triangle_range" face property mapping each original face to the [first, last] range of triangles it produced. Highlighting must therefore be expressed in triangle indices: we look up the picked face's triangle range and pass it to buffer::update_highlight_mask(), so the whole polygon lights up, not just the one triangle that happened to be under the cursor.
Easy3D API
- Viewer::pick() / Viewer::PickResult: the GPU picking query; face_index identifies the original SurfaceMesh face under the cursor (-1 when nothing was hit).
- SurfaceMesh: the polygonal mesh data structure; get_face_property() retrieves the "f:triangle_range" mapping used to convert faces to triangle ranges.
- TrianglesDrawable: the drawable whose highlight mask marks the picked triangles to be drawn in the highlight color.
- buffer::update_highlight_mask(): builds the per-primitive mask from an interval of rendered primitives (here the picked face's triangle range).
- LinesDrawable: the "edges" drawable is made visible so face boundaries can be seen.
Running it
Left-click a face to highlight it (the console prints "picked face f<index>"). Clicking another face moves the highlight; clicking the background clears it. Dragging still rotates the camera because unhandled events are forwarded to the base Viewer.
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
85int main(
int argc,
char **argv) {
91 const std::string file_name = resource::directory() +
"/data/quad_mesh/P.off";
94 TutorialFacePicker viewer(EXAMPLE_TITLE);
98 if (!viewer.add_model(file_name)) {
99 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_FACE_PICKER_VIEWER_H
28#define EASY3D_TUTORIAL_FACE_PICKER_VIEWER_H
30#include <easy3d/viewer/viewer.h>
50 explicit TutorialFacePicker(
const std::string &title);
54 bool fit_scene =
true)
override;
58 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
Model * add_model(const std::string &file_name, bool create_drawables=true, bool fit_scene=true) override
Add a model loaded from a file.
Definition viewer.cpp:1306
The source file of the viewer class:
28#include <easy3d/renderer/camera.h>
29#include <easy3d/core/surface_mesh.h>
30#include <easy3d/renderer/drawable_lines.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/buffer.h>
33#include <easy3d/renderer/renderer.h>
34#include <easy3d/util/logging.h>
42TutorialFacePicker::TutorialFacePicker(
const std::string &title) :
Viewer(title) {
44 camera()->set_up_vector(
vec3(0, 1, 0));
45 camera()->set_view_direction(
vec3(0, 0, -1));
48 hint_ =
"Click the left button on a face to pick it";
53bool TutorialFacePicker::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
54 if (button == BUTTON_LEFT && modifiers == 0) {
58 const auto hit = pick(x, y);
60 for (
const auto &m : models())
61 for (const auto &d : m->renderer()->triangles_drawables())
62 d->update_highlight_mask({});
64 auto *model =
dynamic_cast<SurfaceMesh *
>(hit.model.get());
66 if (model && drawable && hit.face_index >= 0) {
69 const auto ranges = model->get_face_property<std::pair<int, int>>(
"f:triangle_range");
74 buffer::update_highlight_mask(drawable, range.first, range.second);
75 std::cout <<
"picked face f" << hit.face_index << std::endl;
81 return Viewer::mouse_press_event(x, y, button, modifiers);
85Model *TutorialFacePicker::add_model(
const std::string &file_name,
bool create,
bool fit_scene) {
86 auto model = Viewer::add_model(file_name, create, fit_scene);
93 if (
auto edges = model->renderer()->get_lines_drawable(
"edges",
false))
94 edges->set_visible(
true);
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
Definition surface_mesh.h:191
- See also
- Viewer, SurfaceMesh, TrianglesDrawable, Tutorial_401_ModelPicker