Easy3D 2.5.3
Tutorial_306_Image

The source file containing the main() function:

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
31
32using namespace easy3d;
33
34// This example shows how to render an images.
35
36int main(int argc, char **argv) {
37 // initialize Easy3D.
38 initialize();
39
40 // the image file.
41 const std::string image_file = resource::directory() + "/data/fountain/images/0000.jpg";
42
43 ImageViewer viewer(EXAMPLE_TITLE, image_file);
44
45 // run the viewer
46 return viewer.run();
47}
48
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35

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_IMAGE_VIEWER_H
28#define EASY3D_TUTORIAL_IMAGE_VIEWER_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This class visualizes an image in the screen space.
34
35namespace easy3d {
36 class Texture;
37}
38
39class ImageViewer : public easy3d::Viewer
40{
41public:
42 ImageViewer(const std::string& title, const std::string& image_file);
43 ~ImageViewer() override;
44
45protected:
46 bool key_press_event(int key, int modifiers) override;
47 bool mouse_scroll_event(int x, int y, int dx, int dy) override;
48 void init() override;
49
50 void draw() const override;
51
52 void compute_image_region(int& x, int& y, int& w, int& h) const;
53
54 // moves the camera so that the 'model' is centered on the screen.
55 // if 'model' is NULL, it centers the entire scene (all models).
56 void fit_screen();
57
58private:
59 easy3d::Texture* texture_;
60 std::string image_file_;
61 float scale_;
62};
63
64
65#endif // EASY3D_TUTORIAL_IMAGE_VIEWER_H
OpenGL texture.
Definition: texture.h:44
The built-in Easy3D viewer.
Definition: viewer.h:61
void fit_screen(const easy3d::Model *model=nullptr)
Moves the camera so that the entire scene or the active model is centered on the screen at a proper s...
Definition: viewer.cpp:1337

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
29#include <easy3d/renderer/texture.h>
30#include <easy3d/renderer/shape.h>
31#include <easy3d/util/dialog.h>
32#include <easy3d/util/resource.h>
33
34
35using namespace easy3d;
36
37ImageViewer::ImageViewer(const std::string& title, const std::string& image_file)
38 : Viewer(title)
39 , scale_(1.0f)
40 , texture_(nullptr)
41{
42 image_file_ = image_file;
43
44 usage_string_ =
45 "------------ Image Viewer usage ---------- \n"
46 "Press 'Ctrl + O' to open an image \n"
47 "Use wheel to zoom in/out \n"
48 "Press 'F' to reset the view \n"
49 "------------------------------------------ \n";
50}
51
52
53ImageViewer::~ImageViewer() {
54 delete texture_;
55
56 // Not needed: it will be called in the destructor of the base class
57 //Viewer::cleanup();
58}
59
60
61void ImageViewer::init() {
62 Viewer::init();
63 texture_ = Texture::create(image_file_);
64 fit_screen();
65}
66
67
68void ImageViewer::compute_image_region(int& x, int& y, int& w, int& h) const {
69 w = static_cast<int>(static_cast<float>(texture_->width()) * scale_);
70 h = static_cast<int>(static_cast<float>(texture_->height()) * scale_);
71 x = static_cast<int>(static_cast<float>(width() - w) * 0.5f);
72 y = static_cast<int>(static_cast<float>(height() - h) * 0.5f);
73}
74
75
76bool ImageViewer::key_press_event(int key, int modifiers) {
77 if (key == KEY_O && modifiers == MODIF_CTRL) {
78 const std::string title = "Please choose an image file";
79 const std::string default_path = resource::directory() + "/data/";
80 const std::vector<std::string> filters = {
81 "Image Files (*.png *.jpg *.bmp *.ppm *.tga)", "*.png *.jpg *.bmp *.ppm *.tga"
82 };
83
84 const std::string& file_name = dialog::open(title, default_path, filters);
85 if (file_name.empty())
86 return false;
87
88 delete texture_;
89 texture_ = Texture::create(file_name);
90 fit_screen();
91 return texture_ != nullptr;
92 }
93 if (key == KEY_F) {
94 fit_screen();
95 return true;
96 }
97 else
98 return Viewer::key_press_event(key, modifiers);
99}
100
101
102void ImageViewer::fit_screen() {
103 if (texture_ == nullptr)
104 return;
105 const int image_w = texture_->width();
106 const int image_h = texture_->height();
107 float image_as = static_cast<float>(image_w) / static_cast<float>(image_h);
108 float viewer_as = static_cast<float>(width()) / static_cast<float>(height());
109 if (image_as < viewer_as) // thin
110 scale_ = static_cast<float>(height()) / static_cast<float>(image_h);
111 else
112 scale_ = static_cast<float>(width()) / static_cast<float>(image_w);
113 update();
114}
115
116
117bool ImageViewer::mouse_scroll_event(int x, int y, int dx, int dy) {
118 (void)x;
119 (void)y;
120 (void)dx;
121 if (dy > 0)
122 scale_ *= 1.1f;
123 else if (dy < 0)
124 scale_ /= 1.1f;
125 update();
126
127 return false;
128}
129
130
131void ImageViewer::draw() const {
132 if (texture_ == nullptr)
133 return;
134
135 int x, y, w, h;
136 compute_image_region(x, y, w, h);
137
138 const Rect quad(static_cast<float>(x), static_cast<float>(x + w), static_cast<float>(y), static_cast<float>(y + h));
139 shape::draw_quad_filled(quad, texture_->id(), width(), height(), -0.9f);
140}
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42