Easy3D 2.5.3
Tutorial_504_Transparency

The source file containing the main() function:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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
34
35using namespace easy3d;
36
37// This example shows how to render a surface mesh with transparency effect using the following techniques
38// - Average Color Blending
39// - Dual Depth Peeling
40
41
42int main(int argc, char **argv) {
43 // initialize Easy3D.
44 initialize();
45 const std::string file_name = resource::directory() + "/data/torusknot.obj";
46
47 // create the viewer.
48 TutorialTransparency viewer(EXAMPLE_TITLE);
49
50 // Load a mesh model and create a drawable for the faces.
51 if (!viewer.add_model(file_name, true)) {
52 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
53 return EXIT_FAILURE;
54 }
55
56 auto drawable = viewer.current_model()->renderer()->get_triangles_drawable("faces");
57 drawable->set_smooth_shading(true);
58 drawable->set_distinct_back_color(false);
59
60 // run the viewer
61 return viewer.run();
62}
63
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

The header file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_TUTORIAL_TRANSPARENCY_H
28#define EASY3D_TUTORIAL_TRANSPARENCY_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This class renders a surface mesh with transparency effect using the
34// Average Color Blending technique.
35
36namespace easy3d {
37 class Transparency;
38}
39
40class TutorialTransparency : public easy3d::Viewer
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:
51 easy3d::Transparency* transparency_;
52 int method_; // 0: disabled; 1: ACB; 2: DDP
53};
54
55
56#endif // EASY3D_TUTORIAL_TRANSPARENCY_H
Base class for rendering with transparency.
Definition: transparency.h:44
The built-in Easy3D viewer.
Definition: viewer.h:61

The source file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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
36using namespace easy3d;
37
38TutorialTransparency::TutorialTransparency(const std::string& title) : Viewer(title) {
39 transparency_ = new DualDepthPeeling(camera());
40 std::cout << "method: Dual Depth Peeling" << std::endl;
41 method_ = 2;
42
43 usage_string_ =
44 "------------------------ Transparency usage ------------------------ \n"
45 "Press key 'space' to turn on/off or switch between different transparency techniques\n"
46 "Press 'up/down' to increase/decrease the transparency of the current model\n"
47 "-------------------------------------------------------------------- \n";
48}
49
50
51TutorialTransparency::~TutorialTransparency() {
52 delete transparency_;
53
54 // Not needed: it will be called in the destructor of the base class
55 //Viewer::cleanup();
56}
57
58
59bool TutorialTransparency::key_press_event(int key, int modifiers) {
60 if (key == KEY_SPACE) {
61 if (transparency_) {
62 delete transparency_;
63 transparency_ = nullptr;
64 }
65
66 method_ = (method_ + 1) % 3;
67 switch (method_)
68 {
69 case 0:
70 std::cout << "method: NULL(transparency disabled)" << std::endl;
71 break;
72 case 1:
73 transparency_ = new AverageColorBlending(camera());
74 std::cout << "method: Average Color Blending" << std::endl;
75 break;
76 case 2:
77 transparency_ = new DualDepthPeeling(camera());
78 std::cout << "method: Dual Depth Peeling" << std::endl;
79 break;
80 }
81 update();
82 return true;
83 }
84 else if (key == KEY_DOWN) {
85 auto faces = current_model()->renderer()->get_triangles_drawable("faces");
86 if (faces) {
87 float o = faces->opacity();
88 if (o > 0)
89 faces->set_opacity(o - 0.1f);
90 // make sure it is valid
91 if (faces->opacity() <= 0)
92 faces->set_opacity(0.1f);
93 std::cout << "opacity: " << faces->opacity() << std::endl;
94 update();
95 }
96 return true;
97 }
98 else if (key == KEY_UP) {
99 auto faces = current_model()->renderer()->get_triangles_drawable("faces");
100 if (faces) {
101 float o = faces->opacity();
102 if (o > 0)
103 faces->set_opacity(o + 0.1f);
104 // make sure it is valid
105 if (faces->opacity() >= 1.0f)
106 faces->set_opacity(1.0f);
107 std::cout << "opacity: " << faces->opacity() << std::endl;
108 update();
109 }
110 return true;
111 }
112 else
113 return Viewer::key_press_event(key, modifiers);
114}
115
116
117void TutorialTransparency::draw() const {
118 if (!current_model()) {
119 return;
120 }
121
122 std::vector<TrianglesDrawable*> surfaces;
123 for (auto m : models_) {
124 for (auto d : m->renderer()->triangles_drawables()) {
125 if (d->type() == Drawable::DT_TRIANGLES)
126 surfaces.push_back(dynamic_cast<TrianglesDrawable*>(d));
127 }
128 }
129
130 if (method_ != 0)
131 transparency_->draw(surfaces);
132 else
133 Viewer::draw();
134}
Transparency effect using average color blending.
Definition: average_color_blending.h:49
Transparency effect using dual depth peeling.
Definition: dual_depth_peeling.h:89
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition: drawable_triangles.h:46