Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_201_Viewer_default

Opening the default Easy3D Viewer to interactively inspect a 3D model.

Overview

This is the first interactive tutorial: it loads a model from disk and hands it to the built-in Viewer, which opens a window with a complete navigation interface (rotate, pan, zoom, picking, snapshots, ...). Almost every graphical Easy3D example builds on this same skeleton — initialize the library, create a Viewer, add one or more models, then call run(). The later tutorials (202-206) extend this viewer with custom GUI, multiple views, offscreen rendering, and camera control.

The technique

An interactive 3D viewer is driven by a render loop: the windowing system delivers input events (mouse drags, key presses, resize), the viewer updates the camera and scene state in response, and then redraws the framebuffer. Viewer::run() enters this loop and only returns when the user closes the window. The Camera is the piece that maps the 3D scene onto the 2D image: it holds a position and orientation (its Frame), a field of view, and near/far clipping planes, and from these it builds the view and projection matrices used by the GPU. Here we orient the camera before loading the model so the scene is presented from a known direction: looking down the -Z axis with +Y up, i.e. a standard "front" view.

Easy3D API

Running it

A window opens showing the "easy3d" logo model. Drag with the left mouse button to rotate, the right button (or wheel) to pan/zoom, and use the built-in shortcuts for snapshots and display options. Close the window to exit; run() then returns and the program ends.

1
27#include <easy3d/renderer/camera.h>
28#include <easy3d/viewer/viewer.h>
29#include <easy3d/util/resource.h>
30#include <easy3d/util/initializer.h>
31
76using namespace easy3d;
77
78int main(int argc, char **argv) {
79 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
80 initialize();
81
82 // A bundled sample model shipped with Easy3D's resource data.
83 const std::string file_name = resource::directory() + "/data/easy3d.ply";
84
85 // Create the default interactive viewer. EXAMPLE_TITLE is the backend-neutral tutorial name.
86 Viewer viewer(EXAMPLE_TITLE);
87
88 // Orient the camera to a standard front view: look along -Z with +Y pointing up.
89 viewer.camera()->set_view_direction(vec3(0, 0, -1));
90 viewer.camera()->set_up_vector(vec3(0, 1, 0));
91
92 // Load the model into the viewer (which takes ownership). add_model() returns nullptr
93 // on failure, so we bail out with an error message if the file is missing or invalid.
94 if (!viewer.add_model(file_name)) {
95 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
96 return EXIT_FAILURE;
97 }
98
99 // Enter the interactive render loop; returns only when the window is closed.
100 return viewer.run();
101}
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
Viewer, Camera, Tutorial_202_Viewer_imgui, Tutorial_203_MultiView