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
- Viewer: the interactive viewer; subclass it, install RenderSettings in init(), and override key_press_event() to control the effect by replacing those settings.
- RenderSettings::shadow: the declarative shadow settings. softness 0 selects hard edges, and virtual_background/virtual_background_color enable and color the virtual receiver plane.
- DrawableTriangles: the surface drawable; set_uniform_coloring() assigns a constant material color and set_smooth_shading(true) interpolates normals, so shadowing is easy to read.
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.
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>
90int main(
int argc,
char **argv) {
96 const std::string file = resource::directory() +
"/data/room.obj";
99 TutorialHardShadow viewer(EXAMPLE_TITLE);
103 auto model = viewer.add_model(file,
true);
105 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
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);
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_HARD_SHADOW_H
28#define EASY3D_TUTORIAL_HARD_SHADOW_H
30#include <easy3d/viewer/viewer.h>
44 explicit TutorialHardShadow(
const std::string &title =
"");
50 bool key_press_event(
int key,
int modifiers)
override;
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
The source file of the viewer class:
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/render_settings.h>
38TutorialHardShadow::TutorialHardShadow(
const std::string &title) :
Viewer(title) {
40 camera()->set_up_vector(
vec3(0, 1, 0));
41 camera()->set_view_direction(
vec3(0, 0, 1));
44 hint_ =
"Press 'space' to switch on/off shadowing";
47void TutorialHardShadow::init() {
53 settings.shadow.enabled =
true;
54 settings.shadow.softness = 0.0f;
57 settings.shadow.virtual_background =
true;
58 settings.shadow.virtual_background_color = background_color();
59 set_render_settings(settings);
64bool TutorialHardShadow::key_press_event(
int key,
int modifiers) {
65 if (key == KEY_SPACE) {
68 settings.shadow.enabled = !settings.shadow.enabled;
69 set_render_settings(settings);
73 return Viewer::key_press_event(key, modifiers);
Declarative effect settings shared by all rendering hosts.
Definition render_settings.h:80
- See also
- RenderSettings, Viewer, DrawableTriangles, Tutorial_503_SoftShadow