Easy3D 2.5.3
Tutorial_505_EyeDomeLighting

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_points.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// - render a point cloud without normal information (but still be
39// able to perceive the depth) using the Eye Dome Lighting technique.
40
41
42int main(int argc, char **argv) {
43 // initialize Easy3D.
44 initialize();
45
46 const std::string file = resource::directory() + "/data/bunny.bin";
47
48 // create the viewer.
49 TutorialEyeDomeLighting viewer(EXAMPLE_TITLE);
50
51 // Read the point cloud from a known file.
52 Model *model = viewer.add_model(file, true);
53 if (!model) {
54 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
55 return EXIT_FAILURE;
56 }
57
58 auto drawable = model->renderer()->get_points_drawable("vertices");
59 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f));
60 drawable->set_point_size(5.0f);
61
62 // run the viewer
63 return viewer.run();
64}
65
The base class of renderable 3D models.
Definition: model.h:49
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
PointsDrawable * get_points_drawable(const std::string &name) const
Definition: renderer.cpp:286
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_EYE_DOME_LIGHTING_H
28#define EASY3D_TUTORIAL_EYE_DOME_LIGHTING_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This class renders a point cloud without normal information.
34// It uses the Eye Dome Lighting technique to improve depth perception.
35
36namespace easy3d {
37 class EyeDomeLighting;
38}
39
40class TutorialEyeDomeLighting : public easy3d::Viewer
41{
42public:
43 explicit TutorialEyeDomeLighting(const std::string& title = "");
44 ~TutorialEyeDomeLighting() override;
45
46protected:
47 bool key_press_event(int key, int modifiers) override;
48
49 void draw() const override;
50
51private:
53 bool edl_enabled_;
54};
55
56
57#endif // EASY3D_TUTORIAL_EYE_DOME_LIGHTING_H
An implementation of the Eye Dome Lighting (EDL) technique.
Definition: eye_dome_lighting.h:71
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/point_cloud.h>
29#include <easy3d/renderer/opengl.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/camera.h>
32#include <easy3d/renderer/shader_manager.h>
33#include <easy3d/renderer/shader_program.h>
34#include <easy3d/renderer/eye_dome_lighting.h>
35#include <easy3d/renderer/renderer.h>
36#include <easy3d/util/setting.h>
37
38
39using namespace easy3d;
40
41TutorialEyeDomeLighting::TutorialEyeDomeLighting(const std::string& title) : Viewer(title) {
42 camera()->setUpVector(vec3(0, 1, 0));
43 camera()->setViewDirection(vec3(0, 0, -1));
44 camera_->showEntireScene();
45
46 edl_ = new EyeDomeLighting(camera());
47 edl_enabled_ = true;
48
49 usage_string_ =
50 "---------- Eye Dome Lighting usage --------- \n"
51 "Press key 'space' to toggle Eye Dome Lighting\n"
52 "-------------------------------------------- \n";
53}
54
55
56TutorialEyeDomeLighting::~TutorialEyeDomeLighting() {
57 delete edl_;
58
59 // Not needed: it will be called in the destructor of the base class
60 //Viewer::cleanup();
61}
62
63
64bool TutorialEyeDomeLighting::key_press_event(int key, int modifiers) {
65 if (key == KEY_SPACE) {
66 edl_enabled_ = !edl_enabled_;
67 std::cout << "Eye Dome Lighting " << (edl_enabled_ ? "enabled" : "disabled") << std::endl;
68 update();
69 return true;
70 }
71 else
72 return Viewer::key_press_event(key, modifiers);
73}
74
75
76void TutorialEyeDomeLighting::draw() const {
77 if (!current_model()) {
78 return;
79 }
80
81 if (edl_enabled_) {
82 const mat4& MVP = camera_->modelViewProjectionMatrix();
83 // camera position is defined in world coordinate system.
84 const vec3& wCamPos = camera_->position();
85 // it can also be computed as follows:
86 //const vec3& wCamPos = invMV * vec4(0, 0, 0, 1);
87 const mat4& MV = camera_->modelViewMatrix();
88 const vec4& wLightPos = inverse(MV) * setting::light_position;
89
90 ShaderProgram* program = ShaderManager::get_program("points/points_plain_color");
91 if (!program) {
92 std::vector<ShaderProgram::Attribute> attributes;
93 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::POSITION, "vtx_position"));
94 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::COLOR, "vtx_color"));
95 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::NORMAL, "vtx_normal"));
96 program = ShaderManager::create_program_from_files("points/points_plain_color", attributes);
97 }
98 if (!program)
99 return;
100
101 auto drawable = current_model()->renderer()->get_points_drawable("vertices");
102 float point_size = drawable->point_size();
103 glPointSize(point_size);
104
105 edl_->begin();
106 program->bind();
107 program->set_uniform("MVP", MVP);
108 program->set_uniform("wLightPos", wLightPos);
109 program->set_uniform("wCamPos", wCamPos);
110 program->set_uniform("lighting", drawable->normal_buffer());
111 program->set_uniform("per_vertex_color", drawable->coloring_method() != State::UNIFORM_COLOR && drawable->color_buffer());
112 program->set_uniform("default_color", drawable->color());
113 drawable->gl_draw();
114
115 program->release();
116 edl_->end();
117 }
118 else
119 Viewer::draw();
120}
OpenGL Shader Compilation.
Definition: shader_program.h:78
void bind() const
Starts using the program.
Definition: shader_program.cpp:678
void release() const
Ends using the program.
Definition: shader_program.cpp:689
Mat< N, N, T > inverse(const Mat< N, N, T > &m)
Return the inverse of N x N (square) matrix m.
Definition: mat.h:977