This example shows how to renders a surface mesh using ambient occlusion to improve depth perception.
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>
43TutorialAmbientOcclusion::TutorialAmbientOcclusion(
const std::string& title) :
Viewer(title) {
47 hint_ =
"Press 'space' to switch on/off ambient occlusion\n"
48 "Press 'up/down' to increase/decrease the radius";
52TutorialAmbientOcclusion::~TutorialAmbientOcclusion() {
60bool TutorialAmbientOcclusion::key_press_event(
int key,
int modifiers) {
61 if (key == KEY_SPACE) {
62 ao_enabled_ = !ao_enabled_;
65 }
else if (key == KEY_DOWN) {
67 float r = ao_->radius();
69 ao_->set_radius(r - 0.1f);
71 if (ao_->radius() <= 0)
72 ao_->set_radius(0.1f);
73 std::cout <<
"radius: " << ao_->radius() << std::endl;
77 }
else if (key == KEY_UP) {
79 float r = ao_->radius();
81 ao_->set_radius(r + 0.1f);
83 if (ao_->radius() >= 1.0f)
84 ao_->set_radius(1.0f);
85 std::cout <<
"radius: " << ao_->radius() << std::endl;
90 return Viewer::key_press_event(key, modifiers);
94void TutorialAmbientOcclusion::draw()
const {
95 if (!current_model()) {
99 auto drawable = current_model()->renderer()->get_triangles_drawable(
"faces");
105 ao_->generate(models_);
107 const mat4& MVP = camera_->modelViewProjectionMatrix();
109 const vec3& wCamPos = camera_->position();
112 const mat4& MV = camera_->modelViewMatrix();
113 const vec4& wLightPos =
inverse(MV) * setting::light_position;
115 ShaderProgram* program = ShaderManager::get_program(
"surface/surface");
117 std::vector<ShaderProgram::Attribute> attributes = {
123 program = ShaderManager::create_program_from_files(
"surface/surface", attributes);
135 program->
set_uniform(
"smooth_shading", faces->smooth_shading())
139 ->
set_uniform(
"per_vertex_color", faces->coloring_method() != State::UNIFORM_COLOR && drawable->color_buffer())
142 const auto& range = faces->highlight_range();
143 program->
set_uniform(
"highlight_id_min", range.first)
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);
The GenericRect class defines a rectangle in the 2D space, where the origin of the coordinate system ...
Definition rect.h:45
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
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
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)
Returns the inverse of an N x N (square) matrix.
Definition mat.h:1190