Easy3D 2.5.3
Tutorial_503_SoftShadow

The source file containing the main() function:

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#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
34
35using namespace easy3d;
36
37// This example shows how to
38// - renders a scene with hard shadow using the Percentage-Closer Soft
39// Shadows (PCSS) technique.
40
41
42int main(int argc, char **argv) {
43 // initialize Easy3D.
44 initialize();
45
46 const std::string file = resource::directory() + "/data/room.obj";
47
48 // create the viewer.
49 TutorialSoftShadow viewer(EXAMPLE_TITLE);
50
51 Model *model = viewer.add_model(file, true);
52 if (!model) {
53 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
54 return EXIT_FAILURE;
55 }
56
57 auto drawable = model->renderer()->get_triangles_drawable("faces");
58 drawable->set_uniform_coloring(vec4(0.9f, 0.9f, 0.9f, 1.0f));
59 drawable->set_smooth_shading(true);
60
61 // run the viewer
62 return viewer.run();
63}
The base class of renderable 3D models.
Definition: model.h:49
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
TrianglesDrawable * get_triangles_drawable(const std::string &name) const
Definition: renderer.cpp:304
void set_uniform_coloring(const vec4 &color)
Definition: state.cpp:89
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35

The header file of the viewer class:

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_TUTORIAL_SOFT_SHADOW_H
28#define EASY3D_TUTORIAL_SOFT_SHADOW_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This class renders a scene with soft shadow using the
34// Percentage-Closer Soft Shadows (PCSS) technique.
35
36namespace easy3d {
37 class Shadow;
38}
39
40class TutorialSoftShadow : public easy3d::Viewer
41{
42public:
43 explicit TutorialSoftShadow(const std::string& title = "");
44 ~TutorialSoftShadow() override;
45
46protected:
47 bool key_press_event(int key, int modifiers) override;
48 void draw() const override;
49
50private:
51 easy3d::Shadow* shadow_;
52 bool shadow_enabled_;
53};
54
55
56#endif // EASY3D_TUTORIAL_SOFT_SHADOW_H
Shadow implements the standard shadow map (hard shadow) algorithm.
Definition: shadow.h:50
The built-in Easy3D viewer.
Definition: viewer.h:61

The source file of the viewer class:

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#include "viewer.h"
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/drawable_triangles.h>
30#include <easy3d/renderer/soft_shadow.h>
31#include <easy3d/renderer/camera.h>
32#include <easy3d/renderer/renderer.h>
33
34
35using namespace easy3d;
36
37TutorialSoftShadow::TutorialSoftShadow(const std::string& title) : Viewer(title) {
38 camera()->setUpVector(vec3(0, 1, 0));
39 camera()->setViewDirection(vec3(0, 0, 1));
40
41 shadow_ = new SoftShadow(camera());
42 shadow_->set_virtual_background_color(background_color());
43 shadow_enabled_ = true;
44
45 usage_string_ =
46 "------------ Soft Shadow usage ------------- \n"
47 "Press key 'space' to toggle Shadowing \n"
48 "-------------------------------------------- \n";
49}
50
51
52TutorialSoftShadow::~TutorialSoftShadow() {
53 delete shadow_;
54
55 // Not needed: it will be called in the destructor of the base class
56 //Viewer::cleanup();
57}
58
59
60bool TutorialSoftShadow::key_press_event(int key, int modifiers) {
61 if (key == KEY_SPACE) {
62 shadow_enabled_ = !shadow_enabled_;
63 update();
64 return true;
65 }
66 else
67 return Viewer::key_press_event(key, modifiers);
68}
69
70
71void TutorialSoftShadow::draw() const {
72 if (!current_model()) {
73 return;
74 }
75
76 std::vector<TrianglesDrawable*> surfaces;
77 for (auto m : models_) {
78 for (auto d : m->renderer()->triangles_drawables()) {
79 surfaces.push_back(d);
80 }
81 }
82 if (shadow_enabled_)
83 shadow_->draw(surfaces);
84 else
85 Viewer::draw();
86}
An implementation of the Percentage-Closer Soft Shadows.
Definition: soft_shadow.h:70
vec4 background_color
background color of the viewer
Definition: setting.cpp:36