A minimal 2D image viewer built on the Easy3D Viewer, with zoom, fit, and file loading.
Overview
This tutorial turns the 3D Viewer into a simple image viewer: it displays a photograph in screen space (no 3D camera involved), supports mouse-wheel zoom, 'f' to fit the image to the window, Ctrl+O to open another image, and drag-and-drop of image files. It demonstrates that the Viewer's overlay mechanism can render flat 2D content on both the OpenGL and Vulkan backends. All the logic lives in the TutorialImageViewer class (shown after this file); main() only picks the initial image and starts the viewer.
The technique
An image is displayed by texture-mapping it onto a screen-space quad: four vertices whose positions are framebuffer pixel coordinates and whose UVs cover the full [0,1] image square. An orthographic "screen-space projection" maps pixel coordinates directly to clip space, so the quad appears exactly where we place it, independent of any 3D camera. Zooming simply rescales the quad; "fit to screen" chooses the largest scale that keeps the whole image inside the window (the minimum of the width and height ratios, preserving aspect ratio). The quad is rebuilt only when something changes (new image, zoom, resize) — a "dirty" flag avoids redundant geometry-snapshot updates every frame. This is the same textured-quad idea as Tutorial_305_Texture, but driven in pixel coordinates instead of world coordinates.
Easy3D API
- Viewer: subclassed as TutorialImageViewer; the overrides used are key_press_event(), mouse_scroll_event(), drop_event(), post_resize(), and pre_draw().
- ImageIO::load(): decodes an image file into an RGBA byte buffer.
- graphics::Image / graphics::Sampler: immutable CPU image data plus its sampling state (linear filtering here, for smooth zooming).
- graphics::DrawCommand + Viewer::set_overlays(): submit the textured quad as a screen overlay; graphics::screen_space_projection() provides the pixel-to-clip-space matrix.
Running it
The window opens showing data/fountain/images/0000.jpg fitted to the window. Scroll the mouse wheel to zoom in/out, press 'f' to fit the image again, press Ctrl+O to open a different image via a file dialog, or drag an image file onto the window. If the initial image cannot be loaded, the constructor throws and the program terminates with an error.
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
85int main(
int argc,
char **argv) {
90 const std::string image_file = resource::directory() +
"/data/fountain/images/0000.jpg";
94 TutorialImageViewer viewer(EXAMPLE_TITLE, image_file);
Definition collider.cpp:182
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>
44 TutorialImageViewer(
const std::string &title,
const std::string &image_file);
45 ~TutorialImageViewer()
override;
49 bool key_press_event(
int key,
int modifiers)
override;
51 bool mouse_scroll_event(
int x,
int y,
int dx,
int dy)
override;
55 bool drop_event(
const std::vector<std::string> &files)
override;
57 void post_resize(
int width,
int height)
override;
59 void pre_draw()
override;
63 bool load_image(
const std::string &file);
64 std::shared_ptr<const easy3d::graphics::Image> image_;
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
void fit_screen(const Model *model=nullptr) override
Moves the camera so that the entire scene or the active model is centered on the screen at a proper s...
Definition viewer.cpp:1400
The source file of the viewer class:
28#include <easy3d/fileio/image_io.h>
29#include <easy3d/util/dialog.h>
30#include <easy3d/util/resource.h>
41TutorialImageViewer::TutorialImageViewer(
const std::string &title,
const std::string &file) :
Viewer(title) {
46 "Ctrl + o: open an image\n"
47 "Mouse wheel: zoom in/out\n"
50 if (!load_image(file))
51 throw std::runtime_error(
"could not load image: " + file);
56TutorialImageViewer::~TutorialImageViewer() =
default;
60bool TutorialImageViewer::key_press_event(
int key,
int modifiers) {
66 if (key == KEY_O && (modifiers & MODIF_CTRL)) {
69 dialog::open(
"Please choose an image file", resource::directory() +
"/data/",
70 {
"Image Files (*.png *.jpg *.bmp *.ppm *.tga)",
"*.png *.jpg *.bmp *.ppm *.tga"});
71 if (!file.empty() && load_image(file)) {
77 return Viewer::key_press_event(key, modifiers);
82bool TutorialImageViewer::mouse_scroll_event(
int,
int,
int,
int dy) {
83 const float next = scale_ * std::pow(1.1f,
float(dy));
84 scale_ = std::max(1e-6f, std::min(1e6f, next));
92bool TutorialImageViewer::drop_event(
const std::vector<std::string> &files) {
93 if (files.empty() || !load_image(files.front()))
100void TutorialImageViewer::post_resize(
int,
int) {
106void TutorialImageViewer::pre_draw() {
111 const float w = image_->width() * scale_;
112 const float h = image_->height() * scale_;
115 framebuffer_size(fw, fh);
116 const float x =
static_cast<float>(fw - w) * 0.5f;
117 const float y =
static_cast<float>(fh - h) * 0.5f;
119 std::vector<graphics::Vertex> vertices(4);
120 vertices[0].position =
vec3(x, y, 0);
121 vertices[0].texcoord =
vec2(0, 0);
122 vertices[1].position =
vec3(x + w, y, 0);
123 vertices[1].texcoord =
vec2(1, 0);
124 vertices[2].position =
vec3(x + w, y + h, 0);
125 vertices[2].texcoord =
vec2(1, 1);
126 vertices[3].position =
vec3(x, y + h, 0);
127 vertices[3].texcoord =
vec2(0, 1);
132 overlay.
geometry = std::make_shared<graphics::Geometry>(vertices, std::vector<uint32_t>{0, 1, 2, 0, 2, 3});
135 set_overlays({overlay});
141void TutorialImageViewer::fit_screen() {
143 framebuffer_size(fw, fh);
144 scale_ = std::min(
float(fw) / image_->width(),
float(fh) / image_->height());
150bool TutorialImageViewer::load_image(
const std::string &file) {
151 int w = 0, h = 0, channels = 0;
152 std::vector<unsigned char> rgba;
155 if (!ImageIO::load(file, rgba, w, h, channels, 4,
false))
160 sampler.
filter = graphics::TextureFilter::linear;
161 auto image = std::make_shared<graphics::Image>(w, h, std::move(rgba), sampler);
162 image_ = std::move(image);
Complete backend-independent description of a draw operation.
Definition render_device.h:219
std::shared_ptr< const Geometry > geometry
Geometry snapshot to draw.
Definition render_device.h:220
mat4 model_view_projection
Model-to-clip transform in Easy3D/OpenGL convention.
Definition render_device.h:224
std::shared_ptr< const Image > texture
Optional texture; null selects white.
Definition render_device.h:221
Texture addressing and filtering configuration.
Definition render_device.h:165
TextureFilter filter
Interpolation used when sampling between texels.
Definition render_device.h:169
- See also
- Viewer, Tutorial_305_Texture