Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_503_SoftShadow

Rendering a scene with soft shadows using Percentage-Closer Soft Shadows (PCSS).

Overview

This tutorial upgrades the hard shadow mapping of Tutorial_502_HardShadow to soft shadows. The program structure is identical — subclass Viewer and install RenderSettings in init() — only the shadow softness changes from 0 to a positive value. The entry point loads the same indoor scene (room.obj) with a neutral gray material; all shadow logic lives in the TutorialSoftShadow class (shown after this file).

The technique

Real light sources have a finite area, so shadow boundaries are not sharp: near a shadow edge there is a penumbra where the receiver sees only part of the light. Percentage-Closer Soft Shadows (Randima Fernando, GPU Gems, 2005) approximates this on top of a standard shadow map in three steps per pixel: (1) blocker search — sample a neighborhood of the shadow map to estimate the average depth of the occluders between the light and the receiver; (2) penumbra estimation — from similar triangles, the farther the receiver is from the blocker, the wider the penumbra, which sets the filter kernel size; (3) Percentage- Closer Filtering (PCF) — average many binary depth comparisons over that kernel to get a fractional visibility. The result is shadows that are crisp where an object touches the floor and grow softer with distance ("contact hardening"), which is a strong realism cue. Easy3D exposes a softness parameter (scales the simulated light size) in RenderSettings and chooses an appropriate sample pattern for the active rendering device. See the renderer's shadow pass for the implementation and links to the original NVIDIA paper.

Easy3D API

Running it

Press 'space' to toggle shadowing on/off. Look at where objects meet the floor: the shadow is sharp at contact and softens with distance — compare with the uniformly hard edges of Tutorial_502_HardShadow. Standard Viewer navigation (mouse drag, wheel) applies.

1
27#include "viewer.h"
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/drawable_triangles.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 // The same scene as Tutorial_502_HardShadow: furniture above a floor, so the
95 // contact-hardening behavior of the soft shadows is easy to observe.
96 const std::string file = resource::directory() + "/data/room.obj";
97
98 // Create our custom viewer (a Viewer subclass that applies PCSS soft shadows).
99 TutorialSoftShadow viewer(EXAMPLE_TITLE);
100
101 // Load the model and add it to the viewer. add_model() creates the model's renderer;
102 // the second argument (true) centers and scales the model to fit the scene.
103 auto model = viewer.add_model(file, true);
104 if (!model) {
105 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
106 return EXIT_FAILURE;
107 }
108
109 // Give the surface a neutral light-gray material with smooth (interpolated-normal)
110 // shading so the soft penumbra gradients stand out. "faces" is the standard triangles
111 // drawable of a surface mesh.
112 auto drawable = model->renderer()->get_triangles_drawable("faces");
113 drawable->set_uniform_coloring(vec4(0.9f, 0.9f, 0.9f, 1.0f));
114 drawable->set_smooth_shading(true);
115
116 // Enter the interactive loop (returns when the window is closed).
117 return viewer.run();
118}
Definition collider.cpp:182

The header file of the viewer class:

1
27#ifndef EASY3D_TUTORIAL_SOFT_SHADOW_H
28#define EASY3D_TUTORIAL_SOFT_SHADOW_H
29
30#include <easy3d/viewer/viewer.h>
31
42class TutorialSoftShadow : public easy3d::Viewer {
43public:
44 // Builds the viewer; the soft-shadow settings are installed in init().
45 explicit TutorialSoftShadow(const std::string &title = "");
46
47protected:
48 // Installs the initial RenderSettings after the viewer's device is ready.
49 void init() override;
50 // Handles 'space' (toggle); forwards other keys to Viewer.
51 bool key_press_event(int key, int modifiers) override;
52};
53
54#endif // EASY3D_TUTORIAL_SOFT_SHADOW_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#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/render_settings.h>
31
32using namespace easy3d;
33
34// The implementation is reproduced on the example page via \include in main.cpp, so we hide
35// it from Doxygen's separate symbol documentation to avoid documenting it twice.
36// \cond
37
38TutorialSoftShadow::TutorialSoftShadow(const std::string &title) : Viewer(title) {
39 // Choose a natural "walk into the room" viewpoint: y is up and we look along +z.
40 camera()->set_up_vector(vec3(0, 1, 0));
41 camera()->set_view_direction(vec3(0, 0, 1));
42
43 // hint_ is shown in the viewer HUD; it tells the user which key controls the effect.
44 hint_ = "Press 'space' to switch on/off shadowing";
45}
46
47void TutorialSoftShadow::init() {
48 Viewer::init();
49
50 // Effects are declarative: the viewer applies these settings to every frame,
51 // including file snapshots. Positive softness selects the PCSS filter path.
52 RenderSettings settings;
53 settings.shadow.enabled = true;
54 settings.shadow.softness = 0.5f;
55 // A virtual background plane (colored like the viewer background) receives shadows,
56 // so the scene casts a ground shadow even without explicit floor geometry.
57 settings.shadow.virtual_background = true;
58 settings.shadow.virtual_background_color = background_color();
59 set_render_settings(settings);
60}
61
62// Interactive control of the effect. Returning true marks the key as handled; any key we do
63// not recognize is forwarded to the base Viewer so navigation and other shortcuts still work.
64bool TutorialSoftShadow::key_press_event(int key, int modifiers) {
65 if (key == KEY_SPACE) {
66 // Replace the settings to toggle shadowing; the viewer redraws the next frame.
67 RenderSettings settings = render_settings();
68 settings.shadow.enabled = !settings.shadow.enabled;
69 set_render_settings(settings);
70 update();
71 return true;
72 } else
73 return Viewer::key_press_event(key, modifiers);
74}
75
76// \endcond
Declarative effect settings shared by all rendering hosts.
Definition render_settings.h:80
See also
RenderSettings, Viewer, DrawableTriangles, Tutorial_502_HardShadow