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
- Viewer: the interactive viewer; subclass it and override pre_draw(), a hook that runs before the main scene render each frame.
- Viewer::set_overlays(): registers backend-independent graphics::DrawCommand overlays that are drawn on top of the scene.
- graphics::shape::draw_depth_texture() / draw_quad_wire(): build overlay commands that sample the frame's scene depth into a screen-space Rect and outline it, respectively.
- PointCloud / DrawablePoints: the data and its points drawable; set_point_size() enlarges the splats so the depth image has fewer holes.
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.
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>
90int main(
int argc,
char **argv) {
95 TutorialDepthMap viewer(EXAMPLE_TITLE);
98 const std::string file_name = resource::directory() +
"/data/fountain/pointcloud.ply";
101 auto model = viewer.add_model(file_name,
true);
103 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
109 auto drawable = model->renderer()->get_points_drawable(
"vertices");
110 drawable->set_point_size(5);
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_DEPTH_MAP_H
28#define EASY3D_TUTORIAL_DEPTH_MAP_H
30#include <easy3d/viewer/viewer.h>
44 explicit TutorialDepthMap(
const std::string &title =
"");
46 ~TutorialDepthMap()
override;
50 void pre_draw()
override;
52 void draw_depth()
const;
58 mutable int depth_width_ = 0, depth_height_ = 0;
59 mutable float depth_dpi_ = 0;
60 mutable std::vector<easy3d::graphics::DrawCommand>
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
The source file of the viewer class:
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>
42TutorialDepthMap::TutorialDepthMap(
const std::string &title) :
Viewer(title) {
44 camera()->set_up_vector(
vec3(0, 1, 0));
45 camera()->set_view_direction(
vec3(0, 0, -1));
46 camera()->show_entire_scene();
49TutorialDepthMap::~TutorialDepthMap() =
default;
53void TutorialDepthMap::pre_draw() {
59void TutorialDepthMap::draw_depth()
const {
62 const float dpi = dpi_scaling();
66 framebuffer_size(fw, fh);
67 if (fw != depth_width_ || fh != depth_height_ || dpi != depth_dpi_) {
70 const Rect quad(20 * dpi, 20 * dpi + fw / 4.0f, 40 * dpi, 40 * dpi + fh / 4.0f);
75 graphics::shape::draw_depth_texture(quad, fw, fh),
76 graphics::shape::draw_quad_wire(quad,
vec4(1, 0, 0, 1), fw, fh, dpi)};
86 const_cast<TutorialDepthMap *
>(
this)->set_overlays(depth_commands_);
- See also
- Viewer, PointCloud, DrawablePoints, Tutorial_204_Offscreen, Tutorial_505_EyeDomeLighting