Rendering a surface mesh with screen-space ambient occlusion (AO) to improve depth perception.
Overview
This tutorial adds ambient occlusion to the standard Easy3D Viewer. The portable pattern is to subclass Viewer, install a RenderSettings in init(), and replace those settings when the user changes an effect parameter. The entry point below simply loads a model, gives it a neutral white material, and runs the custom viewer; all of the effect logic lives in the TutorialAmbientOcclusion class (shown after this file).
The technique
Ambient occlusion approximates how much diffuse ambient light reaches each surface point. A point sitting in a crease, corner, or contact region is surrounded by nearby geometry that blocks part of the incoming hemisphere, so it receives less light and should look darker; a point on an exposed convex region receives almost full ambient light. Darkening occluded regions gives a strong cue of shape, depth, and contact — even with no explicit light source. Easy3D offers two complementary implementations.
The first one is computed in screen space (a GTAO-style pass): the scene depth is rendered first, then a post-process samples the neighborhood of each pixel in the depth buffer to estimate how much of the hemisphere is blocked, and multiplies the shaded color by the resulting per-pixel occlusion factor. The "radius ratio" parameter sets the world-space extent of that neighborhood as a fraction of the scene radius: a larger ratio yields broader, softer occlusion; a smaller ratio emphasizes fine creases. See RenderSettings::ambient_occlusion.
The second one is a per-vertex bake by hemisphere ray casting (SurfaceMeshAmbientOcclusion::bake()). It is view independent and deterministic, so the shading of a surface does not change while the camera moves, which is what figures in scientific publications need. Press 'b' to bake the current model once and display the occlusion through its vertex colors.
Easy3D API
- Viewer: the interactive viewer; subclass it, install RenderSettings in init(), and override key_press_event() to control the effect by replacing those settings.
- RenderSettings::ambient_occlusion: the declarative screen-space effect. Its radius_ratio tunes the sampling extent as a fraction of the scene radius.
- SurfaceMeshAmbientOcclusion: the baked alternative. bake() casts hemisphere rays per vertex and stores the occlusion in "v:ao"; colorize() turns it into the vertex colors "v:ao_color".
- DrawableTriangles: the surface drawable; set_uniform_coloring() assigns a constant material color so that the shading variation comes purely from the occlusion, not from texture.
Running it
Press 'space' to toggle ambient occlusion on/off and compare the depth cues; press 'up'/'down' to increase/decrease the occlusion radius ratio (the current value is printed to the console); press 'b' to bake the occlusion into vertex colors and compare the view-independent result.
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>
97int main(
int argc,
char **argv) {
103 const std::string file = resource::directory() +
"/data/general_open.obj";
106 TutorialAmbientOcclusion viewer(EXAMPLE_TITLE);
110 auto model = viewer.add_model(file,
true);
112 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
118 auto drawable = model->renderer()->get_triangles_drawable(
"faces");
119 drawable->set_uniform_coloring(
vec4(1.0f, 1.0f, 1.0f, 1.0f));
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_AMBIENT_OCCLUSION_H
28#define EASY3D_TUTORIAL_AMBIENT_OCCLUSION_H
30#include <easy3d/viewer/viewer.h>
44 explicit TutorialAmbientOcclusion(
const std::string &title =
"");
50 bool key_press_event(
int key,
int modifiers)
override;
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
The source file of the viewer class:
30#include <easy3d/core/surface_mesh.h>
31#include <easy3d/algo/surface_mesh_ambient_occlusion.h>
32#include <easy3d/renderer/camera.h>
33#include <easy3d/renderer/render_settings.h>
34#include <easy3d/renderer/drawable_triangles.h>
35#include <easy3d/renderer/renderer.h>
36#include <easy3d/renderer/state.h>
37#include <easy3d/util/logging.h>
46TutorialAmbientOcclusion::TutorialAmbientOcclusion(
const std::string &title) :
Viewer(title) {
48 camera()->set_view_direction(
vec3(-1, -1, -1));
52 hint_ =
"Press 'space' to switch on/off ambient occlusion\n"
53 "Press 'up/down' to increase/decrease the radius\n"
54 "Press 'b' to bake ambient occlusion into vertex colors";
57void TutorialAmbientOcclusion::init() {
63 settings.ambient_occlusion.enabled =
true;
64 set_render_settings(settings);
69bool TutorialAmbientOcclusion::key_press_event(
int key,
int modifiers) {
70 if (key == KEY_SPACE) {
73 settings.ambient_occlusion.enabled = !settings.ambient_occlusion.enabled;
74 set_render_settings(settings);
77 }
else if (key == KEY_DOWN || key == KEY_UP) {
80 if (settings.ambient_occlusion.enabled) {
81 const float delta = key == KEY_UP ? 0.01f : -0.01f;
82 settings.ambient_occlusion.radius_ratio = std::min(0.3f, std::max(
83 0.01f, settings.ambient_occlusion.radius_ratio + delta));
84 set_render_settings(settings);
85 std::cout <<
"radius ratio: " << settings.ambient_occlusion.radius_ratio << std::endl;
89 }
else if (key == KEY_B) {
93 auto mesh =
dynamic_cast<SurfaceMesh *
>(current_model());
95 LOG(INFO) <<
"the current model is not a surface mesh";
98 auto drawable = mesh->renderer()->get_triangles_drawable(
"faces");
101 if (!mesh->is_triangle_mesh()) {
103 LOG(WARNING) <<
"input is not a triangle mesh (triangulated for baking AO)";
106 auto ao = SurfaceMeshAmbientOcclusion::bake(mesh);
107 SurfaceMeshAmbientOcclusion::colorize(mesh, ao,
vec3(0.8f));
109 drawable->set_property_coloring(State::VERTEX,
"v:ao_color");
113 settings.ambient_occlusion.enabled =
false;
116 drawable->set_uniform_coloring(
vec4(1.0f, 1.0f, 1.0f, 1.0f));
120 settings.ambient_occlusion.enabled =
true;
121 LOG(INFO) <<
"removed the baked ambient occlusion";
123 set_render_settings(settings);
127 return Viewer::key_press_event(key, modifiers);
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
Declarative effect settings shared by all rendering hosts.
Definition render_settings.h:80
- See also
- RenderSettings, Viewer, DrawableTriangles