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

This example shows how to render a point cloud without normal information (but still be able to perceive the depth) using the Eye Dome Lighting 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_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:60
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/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
41// \cond
42
43TutorialEyeDomeLighting::TutorialEyeDomeLighting(const std::string& title) : Viewer(title) {
44 camera()->setUpVector(vec3(0, 1, 0));
45 camera()->setViewDirection(vec3(0, 0, -1));
46 camera_->showEntireScene();
47
48 edl_ = new EyeDomeLighting(camera());
49 edl_enabled_ = true;
50
51 hint_ = "Press 'space' to toggle Eye Dome Lighting";
52}
53
54
55TutorialEyeDomeLighting::~TutorialEyeDomeLighting() {
56 delete edl_;
57
58 // Not needed: it will be called in the destructor of the base class
59 //Viewer::cleanup();
60}
61
62
63bool TutorialEyeDomeLighting::key_press_event(int key, int modifiers) {
64 if (key == KEY_SPACE) {
65 edl_enabled_ = !edl_enabled_;
66 std::cout << "Eye Dome Lighting " << (edl_enabled_ ? "enabled" : "disabled") << std::endl;
67 update();
68 return true;
69 }
70 else
71 return Viewer::key_press_event(key, modifiers);
72}
73
74
75void TutorialEyeDomeLighting::draw() const {
76 if (!current_model()) {
77 return;
78 }
79
80 if (edl_enabled_) {
81 const mat4& MVP = camera_->modelViewProjectionMatrix();
82 // camera position is defined in world coordinate system.
83 const vec3& wCamPos = camera_->position();
84 // it can also be computed as follows:
85 //const vec3& wCamPos = invMV * vec4(0, 0, 0, 1);
86 const mat4& MV = camera_->modelViewMatrix();
87 const vec4& wLightPos = inverse(MV) * setting::light_position;
88
89 ShaderProgram* program = ShaderManager::get_program("points/points_plain_color");
90 if (!program) {
91 std::vector<ShaderProgram::Attribute> attributes;
92 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::POSITION, "vtx_position"));
93 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::COLOR, "vtx_color"));
94 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::NORMAL, "vtx_normal"));
95 program = ShaderManager::create_program_from_files("points/points_plain_color", attributes);
96 }
97 if (!program)
98 return;
99
100 auto drawable = current_model()->renderer()->get_points_drawable("vertices");
101 float point_size = drawable->point_size();
102 glPointSize(point_size);
103
104 edl_->begin();
105 program->bind();
106 program->set_uniform("MVP", MVP);
107 program->set_uniform("wLightPos", wLightPos);
108 program->set_uniform("wCamPos", wCamPos);
109 program->set_uniform("lighting", drawable->normal_buffer());
110 program->set_uniform("per_vertex_color", drawable->coloring_method() != State::UNIFORM_COLOR && drawable->color_buffer());
111 program->set_uniform("default_color", drawable->color());
112 drawable->gl_draw();
113
114 program->release();
115 edl_->end();
116 }
117 else
118 Viewer::draw();
119}
120
121// \endcond
static ShaderProgram * create_program_from_files(const std::string &file_base_name, const std::vector< ShaderProgram::Attribute > &attributes=std::vector< ShaderProgram::Attribute >(), const std::vector< std::string > &outputs=std::vector< std::string >(), bool geom_shader=false)
Create a shader program from shader source files specified by the shader file's base name.
Definition shader_manager.cpp:49
static ShaderProgram * get_program(const std::string &shader_name)
Get the shader program if it exists and is working.
Definition shader_manager.cpp:41
OpenGL Shader Compilation.
Definition shader_program.h:75
void bind() const
Start using the program.
Definition shader_program.cpp:678
ShaderProgram * set_uniform(const std::string &name, const void *value)
Set the uniform to value.
Definition shader_program.cpp:436
void release() const
End using the program.
Definition shader_program.cpp:689
@ NORMAL
Normal.
Definition shader_program.h:81
@ COLOR
Color.
Definition shader_program.h:80
@ POSITION
Position.
Definition shader_program.h:79
std::pair< AttribType, std::string > Attribute
Attribute: a pair of attribute type and attribute name.
Definition shader_program.h:85
@ UNIFORM_COLOR
Uniformly colored.
Definition state.h:58
EASY3D_UTIL_EXPORT vec4 light_position
Default light position defined in the camera coordinate system.
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
Mat< N, N, T > inverse(const Mat< N, N, T > &m)
Returns the inverse of an N x N (square) matrix.
Definition mat.h:1190
Mat4< float > mat4
A 4 by 4 matrix of float type.
Definition types.h:67
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46
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
45
46using namespace easy3d;
47
48
49int main(int argc, char **argv) {
50 // initialize Easy3D.
51 initialize();
52
53 const std::string file = resource::directory() + "/data/bunny.bin";
54
55 // create the viewer.
56 TutorialEyeDomeLighting viewer(EXAMPLE_TITLE);
57
58 // Read the point cloud from a known file.
59 auto model = viewer.add_model(file, true);
60 if (!model) {
61 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
62 return EXIT_FAILURE;
63 }
64
65 auto drawable = model->renderer()->get_points_drawable("vertices");
66 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f));
67 drawable->set_point_size(5.0f);
68
69 // run the viewer
70 return viewer.run();
71}
72
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