The source file containing the main() function:
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
36int main(
int argc,
char **argv) {
41 const std::string image_file = resource::directory() +
"/data/fountain/images/0000.jpg";
43 ImageViewer viewer(EXAMPLE_TITLE, image_file);
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:
27#ifndef EASY3D_TUTORIAL_IMAGE_VIEWER_H
28#define EASY3D_TUTORIAL_IMAGE_VIEWER_H
30#include <easy3d/viewer/viewer.h>
42 ImageViewer(
const std::string& title,
const std::string& image_file);
43 ~ImageViewer()
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: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:
29#include <easy3d/renderer/texture.h>
30#include <easy3d/renderer/shape.h>
31#include <easy3d/util/dialog.h>
32#include <easy3d/util/resource.h>
37ImageViewer::ImageViewer(
const std::string& title,
const std::string& image_file)
42 image_file_ = image_file;
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";
53ImageViewer::~ImageViewer() {
61void ImageViewer::init() {
63 texture_ = Texture::create(image_file_);
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);
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"
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 ImageViewer::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 ImageViewer::mouse_scroll_event(
int x,
int y,
int dx,
int dy) {
131void ImageViewer::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.
Definition: rect.h:42