This example shows how to renders a surface mesh using ambient occlusion to improve depth perception.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
40
41
42
43TutorialAmbientOcclusion::TutorialAmbientOcclusion(
const std::string& title) :
Viewer(title) {
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
56
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
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
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");
101 if (!faces)
102 return;
103
104 if (ao_enabled_) {
105 ao_->generate(models_);
106
107 const mat4& MVP = camera_->modelViewProjectionMatrix();
108
109 const vec3& wCamPos = camera_->position();
110
111
112 const mat4& MV = camera_->modelViewMatrix();
114
116 if (!program) {
117 std::vector<ShaderProgram::Attribute> attributes = {
122 };
124 }
125 if (!program)
126 return;
127
134
135 program->
set_uniform(
"smooth_shading", faces->smooth_shading())
141
142 const auto& range = faces->highlight_range();
143 program->
set_uniform(
"highlight_id_min", range.first)
145
146 faces->gl_draw();
147
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
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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
46
47
48int main(int argc, char **argv) {
49
51
53
54
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
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