Moving and rotating an individual model in 3D with the mouse, using the Manipulator class.
This tutorial combines picking (Tutorial_401_ModelPicker) with interactive object manipulation: click a letter of the "easy3d" logo to select it, then drag with Alt held to rotate or translate that model while the camera stays put. It is the standard "select and
transform" interaction of 3D editors and games, and the same machinery drives the interactive collision demo in Tutorial_406_CollisionDetection.
The core idea is a manipulated frame: a local coordinate frame attached to the object (origin at the object center) whose rotation and translation accumulate the user's drag gestures. The frame's matrix becomes the model's transform, so the geometry itself is never edited — moving an object is just changing one 4x4 matrix. Mouse motion is converted to frame motion by the classic trackball technique (see e.g. Chen et al., "A Study in
Interactive 3-D Rotation Using 2-D Control Devices", 1988): a 2D drag is mapped onto a virtual sphere surrounding the object, giving intuitive free rotation; translation drags the object in the screen-parallel plane. Holding 'X', 'Y', or 'O' during a drag constrains the motion to a horizontal, vertical, or view-orthogonal screen axis. The action_start() / action_end() pair brackets a drag so the frame can snapshot its state and apply incremental updates consistently. Which object receives the gestures is decided by GPU picking (Viewer::pick(), see Tutorial_401_ModelPicker), and a visual gizmo (the manipulated frame's axes) is drawn as a screen overlay so the user sees what is being manipulated.
Left-click a model to select it (click again to deselect). With a model selected: Alt + left-drag rotates it, Alt + right-drag translates it; hold 'X'/'Y'/'O' during the drag to constrain the motion to a screen axis. Without Alt, the mouse controls the camera as usual.
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
87int main(
int argc,
char **argv) {
92 TutorialObjectManipulation viewer(EXAMPLE_TITLE);
95 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_e.ply");
96 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_a.ply");
97 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_s.ply");
98 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_y.ply");
99 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_3.ply");
100 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_d.ply");
103 if (viewer.models().empty()) {
104 std::cerr <<
"failed to load the model. Please make sure the file exists and format is correct" << std::endl;
Definition collider.cpp:182
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/renderer/manipulator.h>
33#include <easy3d/renderer/manipulated_frame.h>
41TutorialObjectManipulation::TutorialObjectManipulation(
const std::string &title) :
Viewer(title) {
44 camera()->set_view_direction(
vec3(0, 0, -1));
45 camera()->set_up_vector(
vec3(0, 1, 0));
49 "-------------------- Manipulator Viewer usage ---------------------\n"
50 "Press the left button to pick/unpick a model. \n"
51 "When a model is picked, using the mouse to manipulate it: \n"
52 " - Alt + left button: rotate the model \n"
53 " - Alt + right button: translate the model \n"
54 "------------------------------------------------------------------ \n";
56 "Press left button to pick/unpick a model\n"
58 " - Alt + left button: rotate the model\n"
59 " - Alt + right button: translate the model";
63std::shared_ptr<Model> TutorialObjectManipulation::selected_model()
const {
64 auto model = selected_model_.lock();
65 return model && model->renderer() && model->manipulator() &&
66 std::find(models().begin(), models().end(), model) != models().end()
71void TutorialObjectManipulation::end_model_drag() {
72 if (
auto model = drag_model_.lock())
73 if (model->manipulator())
74 model->manipulator()->frame()->action_end();
79bool TutorialObjectManipulation::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
81 if (modifiers == MODIF_ALT && (button == BUTTON_LEFT || button == BUTTON_RIGHT)) {
82 if (
auto model = selected_model()) {
85 model->manipulator()->frame()->action_start();
90 if (modifiers == 0 && button == BUTTON_LEFT) {
94 const auto hit = pick(x, y);
95 mark(hit.model.get());
97 return Viewer::mouse_press_event(x, y, button, modifiers);
101bool TutorialObjectManipulation::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
102 const auto model = selected_model();
103 if (modifiers == MODIF_ALT && model && drag_model_.lock() == model) {
106 auto axis = ManipulatedFrame::NONE;
107 if (input_.key == KEY_X)
108 axis = ManipulatedFrame::HORIZONTAL;
109 else if (input_.key == KEY_Y)
110 axis = ManipulatedFrame::VERTICAL;
111 else if (input_.key == KEY_O)
112 axis = ManipulatedFrame::ORTHOGONAL;
113 auto *frame = model->manipulator()->frame();
114 if (button == BUTTON_LEFT)
115 frame->action_rotate(x, y, dx, dy, camera(), axis);
116 else if (button == BUTTON_RIGHT)
117 frame->action_translate(x, y, dx, dy, camera(), axis);
124 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
127bool TutorialObjectManipulation::mouse_release_event(
int x,
int y,
int button,
int modifiers) {
129 return Viewer::mouse_release_event(x, y, button, modifiers);
132bool TutorialObjectManipulation::focus_event(
bool focused) {
138void TutorialObjectManipulation::post_resize(
int,
int) {
143void TutorialObjectManipulation::mark(
Model *target) {
145 if (selected_model().get() == target)
148 selected_model_.reset();
149 for (
const auto &model : models())
150 if (model->renderer()) {
151 const bool selected = model.get() == target;
152 model->renderer()->set_selected(selected);
154 selected_model_ = model;
157 if (!model->manipulator())
158 model->set_manipulator(std::make_shared<Manipulator>(model.get()));
165void TutorialObjectManipulation::pre_draw() {
167 const auto model = selected_model();
168 if (model && model->renderer()->is_selected() && model->manipulator()) {
172 framebuffer_size(fw, fh);
173 auto command = model->manipulator()->draw_frame(camera(), fw, fh);
174 set_overlays(command.geometry ? std::vector<graphics::DrawCommand>{command}
175 : std::vector<graphics::DrawCommand>{});
179 selected_model_.reset();