This example shows how to renders a scene with hard shadow using the shadow map technique.
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_HARD_SHADOW_H
28#define EASY3D_TUTORIAL_HARD_SHADOW_H
30#include <easy3d/viewer/viewer.h>
42 explicit TutorialHardShadow(
const std::string& title =
"");
43 ~TutorialHardShadow()
override;
46 bool key_press_event(
int key,
int modifiers)
override;
47 void draw()
const override;
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:
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>
39TutorialHardShadow::TutorialHardShadow(
const std::string& title) :
Viewer(title) {
40 camera()->setUpVector(
vec3(0, 1, 0));
41 camera()->setViewDirection(
vec3(0, 0, 1));
43 shadow_ =
new Shadow(camera());
44 shadow_->set_virtual_background_color(background_color());
45 shadow_enabled_ =
true;
47 hint_ =
"Press 'space' to switch on/off shadowing";
51TutorialHardShadow::~TutorialHardShadow() {
59bool TutorialHardShadow::key_press_event(
int key,
int modifiers) {
60 if (key == KEY_SPACE) {
61 shadow_enabled_ = !shadow_enabled_;
66 return Viewer::key_press_event(key, modifiers);
70void TutorialHardShadow::draw()
const {
71 if (!current_model()) {
75 std::vector<TrianglesDrawable*> surfaces;
76 for (
auto m : models_) {
77 for (
auto d : m->renderer()->triangles_drawables()) {
78 surfaces.push_back(d.get());
82 shadow_->draw(surfaces);
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>
48int main(
int argc,
char **argv) {
52 const std::string file = resource::directory() +
"/data/room.obj";
55 TutorialHardShadow viewer(EXAMPLE_TITLE);
57 auto model = viewer.add_model(file,
true);
59 LOG(ERROR) <<
"failed to load model. Please make sure the file exists and format is correct.";
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);