Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_405_ObjectManipulation/main.cpp

This example shows how to manipulate a model in the 3D space using the Manipulator class.

The header file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_TUTORIAL_OBJECT_MANIPULATION_VIEWER_H
28#define EASY3D_TUTORIAL_OBJECT_MANIPULATION_VIEWER_H
29
30#include <easy3d/viewer/viewer.h>
31
32#include <unordered_map>
33
34// This class demonstrates how to manipulate an object in the 3D space using the mouse
35
36namespace easy3d {
37 class Model;
38}
39
40class TutorialObjectManipulation : public easy3d::Viewer {
41public:
42 explicit TutorialObjectManipulation(const std::string &title);
43
44protected:
45 bool mouse_press_event(int x, int y, int button, int modifiers) override;
46 bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) override;
47 void draw() const override;
48
49private:
50 void mark(easy3d::Model *model);
51
52private:
53 easy3d::Model* selected_model_;
54};
55
56
57#endif // EASY3D_TUTORIAL_OBJECT_MANIPULATION_VIEWER_H
The base class of renderable 3D models.
Definition model.h:50
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182

The source file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include "viewer.h"
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>
36
37
38using namespace easy3d;
39
40// \cond
41
42TutorialObjectManipulation::TutorialObjectManipulation(const std::string &title)
43 : Viewer(title), selected_model_(nullptr)
44{
45 // We always want to look at the front of the easy3d logo.
46 camera()->setViewDirection(vec3(0, 0, -1));
47 camera()->setUpVector(vec3(0, 1, 0));
48
49 manual_ =
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";
56
57 hint_ = "Press left button to pick/unpick a model\n"
58 "When picked:\n"
59 " - Alt + left button: rotate bunny model\n"
60 " - Alt + right button: translate bunny model";
61}
62
63
64bool TutorialObjectManipulation::mouse_press_event(int x, int y, int button, int modifiers) {
65 if (modifiers != MODIF_ALT) {// this is reserved for manipulation
66 ModelPicker picker(camera());
67 auto model = picker.pick(models(), x, y);
68 if (model)
69 mark(model);
70 return true;
71 }
72 else
73 return Viewer::mouse_press_event(x, y, button, modifiers);
74}
75
76
77bool TutorialObjectManipulation::mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) {
78 // control modifier is reserved for zooming on region
79 if (modifiers == MODIF_ALT && selected_model_) {
80 ManipulatedFrame *frame = selected_model_->manipulator()->frame();
81
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;
86 switch (button) {
87 case BUTTON_LEFT:
88 frame->action_rotate(x, y, dx, dy, camera(), axis);
89 break;
90 case BUTTON_RIGHT:
91 frame->action_translate(x, y, dx, dy, camera(), axis);
92 break;
93 default:
94 break;
95 }
96 return true;
97 }
98 else
99 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
100}
101
102
103void TutorialObjectManipulation::mark(easy3d::Model *model) {
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));
109 else
110 faces->set_uniform_coloring(setting::surface_mesh_faces_color);
111 }
112 std::cout << "picked model: " << file_system::simple_name(model->name()) << std::endl;
113
114 selected_model_ = model;
115 if (!model->manipulator()) { // create manipulator if it doesn't exist
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));
119 }
120 update();
121}
122
123
124void TutorialObjectManipulation::draw() const {
125 Viewer::draw();
126 for (auto m : models()) {
127 if (m->renderer()->is_selected() && m->manipulator())
128 m->manipulator()->draw_frame(camera());
129 }
130}
131
132// \endcond
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.
@ VERTICAL
Vertical constraint.
Definition manipulated_frame.h:167
@ HORIZONTAL
Horizontal constraint.
Definition manipulated_frame.h:166
@ ORTHOGONAL
Orthogonal constraint.
Definition manipulated_frame.h:168
@ NONE
No constraint.
Definition manipulated_frame.h:165
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
std::string simple_name(const std::string &path)
Gets file name without path but with extension (e.g, /a/b/c.Ext => c.Ext)
EASY3D_UTIL_EXPORT vec4 surface_mesh_faces_color
Color of surface mesh faces.
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include "viewer.h"
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
41
42using namespace easy3d;
43
44int main(int argc, char **argv) {
45 // initialize Easy3D.
46 initialize();
47
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");
55
56 if (viewer.models().empty()) {
57 LOG(ERROR) << "failed to load the model. Please make sure the file exists and format is correct.";
58 return EXIT_FAILURE;
59 }
60
61 // run the viewer
62 return viewer.run();
63}
64
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39