Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_504_Transparency/main.cpp

This example shows how to render a surface mesh with transparency effect using the following techniques

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:45
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182

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
38// \cond
39
40TutorialTransparency::TutorialTransparency(const std::string& title) : Viewer(title) {
41 transparency_ = new DualDepthPeeling(camera());
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 // Not needed: it will be called in the destructor of the base class
54 //Viewer::cleanup();
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:
72 transparency_ = new AverageColorBlending(camera());
73 std::cout << "method: Average Color Blending" << std::endl;
74 break;
75 case 2:
76 transparency_ = new DualDepthPeeling(camera());
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 // make sure it is valid
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 // make sure it is valid
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()) {
124 if (d->type() == Drawable::DT_TRIANGLES)
125 surfaces.push_back(dynamic_cast<TrianglesDrawable*>(d.get()));
126 }
127 }
128
129 if (method_ != 0)
130 transparency_->draw(surfaces);
131 else
132 Viewer::draw();
133}
134
135// \endcond
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 * 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
46
47using namespace easy3d;
48
49
50int main(int argc, char **argv) {
51 // initialize Easy3D.
52 initialize();
53 const std::string file_name = resource::directory() + "/data/torusknot.obj";
54
55 // create the viewer.
56 TutorialTransparency viewer(EXAMPLE_TITLE);
57
58 // Load a mesh model and create a drawable for the faces.
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 // run the viewer
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