Easy3D 2.5.3
shadow.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_RENDERER_SHADOW_H
28#define EASY3D_RENDERER_SHADOW_H
29
30#include <vector>
31#include <easy3d/core/types.h>
32
33
34namespace easy3d {
35
36 class Camera;
37 class Frustum;
38 class TrianglesDrawable;
39 class FramebufferObject;
40
41 // Code can be simplified by omitting frustum and use the Camera class instead.
42 // check line 611, void Camera::setFOVToFitScene(), in camera.cpp.
43
44 // Optimization tips: rendering with multi-effects (e.g., shadowing, SSAO)
45 // can benefit from sharing the same geometry pass.
46
49 class Shadow
50 {
51 public:
54 explicit Shadow(Camera* cam);
55 virtual ~Shadow();
56
60 bool virtual_background() const { return virtual_background_; }
62 void set_virtual_background(bool b) { virtual_background_ = b; }
63
65 const vec4& virtual_background_color(const vec4& c) { return virtual_background_color_; }
67 void set_virtual_background_color(const vec4& c) { virtual_background_color_ = c; }
68
70 int shadow_map_size() const { return shadow_map_size_; }
72 void set_shadow_map_size(int size) { shadow_map_size_ = size; }
73
75 float light_distance() const { return light_distance_; }
79 void set_light_distance(float dist);
80
83 float darkness() const { return darkness_; }
86 void set_darkness(float darkness);
87
89 void draw(const std::vector<TrianglesDrawable*>& surfaces);
90
91 protected:
92 virtual void ensure_fbo();
93 virtual void shadow_map_pass(const std::vector<TrianglesDrawable*>& surfaces);
94 virtual void render_pass(const std::vector<TrianglesDrawable*>& surfaces);
95
96 void clear();
97 void init();
98
99 // visualize the frustum (useful for debugging)
100 void draw_light_frustum();
101
102 void compute_camera_frustum();
103 void compute_light_frustum();
104
105 // A background plane perpendicular to the light direction and
106 // is placed at the far plane of the light frustum. This only
107 // works for directional lights. It might be more natural to
108 // have a *real* ground, i.e. up right, contacting the object.
109 virtual void update_virtual_background();
110
111 protected:
112 Camera* camera_;
113
114 FramebufferObject* fbo_;
115
116 Frustum* camera_frustum_;
117 Frustum* light_frustum_;
118
119 vec3 light_pos_;
120
121 int shadow_map_size_;
122
123 // the shadow is cast onto a minimum plane orthogonal to the light direction.
124 bool virtual_background_;
125 vec4 virtual_background_color_;
126 TrianglesDrawable* virtual_background_drawable_;
127
128 float light_distance_; // for perspective light frustum only
129 float darkness_;
130
131 mat4 shadow_matrix_; // clip to texture transformation is contained
132 mat4 light_view_matrix_;
133 mat4 light_projection_matrix_;
134
135 private:
136 //copying disabled
137 Shadow(const Shadow&);
138 Shadow& operator=(const Shadow&);
139 };
140
141}
142
143#endif // EASY3D_RENDERER_SHADOW_H
A perspective or orthographic camera.
Definition: camera.h:116
An implementation of framebuffer object (FBO).
Definition: framebuffer_object.h:122
A Frustum description for perspective projection.
Definition: frustum.h:43
Shadow implements the standard shadow map (hard shadow) algorithm.
Definition: shadow.h:50
const vec4 & virtual_background_color(const vec4 &c)
Query the virtual background color.
Definition: shadow.h:65
void draw(const std::vector< TrianglesDrawable * > &surfaces)
Rendering the surfaces.
Definition: shadow.cpp:143
float light_distance() const
The distance of the light source to the scene scene (w.r.t the scene radius).
Definition: shadow.h:75
void set_light_distance(float dist)
Definition: shadow.cpp:86
void set_virtual_background(bool b)
Enable/Disable the virtual background.
Definition: shadow.h:62
Shadow(Camera *cam)
Constructor.
Definition: shadow.cpp:50
void set_shadow_map_size(int size)
Set/Change the size of the shadow map. The shadow is assumed to be square. Default: 1024 by 1024.
Definition: shadow.h:72
float darkness() const
Definition: shadow.h:83
bool virtual_background() const
Definition: shadow.h:60
int shadow_map_size() const
Query the size of the shadow map. The shadow is assumed to be square.
Definition: shadow.h:70
void set_virtual_background_color(const vec4 &c)
Set the virtual background color.
Definition: shadow.h:67
void set_darkness(float darkness)
Definition: shadow.cpp:92
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition: drawable_triangles.h:46
Definition: collider.cpp:182