Easy3D 2.5.3
Tutorial_502_HardShadow

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 shadow map technique
39
40
41int main(int argc, char **argv) {
42 // initialize Easy3D.
43 initialize();
44
45 const std::string file = resource::directory() + "/data/room.obj";
46
47 // create the viewer.
48 TutorialHardShadow viewer(EXAMPLE_TITLE);
49
50 Model *model = viewer.add_model(file, true);
51 if (!model) {
52 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
53 return EXIT_FAILURE;
54 }
55
56 auto drawable = model->renderer()->get_triangles_drawable("faces");
57 drawable->set_uniform_coloring(vec4(0.9f, 0.9f, 0.9f, 1.0f));
58 drawable->set_smooth_shading(true);
59
60 // run the viewer
61 return viewer.run();
62}
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_HARD_SHADOW_H
28#define EASY3D_TUTORIAL_HARD_SHADOW_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This class renders a scene with hard shadow using the shadow map technique.
34
35namespace easy3d {
36 class Shadow;
37}
38
39class TutorialHardShadow : public easy3d::Viewer
40{
41public:
42 explicit TutorialHardShadow(const std::string& title = "");
43 ~TutorialHardShadow() override;
44
45protected:
46 bool key_press_event(int key, int modifiers) override;
47 void draw() const override;
48
49private:
50 easy3d::Shadow* shadow_;
51 bool shadow_enabled_;
52};
53
54
55#endif // EASY3D_TUTORIAL_HARD_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/shadow.h>
31#include <easy3d/renderer/camera.h>
32#include <easy3d/renderer/renderer.h>
33
34
35using namespace easy3d;
36
37TutorialHardShadow::TutorialHardShadow(const std::string& title) : Viewer(title) {
38 camera()->setUpVector(vec3(0, 1, 0));
39 camera()->setViewDirection(vec3(0, 0, 1));
40
41 shadow_ = new Shadow(camera());
42 shadow_->set_virtual_background_color(background_color());
43 shadow_enabled_ = true;
44
45 usage_string_ =
46 "----------------------- Hard Shadow usage ----------------------- \n"
47 "Press key 'space' to switch between Shadowing and normal rendering\n"
48 "----------------------------------------------------------------- \n";
49}
50
51
52TutorialHardShadow::~TutorialHardShadow() {
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 TutorialHardShadow::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 TutorialHardShadow::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}
vec4 background_color
background color of the viewer
Definition: setting.cpp:36