Easy3D 2.5.3
Tutorial_506_DepthMaps

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/model.h>
29#include <easy3d/renderer/drawable_points.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 create depth images from the rendering.
38
39int main(int argc, char **argv) {
40 // initialize Easy3D.
41 initialize();
42
43 DepthImage viewer(EXAMPLE_TITLE);
44
45 // the point cloud file.
46 const std::string file_name = resource::directory() + "/data/fountain/pointcloud.ply";
47 Model *model = viewer.add_model(file_name, true);
48 if (!model) {
49 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
50 return EXIT_FAILURE;
51 }
52
53 auto drawable = model->renderer()->get_points_drawable("vertices");
54 drawable->set_point_size(5);
55
56 // run the viewer
57 return viewer.run();
58}
59
The base class of renderable 3D models.
Definition: model.h:49
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
PointsDrawable * get_points_drawable(const std::string &name) const
Definition: renderer.cpp:286
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_DEPTH_IMAGE_H
28#define EASY3D_TUTORIAL_DEPTH_IMAGE_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This DepthImage class visualizes 3D models as depth images.
34
35namespace easy3d {
36 class FramebufferObject;
37}
38
39
40class DepthImage : public easy3d::Viewer
41{
42public:
43 explicit DepthImage(const std::string& title = "");
44 ~DepthImage() override;
45
46protected:
47 void draw() const override;
48
49 void generate_depth();
50 void draw_depth() const;
51
52private:
54};
55
56
57#endif // EASY3D_TUTORIAL_DEPTH_IMAGE_H
An implementation of framebuffer object (FBO).
Definition: framebuffer_object.h:122
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
29#include <easy3d/core/model.h>
30#include <easy3d/renderer/camera.h>
31#include <easy3d/renderer/drawable_points.h>
32#include <easy3d/renderer/drawable_triangles.h>
33#include <easy3d/renderer/framebuffer_object.h>
34#include <easy3d/renderer/shader_manager.h>
35#include <easy3d/renderer/shader_program.h>
36#include <easy3d/renderer/shape.h>
37#include <easy3d/renderer/renderer.h>
38
39
40using namespace easy3d;
41
42
43DepthImage::DepthImage(const std::string& title)
44 : Viewer(title)
45 , fbo_(nullptr)
46{
47 camera()->setUpVector(vec3(0, 1, 0));
48 camera()->setViewDirection(vec3(0, 0, -1));
49 camera_->showEntireScene();
50}
51
52
53DepthImage::~DepthImage() {
54 delete fbo_;
55
56 // Not needed: it will be called in the destructor of the base class
57 //Viewer::cleanup();
58}
59
60
61void DepthImage::draw() const {
62 draw_depth();
63 Viewer::draw();
64}
65
66
67void DepthImage::generate_depth() {
68 static const std::string name = "shadow/shadow_generate";
69 ShaderProgram* program = ShaderManager::get_program(name);
70 if (!program) {
71 std::vector<ShaderProgram::Attribute> attributes;
72 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::POSITION, "vtx_position"));
73 program = ShaderManager::create_program_from_files(name, attributes);
74 }
75 if (!program)
76 return;
77
78 fbo_->bind();
79 fbo_->deactivate_draw_buffers();
80 glClear(GL_DEPTH_BUFFER_BIT);
81 program->bind();
82 program->set_uniform("MVP", camera()->modelViewProjectionMatrix());
83 for (auto m : models_) {
84 for (auto d : m->renderer()->points_drawables()) {
85 if (d->is_visible()) {
86 glPointSize(d->point_size());
87 d->gl_draw();
88 }
89 }
90 for (auto d : m->renderer()->triangles_drawables()) {
91 if (d->is_visible())
92 d->gl_draw();
93 }
94 }
95 program->release();
96 fbo_->release();
97}
98
99
100void DepthImage::draw_depth() const {
101 auto viewer = const_cast<DepthImage*>(this);
102 auto w = static_cast<float>(width()) * dpi_scaling();
103 auto h = static_cast<float>(height()) * dpi_scaling();
104
105 if (!fbo_) {
106 const int samples = 0;
107 viewer->fbo_ = new FramebufferObject(static_cast<int>(w), static_cast<int>(h), samples);
108 viewer->fbo_->add_depth_texture(GL_DEPTH_COMPONENT32F, GL_LINEAR, GL_COMPARE_REF_TO_TEXTURE, GL_LEQUAL);
109 }
110 fbo_->ensure_size(static_cast<int>(w), static_cast<int>(h));
111
112 // generate
113 viewer->generate_depth();
114
115 const Rect quad(20 * dpi_scaling(), 20 * dpi_scaling() + w/4, 40 * dpi_scaling(), 40 * dpi_scaling() + h/4);
116 shape::draw_depth_texture(quad, fbo_->depth_texture(), static_cast<int>(w), static_cast<int>(h), -0.9f);
117 shape::draw_quad_wire(quad, vec4(1.0f, 0.0f, 0.0f, 1.0f), static_cast<int>(w), static_cast<int>(h), -0.99f);
118}
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42
OpenGL Shader Compilation.
Definition: shader_program.h:78
void bind() const
Starts using the program.
Definition: shader_program.cpp:678
void release() const
Ends using the program.
Definition: shader_program.cpp:689