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

This example shows how to select a model from a set of models by clicking the mouse.

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_PICKER_VIEWER_H
28#define EASY3D_TUTORIAL_PICKER_VIEWER_H
29
30#include <easy3d/viewer/viewer.h>
31
32// This class demonstrates how to pick a model using the mouse
33
34namespace easy3d {
35 class Model;
36}
37
38class TutorialModelPicker : public easy3d::Viewer {
39public:
40 explicit TutorialModelPicker(const std::string &title);
41
42protected:
43 bool mouse_press_event(int x, int y, int button, int modifiers) override;
44
45private:
46 void mark(easy3d::Model *model);
47};
48
49
50#endif // EASY3D_TUTORIAL_PICKER_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/util/file_system.h>
33#include <easy3d/util/setting.h>
34
35
36using namespace easy3d;
37
38// \cond
39
40TutorialModelPicker::TutorialModelPicker(const std::string &title)
41 : Viewer(title) {
42 // We always want to look at the front of the easy3d logo.
43 camera()->setViewDirection(vec3(0, 0, -1));
44 camera()->setUpVector(vec3(0, 1, 0));
45
46 hint_ = "Press the left button to pick/unpick a model";
47}
48
49
50bool TutorialModelPicker::mouse_press_event(int x, int y, int button, int modifiers) {
51 ModelPicker picker(camera());
52 auto model = picker.pick(models(), x, y);
53 if (model)
54 mark(model);
55
56 return Viewer::mouse_press_event(x, y, button, modifiers);
57}
58
59
60void TutorialModelPicker::mark(easy3d::Model *model) {
61 for (auto m : models()) {
62 if (m.get() == model)
63 m->renderer()->set_selected(!m->renderer()->is_selected());
64
65 auto faces = m->renderer()->get_triangles_drawable("faces");
66 if (m->renderer()->is_selected())
67 faces->set_uniform_coloring(vec4(1, 0, 0, 1.0f));
68 else
69 faces->set_uniform_coloring(setting::surface_mesh_faces_color);
70 }
71 std::cout << "picked model: " << file_system::simple_name(model->name()) << std::endl;
72 update();
73}
74
75// \endcond
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
44
45int main(int argc, char **argv) {
46 // initialize Easy3D.
47 initialize();
48
49 TutorialModelPicker viewer(EXAMPLE_TITLE);
50 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_e.ply");
51 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_a.ply");
52 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_s.ply");
53 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_y.ply");
54 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_3.ply");
55 viewer.add_model(resource::directory() + "/data/easy3d/easy3d_d.ply");
56
57 if (viewer.models().empty()) {
58 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
59 return EXIT_FAILURE;
60 }
61
62 // run the viewer
63 return viewer.run();
64}
65
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