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>
41int main(
int argc,
char **argv) {
45 const std::string file = resource::directory() +
"/data/general_open.obj";
48 TutorialAmbientOcclusion viewer(EXAMPLE_TITLE);
50 Model *model = viewer.add_model(file,
true);
52 LOG(ERROR) <<
"failed to load model. Please make sure the file exists and format is correct.";
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
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>
41TutorialAmbientOcclusion::TutorialAmbientOcclusion(
const std::string& title) :
Viewer(title) {
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";
53TutorialAmbientOcclusion::~TutorialAmbientOcclusion() {
61bool TutorialAmbientOcclusion::key_press_event(
int key,
int modifiers) {
62 if (key == KEY_SPACE) {
63 ao_enabled_ = !ao_enabled_;
66 }
else if (key == KEY_DOWN) {
68 float r = ao_->radius();
70 ao_->set_radius(r - 0.1f);
72 if (ao_->radius() <= 0)
73 ao_->set_radius(0.1f);
74 std::cout <<
"radius: " << ao_->radius() << std::endl;
78 }
else if (key == KEY_UP) {
80 float r = ao_->radius();
82 ao_->set_radius(r + 0.1f);
84 if (ao_->radius() >= 1.0f)
85 ao_->set_radius(1.0f);
86 std::cout <<
"radius: " << ao_->radius() << std::endl;
91 return Viewer::key_press_event(key, modifiers);
95void TutorialAmbientOcclusion::draw()
const {
96 if (!current_model()) {
100 auto drawable = current_model()->renderer()->get_triangles_drawable(
"faces");
106 ao_->generate(models_);
108 const mat4& MVP = camera_->modelViewProjectionMatrix();
110 const vec3& wCamPos = camera_->position();
113 const mat4& MV = camera_->modelViewMatrix();
114 const vec4& wLightPos =
inverse(MV) * setting::light_position;
116 ShaderProgram* program = ShaderManager::get_program(
"surface/surface");
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")
124 program = ShaderManager::create_program_from_files(
"surface/surface", attributes);
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);
136 program->set_uniform(
"smooth_shading", faces->smooth_shading())
140 ->set_uniform(
"per_vertex_color", faces->coloring_method() != State::UNIFORM_COLOR && drawable->color_buffer())
141 ->set_uniform(
"default_color", faces->color());
143 const auto& range = faces->highlight_range();
144 program->set_uniform(
"highlight_id_min", range.first)
145 ->set_uniform(
"highlight_id_max", range.second);
149 program->
release_texture();
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);
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