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

This example shows how to render an images.

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 TutorialImageViewer : public easy3d::Viewer
40{
41public:
42 TutorialImageViewer(const std::string& title, const std::string& image_file);
43 ~TutorialImageViewer() 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:42
The built-in Easy3D viewer.
Definition viewer.h:63
void fit_screen(const 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:1387
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
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
37// \cond
38
39TutorialImageViewer::TutorialImageViewer(const std::string& title, const std::string& image_file)
40 : Viewer(title)
41 , scale_(1.0f)
42 , texture_(nullptr)
43{
44 image_file_ = image_file;
45
46 manual_ = "";
47 hint_ = "Ctrl + o: open an image\n"
48 "Mouse wheel: zoom in/out\n"
49 "f: reset the view";
50}
51
52
53TutorialImageViewer::~TutorialImageViewer() {
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 TutorialImageViewer::init() {
62 Viewer::init();
63 texture_ = Texture::create(image_file_);
64 fit_screen();
65}
66
67
68void TutorialImageViewer::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 TutorialImageViewer::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 TutorialImageViewer::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 TutorialImageViewer::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 TutorialImageViewer::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}
141
142// \endcond
static Texture * create(const std::string &image_file, WrapMode wrap=CLAMP_TO_EDGE, FilterMode filter=LINEAR)
Creates a texture from an image file.
Definition texture.cpp:53
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
void draw_quad_filled(const Rect &rect, const vec4 &color, int width, int height, float depth)
Draws a solid quad defined in the screen space.
Definition shape.cpp:90
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111
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 // the image file.
50 const std::string image_file = resource::directory() + "/data/fountain/images/0000.jpg";
51
52 TutorialImageViewer viewer(EXAMPLE_TITLE, image_file);
53
54 // run the viewer
55 return viewer.run();
56}
57
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