Inspecting a model's interior with an interactively movable clipping plane and cross-sections.
Overview
This tutorial shows how to cut a model open while rendering it: everything on one side of a plane is hidden, so you can look inside the geometry. Easy3D ships a ready-made, manipulable ClippingPlane; the custom CrossSection viewer below simply enables it and re-routes mouse drags so the plane can be moved directly (without holding a modifier key). The entry point loads the Stanford bunny and picks a viewing direction that makes the cut easy to see.
The technique
A plane splits space into two half-spaces: for a plane with normal n and offset d, the sign of dot(n, p) + d tells on which side a point p lies. Clipping evaluates this test per fragment in the shader and discards fragments on the forbidden side — the mesh itself is never modified, so the cut can be moved interactively at no geometry-processing cost. A cross-section generalizes this to a thin slab between two parallel planes (plane0() and plane1() in ClippingPlane): only fragments inside the slab survive, revealing a slice of the model, like a CT scan slice. The slab thickness is defined relative to the scene size. A translucent guide quad is drawn to show where the plane is, and a ManipulatedFrame (a local coordinate frame with rotate/translate actions driven by mouse deltas) lets the user reorient and slide the plane through the model.
Easy3D API
- ClippingPlane: a program-wide singleton (instance()) controlling clipping; set_enabled() turns it on, and its built-in viewer shortcuts toggle visibility and cross-section mode.
- Manipulator / ManipulatedFrame: the interaction machinery; frame()->action_rotate() and action_translate() convert 2D mouse motion into 3D plane motion using the Camera.
- Viewer: subclassed here to override mouse_drag_event(); camera()->set_view_direction() aims the initial view.
Running it
Drag with the left button to rotate the clipping plane, drag with the right button to translate it. Alt+'c' enables/disables clipping, Alt+'v' shows/hides the plane guide, and Alt+'x' toggles cross-section (slab) mode.
27#include <easy3d/renderer/camera.h>
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/bunny.ply";
93 CrossSection viewer(EXAMPLE_TITLE);
96 viewer.camera()->set_view_direction(
vec3(-1, 1, -1));
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_CROSS_SECTION_H
28#define EASY3D_TUTORIAL_CROSS_SECTION_H
30#include "easy3d/viewer/viewer.h"
44 CrossSection(
const std::string &title);
49 bool mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers)
override;
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/clipping_plane.h>
29#include <easy3d/renderer/manipulated_frame.h>
30#include <easy3d/renderer/manipulator.h>
35CrossSection::CrossSection(
const std::string &title) :
Viewer(title) {
39 ClippingPlane::instance()->set_enabled(
true);
43 "Left button: rotate the clipping plane \n" "Right button: translate the clipping plane \n"
44 "Alt + 'c': enable/disable clipping plane\n" "Alt + 'v': show/hide clipping plane \n"
45 "Alt + 'x': toggle cross-section";
49bool CrossSection::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
56 if (button == BUTTON_LEFT)
57 frame->action_rotate(x, y, dx, dy, camera(), ManipulatedFrame::NONE);
58 else if (button == BUTTON_RIGHT)
59 frame->action_translate(x, y, dx, dy, camera(), ManipulatedFrame::NONE);
62 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
A Frame that can be rotated and translated using the mouse.
Definition manipulated_frame.h:58
- See also
- ClippingPlane, ManipulatedFrame, Viewer, Camera