Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_506_DepthMap

Visualizing the scene's depth buffer as an inset depth image.

Overview

This tutorial shows how to access and display the depth information that the renderer produces for every frame. The custom viewer draws a small depth-map inset in the corner of the window, next to the normal color rendering of a point cloud. Unlike the other 5xx effect tutorials, it uses RenderSettings; this tutorial instead overrides pre_draw() to register a screen-space overlay that samples the frame's depth texture on the GPU (all logic lives in the TutorialDepthMap class, shown after this file).

The technique

Every rasterized frame implicitly computes a depth (z-)buffer: for each pixel it stores the distance of the nearest rendered fragment to the camera, which is how hidden-surface removal works. That buffer is itself an image — a "depth map" or range image — where each pixel encodes geometry instead of color. Depth maps are the raw product of depth cameras and virtual scanners, and they are the input of many screen-space effects (shadow mapping, ambient occlusion, eye-dome lighting). Visualizing one requires mapping the depth values to grayscale: nearer surfaces appear brighter/darker than distant ones, so silhouettes and surface relief become visible as intensity variations. Here the inset samples the current frame's scene depth directly on the GPU (no readback to the CPU and no image upload), so it updates live as you navigate. Note that the perspective depth stored by the GPU is non-linear — precision is concentrated near the camera — which is why the intensity gradient is steeper for close geometry.

Easy3D API

Running it

No special keys: navigate with the standard Viewer controls (mouse drag to rotate, wheel to zoom) and watch the red-framed depth inset in the lower-left corner update with the view.

1
27#include "viewer.h"
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/drawable_points.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/util/initializer.h>
33
88using namespace easy3d;
89
90int main(int argc, char **argv) {
91 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
92 initialize();
93
94 // Create our custom viewer (a Viewer subclass that shows a live depth-map inset).
95 TutorialDepthMap viewer(EXAMPLE_TITLE);
96
97 // A scanned point cloud: rich geometry whose depth image shows clear relief.
98 const std::string file_name = resource::directory() + "/data/fountain/pointcloud.ply";
99 // Load the cloud and add it to the viewer. add_model() creates the model's renderer;
100 // the second argument (true) centers and scales the model to fit the scene.
101 auto model = viewer.add_model(file_name, true);
102 if (!model) {
103 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
104 return EXIT_FAILURE;
105 }
106
107 // "vertices" is the standard points drawable of a point cloud. Larger splats cover more
108 // pixels, so the depth buffer (and thus the depth inset) has fewer background holes.
109 auto drawable = model->renderer()->get_points_drawable("vertices");
110 drawable->set_point_size(5);
111
112 // Enter the interactive loop (returns when the window is closed).
113 return viewer.run();
114}
Definition collider.cpp:182

The header file of the viewer class:

1
27#ifndef EASY3D_TUTORIAL_DEPTH_MAP_H
28#define EASY3D_TUTORIAL_DEPTH_MAP_H
29
30#include <easy3d/viewer/viewer.h>
31
41class TutorialDepthMap : public easy3d::Viewer {
42public:
43 // Sets up a conventional camera framing the whole scene.
44 explicit TutorialDepthMap(const std::string &title = "");
45 // Nothing extra to release: the overlay commands are plain values owned by the viewer.
46 ~TutorialDepthMap() override;
47
48protected:
49 // Per-frame hook: runs the base-class pre_draw(), then refreshes the depth inset overlay.
50 void pre_draw() override;
51 // Builds (or reuses) the overlay commands that show the frame's depth in a corner quad.
52 void draw_depth() const;
53
54private:
55 // The cache is an implementation detail of the logically const draw_depth(), hence
56 // mutable: these members remember the framebuffer size / DPI the cached geometry was
57 // built for, so it can be reused until the window is resized.
58 mutable int depth_width_ = 0, depth_height_ = 0;
59 mutable float depth_dpi_ = 0;
60 mutable std::vector<easy3d::graphics::DrawCommand>
61 depth_commands_;
62};
63
64#endif // EASY3D_TUTORIAL_DEPTH_MAP_H
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143

The source file of the viewer class:

1
27#include "viewer.h"
28
29#include <easy3d/core/model.h>
30#include <easy3d/renderer/camera.h>
31#include <easy3d/renderer/drawable_points.h>
32#include <easy3d/renderer/drawable_triangles.h>
33#include <easy3d/renderer/shape.h>
34#include <easy3d/renderer/renderer.h>
35
36using namespace easy3d;
37
38// The implementation is reproduced on the example page via \include in main.cpp, so we hide
39// it from Doxygen's separate symbol documentation to avoid documenting it twice.
40// \cond
41
42TutorialDepthMap::TutorialDepthMap(const std::string &title) : Viewer(title) {
43 // A conventional viewing setup: y up, looking down -z, framed on the whole scene.
44 camera()->set_up_vector(vec3(0, 1, 0));
45 camera()->set_view_direction(vec3(0, 0, -1));
46 camera()->show_entire_scene();
47}
48
49TutorialDepthMap::~TutorialDepthMap() = default;
50
51// pre_draw() is called by the Viewer before the main scene render each frame. We keep the
52// base-class behavior and add our depth inset registration on top.
53void TutorialDepthMap::pre_draw() {
54 Viewer::pre_draw();
55 draw_depth();
56}
57
58// Builds the overlay that visualizes the frame's depth buffer in a corner quad.
59void TutorialDepthMap::draw_depth() const {
60 // High-DPI displays scale logical pixels to framebuffer pixels; all screen-space
61 // geometry must use the same scaling to keep a constant apparent size and margin.
62 const float dpi = dpi_scaling();
63 // The device generates and samples current scene depth in its render passes.
64 // Keep only the inset's immutable geometry here; presentation stays GPU-only.
65 int fw = 0, fh = 0;
66 framebuffer_size(fw, fh);
67 if (fw != depth_width_ || fh != depth_height_ || dpi != depth_dpi_) {
68 // The inset rectangle: a quarter of the framebuffer in each dimension, offset by a
69 // small DPI-scaled margin from the lower-left corner. Rect takes (xmin, xmax, ymin, ymax).
70 const Rect quad(20 * dpi, 20 * dpi + fw / 4.0f, 40 * dpi, 40 * dpi + fh / 4.0f);
71 // Two immutable overlay commands: one samples the frame's scene depth texture into
72 // the quad (as a grayscale range image, in top-left image order, entirely on the
73 // GPU), and one draws a red wire frame around it so the inset is easy to spot.
74 depth_commands_ = {
75 graphics::shape::draw_depth_texture(quad, fw, fh),
76 graphics::shape::draw_quad_wire(quad, vec4(1, 0, 0, 1), fw, fh, dpi)};
77 // Remember the configuration the cached geometry was built for; it stays valid
78 // until the window is resized or moved to a display with different DPI scaling.
79 depth_width_ = fw;
80 depth_height_ = fh;
81 depth_dpi_ = dpi;
82 }
83 // draw_depth() is logically const (it only prepares what the frame will show), so we
84 // cast away constness to hand the cached commands to the non-const Viewer API. The
85 // viewer draws them on top of the scene later in this frame.
86 const_cast<TutorialDepthMap *>(this)->set_overlays(depth_commands_);
87}
88
89// \endcond
See also
Viewer, PointCloud, DrawablePoints, Tutorial_204_Offscreen, Tutorial_505_EyeDomeLighting