Rendering a surface mesh with order-independent transparency (OIT).
Overview
This tutorial shows how to render a semi-transparent surface mesh so that all overlapping layers remain visible and correctly composited. It renders a torus knot — a shape whose tube overlaps itself many times from most viewpoints, which makes incorrect transparency obvious. The custom viewer lets you cycle interactively between the methods available through RenderSettings: Average Color Blending, Depth Peeling, and Per Pixel Linked Lists OIT (plus a "disabled" mode for comparison). The entry point below loads the model and configures its material; the method switching lives in the TutorialTransparency class (shown after this file).
The technique
Standard alpha blending is order dependent: fragments must be composited back-to-front for the "over" operator to give the right color, but the GPU rasterizes triangles in submission order and the depth buffer keeps only the nearest fragment. Order-independent transparency (OIT) removes the need to sort. This tutorial demonstrates three approaches:
- Average Color Blending (Meshkin's method): accumulate the colors and the alphas of all transparent fragments per pixel in a single geometry pass, then blend their weighted average over the opaque scene. Fast and order independent, but it loses depth ordering, so overlapping layers look "averaged" rather than stacked.
- Depth peeling (Everitt 2001; historically also called dual depth peeling after Bavoil & Myers, NVIDIA 2008): extract transparency layers one at a time using the previous layer's depth as a clip boundary, and composite them in correct order. Easy3D's current OpenGL and Vulkan paths keep one nearest surviving fragment per peel-target sample and layer; they do not use the historical dual min-max front/back pairing. The result converges to exact transparency as the number of peels approaches the actual number of depth layers; Easy3D defaults to a small fixed number of peels for speed.
- Per-pixel linked lists (Yang et al. 2010): a build pass appends every transparent fragment to a per-pixel linked list in GPU memory using atomic operations; a resolve pass sorts each pixel's fragments by depth and composites them exactly. Perfect quality in one geometry pass, at the cost of large, scene-dependent GPU memory. It is only offered when the active rendering device advertises it; otherwise Depth Peeling is the portable high-quality choice.
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::transparency: the declarative settings. enabled must be true for the explicit transparency pass; method selects average, peeling, or linked-list compositing.
- DrawableTriangles: set_opacity() controls the material alpha interactively; set_distinct_back_color(false) gives front and back faces the same color.
Running it
Press 'space' to cycle through the methods (disabled → Average Color Blending → Depth Peeling → Per Pixel Linked Lists); the active method is printed and shown in the usage help. Press 'up'/'down' to increase/decrease the surface opacity. Rotate the torus knot and compare how each method handles the self-overlapping tube.
28#include <easy3d/core/surface_mesh.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>
101int main(
int argc,
char **argv) {
107 const std::string file_name = resource::directory() +
"/data/torusknot.obj";
110 TutorialTransparency viewer(EXAMPLE_TITLE);
114 if (!viewer.add_model(file_name,
true)) {
115 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
123 auto drawable = viewer.current_model()->renderer()->get_triangles_drawable(
"faces");
124 drawable->set_smooth_shading(
true);
125 drawable->set_coloring_method(State::UNIFORM_COLOR);
126 drawable->set_distinct_back_color(
false);
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_TRANSPARENCY_H
28#define EASY3D_TUTORIAL_TRANSPARENCY_H
30#include <easy3d/viewer/viewer.h>
44 explicit TutorialTransparency(
const std::string &title =
"");
50 bool key_press_event(
int key,
int modifiers)
override;
54 std::string hint_string_;
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
The source file of the viewer class:
29#include <easy3d/core/model.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/renderer/camera.h>
33#include <easy3d/renderer/render_settings.h>
41 graphics::TransparencyMethod method_from_index(
int method) {
43 case 1:
return graphics::TransparencyMethod::average;
44 case 3:
return graphics::TransparencyMethod::linked_lists;
45 default:
return graphics::TransparencyMethod::peeling;
49 const char *method_name(
int method) {
51 case 0:
return "NULL (transparency disabled)";
52 case 1:
return "Average Color Blending";
53 case 2:
return "Depth Peeling";
54 case 3:
return "Per Pixel Linked Lists OIT";
55 default:
return "unknown";
59 bool method_supported(
const Viewer &viewer,
int method) {
62 const auto wanted = method_from_index(method);
63 const auto &supported = viewer.device().capabilities().transparency_methods;
64 return std::find(supported.begin(), supported.end(), wanted) != supported.end();
73TutorialTransparency::TutorialTransparency(
const std::string &title) :
Viewer(title) {
80 "Press 'space' to switch between different transparency techniques\n" "Press 'up/down' to increase/decrease the transparency\n\nMethod: ";
81 Viewer::set_usage(
"default", hint_string_ + method_name(method_));
84void TutorialTransparency::init() {
90 settings.transparency.enabled =
true;
91 settings.transparency.method = method_from_index(method_);
92 set_render_settings(settings);
98bool TutorialTransparency::key_press_event(
int key,
int modifiers) {
99 if (key == KEY_SPACE) {
101 const int previous = method_;
102 method_ = (method_ + 1) % 4;
103 if (!method_supported(*
this, method_)) {
104 std::cerr << method_name(method_) <<
" is not supported by this rendering device" << std::endl;
110 settings.transparency.enabled = method_ != 0;
112 settings.transparency.method = method_from_index(method_);
113 set_usage(
"default", hint_string_ + method_name(method_));
114 std::cout <<
"Method: " << method_name(method_) << std::endl;
115 set_render_settings(settings);
118 }
else if (key == KEY_DOWN || key == KEY_UP) {
120 auto *model = current_model();
121 auto *renderer = model ? model->renderer() :
nullptr;
122 auto faces = renderer ? renderer->get_triangles_drawable(
"faces") :
nullptr;
124 const float delta = key == KEY_UP ? 0.1f : -0.1f;
125 faces->set_opacity(std::min(1.0f, std::max(0.1f, faces->opacity() + delta)));
126 std::cout <<
"opacity: " << faces->opacity() << std::endl;
131 return Viewer::key_press_event(key, modifiers);
Declarative effect settings shared by all rendering hosts.
Definition render_settings.h:80
- See also
- RenderSettings, Viewer, DrawableTriangles