This example shows how to manipulate a model in the 3D space using the Manipulator class.
28#include <easy3d/gui/picker_model.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>
34#include <easy3d/util/file_system.h>
35#include <easy3d/util/setting.h>
42TutorialObjectManipulation::TutorialObjectManipulation(
const std::string &title)
43 :
Viewer(title), selected_model_(nullptr)
46 camera()->setViewDirection(
vec3(0, 0, -1));
47 camera()->setUpVector(
vec3(0, 1, 0));
50 "-------------------- Manipulator Viewer usage ---------------------\n"
51 "Press the left button to pick/unpick a model. \n"
52 "When a model is picked, using the mouse to manipulate it: \n"
53 " - Alt + left button: rotate the model \n"
54 " - Alt + right button: translate the model \n"
55 "------------------------------------------------------------------ \n";
57 hint_ =
"Press left button to pick/unpick a model\n"
59 " - Alt + left button: rotate bunny model\n"
60 " - Alt + right button: translate bunny model";
64bool TutorialObjectManipulation::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
65 if (modifiers != MODIF_ALT) {
67 auto model = picker.pick(models(), x, y);
73 return Viewer::mouse_press_event(x, y, button, modifiers);
77bool TutorialObjectManipulation::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
79 if (modifiers == MODIF_ALT && selected_model_) {
82 auto axis = ManipulatedFrame::NONE;
83 if (pressed_key_ == KEY_X) axis = ManipulatedFrame::HORIZONTAL;
84 else if (pressed_key_ == KEY_Y) axis = ManipulatedFrame::VERTICAL;
85 else if (pressed_key_ == KEY_O) axis = ManipulatedFrame::ORTHOGONAL;
99 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
104 for (
auto m : models()) {
105 m->renderer()->set_selected(m.get() == model);
106 auto faces = m->renderer()->get_triangles_drawable(
"faces");
107 if (m.get() == model)
108 faces->set_uniform_coloring(
vec4(1, 0, 0, 1.0f));
110 faces->set_uniform_coloring(setting::surface_mesh_faces_color);
112 std::cout <<
"picked model: " << file_system::simple_name(model->name()) << std::endl;
114 selected_model_ = model;
115 if (!model->manipulator()) {
116 model->set_manipulator(std::make_shared<Manipulator>(model));
117 model->manipulator()->frame()->modified.connect(
this,
118 static_cast<void (TutorialObjectManipulation::*)(
void) const
>(&TutorialObjectManipulation::update));
124void TutorialObjectManipulation::draw()
const {
126 for (
auto m : models()) {
127 if (m->renderer()->is_selected() && m->manipulator())
128 m->manipulator()->draw_frame(camera());
A Frame that can be rotated and translated using the mouse.
Definition manipulated_frame.h:57
virtual void action_translate(int mouse_x, int mouse_y, int mouse_dx, int mouse_dy, Camera *camera, ScreenAxis axis)
Translates the frame based on mouse movement.
virtual void action_rotate(int mouse_x, int mouse_y, int mouse_dx, int mouse_dy, Camera *camera, ScreenAxis axis)
Rotates the frame based on mouse movement.
Implementation of picking mechanism for set of models.
Definition picker_model.h:49
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
44int main(
int argc,
char **argv) {
48 TutorialObjectManipulation viewer(EXAMPLE_TITLE);
49 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_e.ply");
50 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_a.ply");
51 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_s.ply");
52 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_y.ply");
53 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_3.ply");
54 viewer.add_model(resource::directory() +
"/data/easy3d/easy3d_d.ply");
56 if (viewer.models().empty()) {
57 LOG(ERROR) <<
"failed to load the model. Please make sure the file exists and format is correct.";