This example shows how to render an images.
27#ifndef EASY3D_TUTORIAL_IMAGE_VIEWER_H
28#define EASY3D_TUTORIAL_IMAGE_VIEWER_H
30#include <easy3d/viewer/viewer.h>
42 TutorialImageViewer(
const std::string& title,
const std::string& image_file);
43 ~TutorialImageViewer()
override;
46 bool key_press_event(
int key,
int modifiers)
override;
47 bool mouse_scroll_event(
int x,
int y,
int dx,
int dy)
override;
50 void draw()
const override;
52 void compute_image_region(
int& x,
int& y,
int& w,
int& h)
const;
60 std::string image_file_;
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
29#include <easy3d/renderer/texture.h>
30#include <easy3d/renderer/shape.h>
31#include <easy3d/util/dialog.h>
32#include <easy3d/util/resource.h>
39TutorialImageViewer::TutorialImageViewer(
const std::string& title,
const std::string& image_file)
44 image_file_ = image_file;
47 hint_ =
"Ctrl + o: open an image\n"
48 "Mouse wheel: zoom in/out\n"
53TutorialImageViewer::~TutorialImageViewer() {
61void TutorialImageViewer::init() {
63 texture_ = Texture::create(image_file_);
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);
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"
84 const std::string& file_name = dialog::open(title, default_path, filters);
85 if (file_name.empty())
89 texture_ = Texture::create(file_name);
91 return texture_ !=
nullptr;
98 return Viewer::key_press_event(key, modifiers);
102void TutorialImageViewer::fit_screen() {
103 if (texture_ ==
nullptr)
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);
112 scale_ =
static_cast<float>(width()) /
static_cast<float>(image_w);
117bool TutorialImageViewer::mouse_scroll_event(
int x,
int y,
int dx,
int dy) {
131void TutorialImageViewer::draw()
const {
132 if (texture_ ==
nullptr)
136 compute_image_region(x, y, w, h);
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);
The GenericRect class defines a rectangle in the 2D space, where the origin of the coordinate system ...
Definition rect.h:45