Easy3D 2.5.3
Tutorial_501_AmbientOcclusion

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_triangles.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// - renders a surface mesh using ambient occlusion to improve depth perception.e.
39
40
41int main(int argc, char **argv) {
42 // initialize Easy3D.
43 initialize();
44
45 const std::string file = resource::directory() + "/data/general_open.obj";
46
47 // create the viewer.
48 TutorialAmbientOcclusion viewer(EXAMPLE_TITLE);
49
50 Model *model = viewer.add_model(file, true);
51 if (!model) {
52 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
53 return EXIT_FAILURE;
54 }
55
56 auto drawable = model->renderer()->get_triangles_drawable("faces");
57 drawable->set_uniform_coloring(vec4(1.0f, 1.0f, 1.0f, 1.0f));
58
59 // run the viewer
60 return viewer.run();
61}
The base class of renderable 3D models.
Definition: model.h:49
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
TrianglesDrawable * get_triangles_drawable(const std::string &name) const
Definition: renderer.cpp:304
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_AMBIENT_OCCLUSION_H
28#define EASY3D_TUTORIAL_AMBIENT_OCCLUSION_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This class renders a surface mesh with ambient occlusion to improve depth perception.
34
35namespace easy3d {
36 class AmbientOcclusion;
37}
38
39
40class TutorialAmbientOcclusion : public easy3d::Viewer
41{
42public:
43 explicit TutorialAmbientOcclusion(const std::string& title = "");
44 ~TutorialAmbientOcclusion() override;
45
46protected:
47 bool key_press_event(int key, int modifiers) override;
48 void draw() const override;
49
50private:
52 bool ao_enabled_;
53};
54
55
56#endif // EASY3D_TUTORIAL_AMBIENT_OCCLUSION_H
Traditional Screen Space Ambient Occlusion (SSAO) technique.
Definition: ambient_occlusion.h:50
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/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/ambient_occlusion.h>
32#include <easy3d/renderer/shader_manager.h>
33#include <easy3d/renderer/shader_program.h>
34#include <easy3d/renderer/shape.h>
35#include <easy3d/renderer/renderer.h>
36#include <easy3d/util/setting.h>
37
38
39using namespace easy3d;
40
41TutorialAmbientOcclusion::TutorialAmbientOcclusion(const std::string& title) : Viewer(title) {
42 ao_ = new AmbientOcclusion(camera());
43 ao_enabled_ = true;
44
45 usage_string_ =
46 "----------------------- Ambient Occlusion usage ------------------------- \n"
47 "Press key 'space' to switch between Ambient Occlusion and normal rendering\n"
48 "Press 'up/down' to increase/decrease the radius \n"
49 "------------------------------------------------------------------------- \n";
50}
51
52
53TutorialAmbientOcclusion::~TutorialAmbientOcclusion() {
54 delete ao_;
55
56 // Not needed: it will be called in the destructor of the base class
57 //Viewer::cleanup();
58}
59
60
61bool TutorialAmbientOcclusion::key_press_event(int key, int modifiers) {
62 if (key == KEY_SPACE) {
63 ao_enabled_ = !ao_enabled_;
64 update();
65 return true;
66 } else if (key == KEY_DOWN) {
67 if (ao_enabled_) {
68 float r = ao_->radius();
69 if (r > 0)
70 ao_->set_radius(r - 0.1f);
71 // make sure it is valid
72 if (ao_->radius() <= 0)
73 ao_->set_radius(0.1f);
74 std::cout << "radius: " << ao_->radius() << std::endl;
75 update();
76 }
77 return true;
78 } else if (key == KEY_UP) {
79 if (ao_enabled_) {
80 float r = ao_->radius();
81 if (r > 0)
82 ao_->set_radius(r + 0.1f);
83 // make sure it is valid
84 if (ao_->radius() >= 1.0f)
85 ao_->set_radius(1.0f);
86 std::cout << "radius: " << ao_->radius() << std::endl;
87 update();
88 }
89 return true;
90 } else
91 return Viewer::key_press_event(key, modifiers);
92}
93
94
95void TutorialAmbientOcclusion::draw() const {
96 if (!current_model()) {
97 return;
98 }
99
100 auto drawable = current_model()->renderer()->get_triangles_drawable("faces");
101 auto faces = dynamic_cast<TrianglesDrawable*>(drawable);
102 if (!faces)
103 return;
104
105 if (ao_enabled_) {
106 ao_->generate(models_);
107
108 const mat4& MVP = camera_->modelViewProjectionMatrix();
109 // camera position is defined in world coordinate system.
110 const vec3& wCamPos = camera_->position();
111 // it can also be computed as follows:
112 //const vec3& wCamPos = invMV * vec4(0, 0, 0, 1);
113 const mat4& MV = camera_->modelViewMatrix();
114 const vec4& wLightPos = inverse(MV) * setting::light_position;
115
116 ShaderProgram* program = ShaderManager::get_program("surface/surface");
117 if (!program) {
118 std::vector<ShaderProgram::Attribute> attributes = {
119 ShaderProgram::Attribute(ShaderProgram::POSITION, "vtx_position"),
120 ShaderProgram::Attribute(ShaderProgram::TEXCOORD, "vtx_texcoord"),
121 ShaderProgram::Attribute(ShaderProgram::COLOR, "vtx_color"),
122 ShaderProgram::Attribute(ShaderProgram::NORMAL, "vtx_normal")
123 };
124 program = ShaderManager::create_program_from_files("surface/surface", attributes);
125 }
126 if (!program)
127 return;
128
129 program->bind();
130 program->set_uniform("MVP", MVP)
131 ->set_uniform("wLightPos", wLightPos)
132 ->set_uniform("wCamPos", wCamPos)
133 ->set_uniform("ssaoEnabled", true)
134 ->bind_texture("ssaoTexture", ao_->ssao_texture(), 0);
135
136 program->set_uniform("smooth_shading", faces->smooth_shading())
137 ->set_block_uniform("Material", "ambient", faces->material().ambient)
138 ->set_block_uniform("Material", "specular", faces->material().specular)
139 ->set_block_uniform("Material", "shininess", &faces->material().shininess)
140 ->set_uniform("per_vertex_color", faces->coloring_method() != State::UNIFORM_COLOR && drawable->color_buffer())
141 ->set_uniform("default_color", faces->color());
142
143 const auto& range = faces->highlight_range();
144 program->set_uniform("highlight_id_min", range.first)
145 ->set_uniform("highlight_id_max", range.second);
146
147 faces->gl_draw();
148
149 program->release_texture();
150 program->release();
151
152 const float x = 20.0f * dpi_scaling();
153 const float y = 40.0f * dpi_scaling();
154 const float w = static_cast<float>(width()) / 4.0f * dpi_scaling();
155 const float h = static_cast<float>(height()) / 4.0f * dpi_scaling();
156 const Rect quad(x, x+w, y, y+h);
157 shape::draw_depth_texture(quad, ao_->ssao_texture(), static_cast<int>(static_cast<float>(width()) * dpi_scaling()), static_cast<int>(static_cast<float>(height()) * dpi_scaling()), -0.9f);
158 shape::draw_quad_wire(quad, vec4(1, 0,0, 1), static_cast<int>(static_cast<float>(width()) * dpi_scaling()), static_cast<int>(static_cast<float>(height()) * dpi_scaling()), -0.99f);
159 }
160 else
161 Viewer::draw();
162}
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42
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
ShaderProgram * set_block_uniform(const std::string &blockName, const std::string &uniformName, const void *value)
Definition: shader_program.cpp:355
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition: drawable_triangles.h:46
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