Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_502_HardShadow/main.cpp

This example shows how to renders a scene with hard shadow using the shadow map technique.

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:63
Definition collider.cpp:182

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
37// \cond
38
39TutorialHardShadow::TutorialHardShadow(const std::string& title) : Viewer(title) {
40 camera()->setUpVector(vec3(0, 1, 0));
41 camera()->setViewDirection(vec3(0, 0, 1));
42
43 shadow_ = new Shadow(camera());
44 shadow_->set_virtual_background_color(background_color());
45 shadow_enabled_ = true;
46
47 hint_ = "Press 'space' to switch on/off shadowing";
48}
49
50
51TutorialHardShadow::~TutorialHardShadow() {
52 delete shadow_;
53
54 // Not needed: it will be called in the destructor of the base class
55 //Viewer::cleanup();
56}
57
58
59bool TutorialHardShadow::key_press_event(int key, int modifiers) {
60 if (key == KEY_SPACE) {
61 shadow_enabled_ = !shadow_enabled_;
62 update();
63 return true;
64 }
65 else
66 return Viewer::key_press_event(key, modifiers);
67}
68
69
70void TutorialHardShadow::draw() const {
71 if (!current_model()) {
72 return;
73 }
74
75 std::vector<TrianglesDrawable*> surfaces;
76 for (auto m : models_) {
77 for (auto d : m->renderer()->triangles_drawables()) {
78 surfaces.push_back(d.get());
79 }
80 }
81 if (shadow_enabled_)
82 shadow_->draw(surfaces);
83 else
84 Viewer::draw();
85}
86
87// \endcond
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
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
44
45using namespace easy3d;
46
47
48int main(int argc, char **argv) {
49 // initialize Easy3D.
50 initialize();
51
52 const std::string file = resource::directory() + "/data/room.obj";
53
54 // create the viewer.
55 TutorialHardShadow viewer(EXAMPLE_TITLE);
56
57 auto model = viewer.add_model(file, true);
58 if (!model) {
59 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
60 return EXIT_FAILURE;
61 }
62
63 auto drawable = model->renderer()->get_triangles_drawable("faces");
64 drawable->set_uniform_coloring(vec4(0.9f, 0.9f, 0.9f, 1.0f));
65 drawable->set_smooth_shading(true);
66
67 // run the viewer
68 return viewer.run();
69}
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46