This example shows how to render an images.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
34
36 class Texture;
37}
38
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
55
57
58private:
60 std::string image_file_;
61 float scale_;
62};
63
64
65#endif
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
36
37
38
39TutorialImageViewer::TutorialImageViewer(const std::string& title, const std::string& image_file)
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
57
58}
59
60
61void TutorialImageViewer::init() {
62 Viewer::init();
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";
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_;
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)
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));
140}
141
142
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