This example shows how to render a surface mesh with transparency effect using the following techniques
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#ifndef EASY3D_TUTORIAL_TRANSPARENCY_H
28#define EASY3D_TUTORIAL_TRANSPARENCY_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33
34
35
37 class Transparency;
38}
39
41{
42public:
43 explicit TutorialTransparency(const std::string& title = "");
44 ~TutorialTransparency() override;
45
46protected:
47 bool key_press_event(int key, int modifiers) override;
48 void draw() const override;
49
50private:
52 int method_;
53};
54
55
56#endif
Base class for rendering with transparency.
Definition transparency.h:45
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "viewer.h"
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/average_color_blending.h>
30#include <easy3d/renderer/dual_depth_peeling.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/renderer/camera.h>
34
35
37
38
39
40TutorialTransparency::TutorialTransparency(
const std::string& title) :
Viewer(title) {
42 std::cout << "method: Dual Depth Peeling" << std::endl;
43 method_ = 2;
44
45 hint_ = "Press 'space' to switch between different transparency techniques\n"
46 "Press 'up/down' to increase/decrease the transparency";
47}
48
49
50TutorialTransparency::~TutorialTransparency() {
51 delete transparency_;
52
53
54
55}
56
57
58bool TutorialTransparency::key_press_event(int key, int modifiers) {
59 if (key == KEY_SPACE) {
60 if (transparency_) {
61 delete transparency_;
62 transparency_ = nullptr;
63 }
64
65 method_ = (method_ + 1) % 3;
66 switch (method_)
67 {
68 case 0:
69 std::cout << "method: NULL(transparency disabled)" << std::endl;
70 break;
71 case 1:
73 std::cout << "method: Average Color Blending" << std::endl;
74 break;
75 case 2:
77 std::cout << "method: Dual Depth Peeling" << std::endl;
78 break;
79 }
80 update();
81 return true;
82 }
83 else if (key == KEY_DOWN) {
84 auto faces = current_model()->renderer()->get_triangles_drawable("faces");
85 if (faces) {
86 float o = faces->opacity();
87 if (o > 0)
88 faces->set_opacity(o - 0.1f);
89
90 if (faces->opacity() <= 0)
91 faces->set_opacity(0.1f);
92 std::cout << "opacity: " << faces->opacity() << std::endl;
93 update();
94 }
95 return true;
96 }
97 else if (key == KEY_UP) {
98 auto faces = current_model()->renderer()->get_triangles_drawable("faces");
99 if (faces) {
100 float o = faces->opacity();
101 if (o > 0)
102 faces->set_opacity(o + 0.1f);
103
104 if (faces->opacity() >= 1.0f)
105 faces->set_opacity(1.0f);
106 std::cout << "opacity: " << faces->opacity() << std::endl;
107 update();
108 }
109 return true;
110 }
111 else
112 return Viewer::key_press_event(key, modifiers);
113}
114
115
116void TutorialTransparency::draw() const {
117 if (!current_model()) {
118 return;
119 }
120
121 std::vector<TrianglesDrawable*> surfaces;
122 for (auto m : models_) {
123 for (auto d : m->renderer()->triangles_drawables()) {
126 }
127 }
128
129 if (method_ != 0)
130 transparency_->draw(surfaces);
131 else
132 Viewer::draw();
133}
134
135
Transparency effect using average color blending.
Definition average_color_blending.h:48
@ DT_TRIANGLES
Triangles drawable (GL_TRIANGLES).
Definition drawable.h:66
Transparency effect using dual depth peeling.
Definition dual_depth_peeling.h:81
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
46
48
49
50int main(int argc, char **argv) {
51
54
55
56 TutorialTransparency viewer(EXAMPLE_TITLE);
57
58
59 if (!viewer.add_model(file_name, true)) {
60 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
61 return EXIT_FAILURE;
62 }
63
64 auto drawable = viewer.current_model()->renderer()->get_triangles_drawable("faces");
65 drawable->set_smooth_shading(true);
66 drawable->set_distinct_back_color(false);
67
68
69 return viewer.run();
70}
71
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39