Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_502_HardShadow

Rendering a scene with hard shadows using the shadow mapping technique.

Overview

This tutorial adds hard shadows to the standard Easy3D Viewer. It follows the same pattern as the other rendering-effect tutorials (e.g. Tutorial_501_AmbientOcclusion): subclass Viewer, install a RenderSettings in init(), and replace those settings to control the effect. The entry point below loads an indoor scene (room.obj), gives it a neutral gray material, and runs the custom viewer; all shadow logic lives in the TutorialHardShadow class (shown after this file). Compare with Tutorial_503_SoftShadow, which uses the same scene but produces physically more plausible soft shadow edges.

The technique

Shadow mapping (Lance Williams, "Casting curved shadows on curved surfaces", SIGGRAPH 1978) answers the visibility question "can the light see this point?" with a depth-buffer trick. In a first pass the scene is rendered from the light's point of view into a depth texture (the shadow map), which records the distance of the nearest surface to the light along every light ray. In the normal camera pass, each surface point is re-projected into the light's clip space and its depth is compared against the stored value: if the point is farther from the light than the recorded nearest depth, something blocks the light and the point lies in shadow. Because the test is a single binary comparison per pixel, the resulting shadow edges are razor sharp ("hard") and show aliasing at the shadow-map resolution — real light sources have area and produce soft penumbras (see Tutorial_503_SoftShadow). Easy3D's shadow settings also support a virtual background plane, perpendicular to the light direction at the far plane of the light frustum, that acts as an extra shadow receiver so the object casts a visible ground shadow even when the model has no floor.

Easy3D API

Running it

Press 'space' to toggle shadowing on/off and compare. Use the standard Viewer navigation (mouse drag to rotate, wheel to zoom) to inspect the shadow from different angles.

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 scene to render. room.obj contains furniture above a floor, so the objects
95 // cast clearly readable shadows onto the ground.
96 const std::string file = resource::directory() + "/data/room.obj";
97
98 // Create our custom viewer (a Viewer subclass that applies hard shadows).
99 TutorialHardShadow 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 darkened shadow regions 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_HARD_SHADOW_H
28#define EASY3D_TUTORIAL_HARD_SHADOW_H
29
30#include <easy3d/viewer/viewer.h>
31
41class TutorialHardShadow : public easy3d::Viewer {
42public:
43 // Builds the viewer; the shadow settings are installed in init().
44 explicit TutorialHardShadow(const std::string &title = "");
45
46protected:
47 // Installs the initial RenderSettings after the viewer's device is ready.
48 void init() override;
49 // Handles 'space' (toggle); forwards other keys to Viewer.
50 bool key_press_event(int key, int modifiers) override;
51};
52
53#endif // EASY3D_TUTORIAL_HARD_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
38TutorialHardShadow::TutorialHardShadow(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 TutorialHardShadow::init() {
48 Viewer::init();
49
50 // Effects are declarative: the viewer applies these settings to every frame,
51 // including file snapshots. Zero softness selects hard shadow-map edges.
52 RenderSettings settings;
53 settings.shadow.enabled = true;
54 settings.shadow.softness = 0.0f;
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 TutorialHardShadow::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_503_SoftShadow