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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
34
36 class Shadow;
37}
38
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:
51 bool shadow_enabled_;
52};
53
54
55#endif
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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
36
37
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
55
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
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
46
47
48int main(int argc, char **argv) {
49
51
53
54
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
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