Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_504_Transparency

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:

Easy3D API

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.

1
27#include "viewer.h"
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>
33
99using namespace easy3d;
100
101int main(int argc, char **argv) {
102 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
103 initialize();
104
105 // A torus knot: its tube passes over and under itself, producing many transparency
106 // layers per pixel — an ideal stress test for order-independent transparency.
107 const std::string file_name = resource::directory() + "/data/torusknot.obj";
108
109 // Create our custom viewer (a Viewer subclass that applies an OIT technique).
110 TutorialTransparency viewer(EXAMPLE_TITLE);
111
112 // Load the mesh and add it to the viewer. add_model() creates the model's renderer
113 // (including the "faces" drawable); true centers and scales the model to fit the scene.
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;
116 return EXIT_FAILURE;
117 }
118
119 // Configure the material: smooth shading for a clean look, a uniform color, and the
120 // same color for front and back faces (a distinct back color would confuse the
121 // comparison of transparency methods). The actual opacity is adjusted interactively
122 // with the 'up'/'down' keys inside the viewer.
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);
127
128 // Enter the interactive loop (returns when the window is closed).
129 return viewer.run();
130}
Definition collider.cpp:182

The header file of the viewer class:

1
27#ifndef EASY3D_TUTORIAL_TRANSPARENCY_H
28#define EASY3D_TUTORIAL_TRANSPARENCY_H
29
30#include <easy3d/viewer/viewer.h>
31
41class TutorialTransparency : public easy3d::Viewer {
42public:
43 // Builds the viewer; the initial settings are installed in init().
44 explicit TutorialTransparency(const std::string &title = "");
45
46protected:
47 // Installs the initial RenderSettings after the viewer's device is ready.
48 void init() override;
49 // Handles 'space' (cycle technique) and 'up'/'down' (opacity); forwards other keys to Viewer.
50 bool key_press_event(int key, int modifiers) override;
51
52private:
53 int method_;
54 std::string hint_string_;
55};
56
57#endif // EASY3D_TUTORIAL_TRANSPARENCY_H
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143

The source file of the viewer class:

1
27#include "viewer.h"
28#include <algorithm>
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>
34
35using namespace easy3d;
36
37namespace {
38
39 // Method indices match the old tutorial cycle: 0 disabled, 1 average, 2 peeling,
40 // 3 linked lists. Only indices 1-3 map to a RenderSettings transparency method.
41 graphics::TransparencyMethod method_from_index(int method) {
42 switch (method) {
43 case 1: return graphics::TransparencyMethod::average;
44 case 3: return graphics::TransparencyMethod::linked_lists;
45 default: return graphics::TransparencyMethod::peeling;
46 }
47 }
48
49 const char *method_name(int method) {
50 switch (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";
56 }
57 }
58
59 bool method_supported(const Viewer &viewer, int method) {
60 if (method == 0)
61 return true;
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();
65 }
66
67} // namespace
68
69// The implementation is reproduced on the example page via \include in main.cpp, so we hide
70// it from Doxygen's separate symbol documentation to avoid documenting it twice.
71// \cond
72
73TutorialTransparency::TutorialTransparency(const std::string &title) : Viewer(title) {
74 // Start with depth peeling: good quality on every rendering device (unlike linked lists,
75 // which need atomic counters and storage buffers).
76 method_ = 2;
77 // The usage/help text is the same for every method except for the trailing method name;
78 // keep the common part so each switch only appends the new name.
79 hint_string_ =
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_));
82}
83
84void TutorialTransparency::init() {
85 Viewer::init();
86
87 // Effects are declarative: the viewer applies these settings to every frame,
88 // including file snapshots.
89 RenderSettings settings;
90 settings.transparency.enabled = true;
91 settings.transparency.method = method_from_index(method_);
92 set_render_settings(settings);
93}
94
95// Interactive control: 'space' cycles the method, 'up'/'down' change the opacity.
96// Returning true marks the key as handled; unrecognized keys go to the base Viewer so
97// navigation and other shortcuts still work.
98bool TutorialTransparency::key_press_event(int key, int modifiers) {
99 if (key == KEY_SPACE) {
100 // Cycle through the four states: 0 disabled, 1 average, 2 peeling, 3 linked lists.
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;
105 method_ = previous;
106 return true;
107 }
108
109 RenderSettings settings = render_settings();
110 settings.transparency.enabled = method_ != 0;
111 if (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);
116 update();
117 return true;
118 } else if (key == KEY_DOWN || key == KEY_UP) {
119 // Change the surface opacity, clamped to a small positive value or full opacity.
120 auto *model = current_model();
121 auto *renderer = model ? model->renderer() : nullptr;
122 auto faces = renderer ? renderer->get_triangles_drawable("faces") : nullptr;
123 if (faces) {
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;
127 update();
128 }
129 return true;
130 } else
131 return Viewer::key_press_event(key, modifiers);
132}
133
134// \endcond
Declarative effect settings shared by all rendering hosts.
Definition render_settings.h:80
See also
RenderSettings, Viewer, DrawableTriangles