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

This example shows how to renders a surface mesh using ambient occlusion to improve depth perception.

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:49
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/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
41// \cond
42
43TutorialAmbientOcclusion::TutorialAmbientOcclusion(const std::string& title) : Viewer(title) {
44 ao_ = new AmbientOcclusion(camera());
45 ao_enabled_ = true;
46
47 hint_ = "Press 'space' to switch on/off ambient occlusion\n"
48 "Press 'up/down' to increase/decrease the radius";
49}
50
51
52TutorialAmbientOcclusion::~TutorialAmbientOcclusion() {
53 delete ao_;
54
55 // Not needed: it will be called in the destructor of the base class
56 //Viewer::cleanup();
57}
58
59
60bool TutorialAmbientOcclusion::key_press_event(int key, int modifiers) {
61 if (key == KEY_SPACE) {
62 ao_enabled_ = !ao_enabled_;
63 update();
64 return true;
65 } else if (key == KEY_DOWN) {
66 if (ao_enabled_) {
67 float r = ao_->radius();
68 if (r > 0)
69 ao_->set_radius(r - 0.1f);
70 // make sure it is valid
71 if (ao_->radius() <= 0)
72 ao_->set_radius(0.1f);
73 std::cout << "radius: " << ao_->radius() << std::endl;
74 update();
75 }
76 return true;
77 } else if (key == KEY_UP) {
78 if (ao_enabled_) {
79 float r = ao_->radius();
80 if (r > 0)
81 ao_->set_radius(r + 0.1f);
82 // make sure it is valid
83 if (ao_->radius() >= 1.0f)
84 ao_->set_radius(1.0f);
85 std::cout << "radius: " << ao_->radius() << std::endl;
86 update();
87 }
88 return true;
89 } else
90 return Viewer::key_press_event(key, modifiers);
91}
92
93
94void TutorialAmbientOcclusion::draw() const {
95 if (!current_model()) {
96 return;
97 }
98
99 auto drawable = current_model()->renderer()->get_triangles_drawable("faces");
100 auto faces = dynamic_cast<TrianglesDrawable*>(drawable);
101 if (!faces)
102 return;
103
104 if (ao_enabled_) {
105 ao_->generate(models_);
106
107 const mat4& MVP = camera_->modelViewProjectionMatrix();
108 // camera position is defined in world coordinate system.
109 const vec3& wCamPos = camera_->position();
110 // it can also be computed as follows:
111 //const vec3& wCamPos = invMV * vec4(0, 0, 0, 1);
112 const mat4& MV = camera_->modelViewMatrix();
113 const vec4& wLightPos = inverse(MV) * setting::light_position;
114
115 ShaderProgram* program = ShaderManager::get_program("surface/surface");
116 if (!program) {
117 std::vector<ShaderProgram::Attribute> attributes = {
122 };
123 program = ShaderManager::create_program_from_files("surface/surface", attributes);
124 }
125 if (!program)
126 return;
127
128 program->bind();
129 program->set_uniform("MVP", MVP)
130 ->set_uniform("wLightPos", wLightPos)
131 ->set_uniform("wCamPos", wCamPos)
132 ->set_uniform("ssaoEnabled", true)
133 ->bind_texture("ssaoTexture", ao_->ssao_texture(), 0);
134
135 program->set_uniform("smooth_shading", faces->smooth_shading())
136 ->set_block_uniform("Material", "ambient", faces->material().ambient)
137 ->set_block_uniform("Material", "specular", faces->material().specular)
138 ->set_block_uniform("Material", "shininess", &faces->material().shininess)
139 ->set_uniform("per_vertex_color", faces->coloring_method() != State::UNIFORM_COLOR && drawable->color_buffer())
140 ->set_uniform("default_color", faces->color());
141
142 const auto& range = faces->highlight_range();
143 program->set_uniform("highlight_id_min", range.first)
144 ->set_uniform("highlight_id_max", range.second);
145
146 faces->gl_draw();
147
148 program->release_texture();
149 program->release();
150
151 const float x = 20.0f * dpi_scaling();
152 const float y = 100.0f * dpi_scaling();
153 const float w = static_cast<float>(width()) / 4.0f * dpi_scaling();
154 const float h = static_cast<float>(height()) / 4.0f * dpi_scaling();
155 const Rect quad(x, x+w, y, y+h);
156 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);
157 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);
158 }
159 else
160 Viewer::draw();
161}
162
163// \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
@ TEXCOORD
Texture coordinates.
Definition shader_program.h:82
@ NORMAL
Normal.
Definition shader_program.h:81
@ COLOR
Color.
Definition shader_program.h:80
@ POSITION
Position.
Definition shader_program.h:79
ShaderProgram * release_texture(unsigned int tex_target=0x0DE1)
Release a texture from the shader program.
Definition shader_program.cpp:703
ShaderProgram * bind_texture(const std::string &name, unsigned int tex_id, int unit, unsigned int tex_target=0x0DE1)
Bind a texture to the shader program.
Definition shader_program.cpp:694
std::pair< AttribType, std::string > Attribute
Attribute: a pair of attribute type and attribute name.
Definition shader_program.h:85
ShaderProgram * set_block_uniform(const std::string &blockName, const std::string &uniformName, const void *value)
Set a uniform inside a named block.
Definition shader_program.cpp:355
@ UNIFORM_COLOR
Uniformly colored.
Definition state.h:58
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
EASY3D_UTIL_EXPORT vec4 light_position
Default light position defined in the camera coordinate system.
void draw_quad_wire(const Rect &rect, const vec4 &color, int width, int height, float depth)
Draws a wire quad defined in the screen space.
Definition shape.cpp:42
void draw_depth_texture(const Rect &rect, unsigned int texture, int width, int height, float depth)
Draws a quad visualizing a depth texture in a region.
Definition shape.cpp:269
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111
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_triangles.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/util/initializer.h>
33
44
45using namespace easy3d;
46
47
48int main(int argc, char **argv) {
49 // initialize Easy3D.
50 initialize();
51
52 const std::string file = resource::directory() + "/data/general_open.obj";
53
54 // create the viewer.
55 TutorialAmbientOcclusion 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(1.0f, 1.0f, 1.0f, 1.0f));
65
66 // run the viewer
67 return viewer.run();
68}
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