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

This example shows how to create depth images from the rendering.

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_MAP_H
28#define EASY3D_TUTORIAL_DEPTH_MAP_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33// This DepthMap class visualizes 3D models as depth images.
34
35namespace easy3d {
36 class FramebufferObject;
37}
38
39
40class TutorialDepthMap : public easy3d::Viewer
41{
42public:
43 explicit TutorialDepthMap(const std::string& title = "");
44 ~TutorialDepthMap() 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_MAP_H
An implementation of framebuffer object (FBO).
Definition framebuffer_object.h:122
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
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// \cond
43
44TutorialDepthMap::TutorialDepthMap(const std::string& title)
45 : Viewer(title)
46 , fbo_(nullptr)
47{
48 camera()->setUpVector(vec3(0, 1, 0));
49 camera()->setViewDirection(vec3(0, 0, -1));
50 camera_->showEntireScene();
51}
52
53
54TutorialDepthMap::~TutorialDepthMap() {
55 delete fbo_;
56
57 // Not needed: it will be called in the destructor of the base class
58 //Viewer::cleanup();
59}
60
61
62void TutorialDepthMap::draw() const {
63 draw_depth();
64 Viewer::draw();
65}
66
67
68void TutorialDepthMap::generate_depth() {
69 static const std::string name = "shadow/shadow_generate";
71 if (!program) {
72 std::vector<ShaderProgram::Attribute> attributes;
73 attributes.emplace_back(ShaderProgram::Attribute(ShaderProgram::POSITION, "vtx_position"));
74 program = ShaderManager::create_program_from_files(name, attributes);
75 }
76 if (!program)
77 return;
78
79 fbo_->bind();
80 fbo_->deactivate_draw_buffers();
81 glClear(GL_DEPTH_BUFFER_BIT);
82 program->bind();
83 program->set_uniform("MVP", camera()->modelViewProjectionMatrix());
84 for (auto m : models_) {
85 for (auto d : m->renderer()->points_drawables()) {
86 if (d->is_visible()) {
87 glPointSize(d->point_size());
88 d->gl_draw();
89 }
90 }
91 for (auto d : m->renderer()->triangles_drawables()) {
92 if (d->is_visible())
93 d->gl_draw();
94 }
95 }
96 program->release();
97 fbo_->release();
98}
99
100
101void TutorialDepthMap::draw_depth() const {
102 auto viewer = const_cast<TutorialDepthMap*>(this);
103 auto w = static_cast<float>(width()) * dpi_scaling();
104 auto h = static_cast<float>(height()) * dpi_scaling();
105
106 if (!fbo_) {
107 const int samples = 0;
108 viewer->fbo_ = new FramebufferObject(static_cast<int>(w), static_cast<int>(h), samples);
109 viewer->fbo_->add_depth_texture(GL_DEPTH_COMPONENT32F, GL_LINEAR, GL_COMPARE_REF_TO_TEXTURE, GL_LEQUAL);
110 }
111 fbo_->ensure_size(static_cast<int>(w), static_cast<int>(h));
112
113 // generate
114 viewer->generate_depth();
115
116 const Rect quad(20 * dpi_scaling(), 20 * dpi_scaling() + w/4, 40 * dpi_scaling(), 40 * dpi_scaling() + h/4);
117 shape::draw_depth_texture(quad, fbo_->depth_texture(), static_cast<int>(w), static_cast<int>(h), -0.9f);
118 shape::draw_quad_wire(quad, vec4(1.0f, 0.0f, 0.0f, 1.0f), static_cast<int>(w), static_cast<int>(h), -0.99f);
119}
120
121// \endcond
static ShaderProgram * create_program_from_files(const std::string &file_base_name, const std::vector< ShaderProgram::Attribute > &attributes=std::vector< ShaderProgram::Attribute >(), const std::vector< std::string > &outputs=std::vector< std::string >(), bool geom_shader=false)
Create a shader program from shader source files specified by the shader file's base name.
Definition shader_manager.cpp:49
static ShaderProgram * get_program(const std::string &shader_name)
Get the shader program if it exists and is working.
Definition shader_manager.cpp:41
OpenGL Shader Compilation.
Definition shader_program.h:75
void bind() const
Start using the program.
Definition shader_program.cpp:678
ShaderProgram * set_uniform(const std::string &name, const void *value)
Set the uniform to value.
Definition shader_program.cpp:436
void release() const
End using the program.
Definition shader_program.cpp:689
@ POSITION
Position.
Definition shader_program.h:79
std::pair< AttribType, std::string > Attribute
Attribute: a pair of attribute type and attribute name.
Definition shader_program.h:85
void draw_quad_wire(const Rect &rect, const vec4 &color, int width, int height, float depth)
Draws a wire quad defined in the screen space.
Definition shape.cpp:42
void draw_depth_texture(const Rect &rect, unsigned int texture, int width, int height, float depth)
Draws a quad visualizing a depth texture in a region.
Definition shape.cpp:269
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.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/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
45
46
47using namespace easy3d;
48
49
50int main(int argc, char **argv) {
51 // initialize Easy3D.
52 initialize();
53
54 TutorialDepthMap viewer(EXAMPLE_TITLE);
55
56 // the point cloud file.
57 const std::string file_name = resource::directory() + "/data/fountain/pointcloud.ply";
58 auto model = viewer.add_model(file_name, true);
59 if (!model) {
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 = model->renderer()->get_points_drawable("vertices");
65 drawable->set_point_size(5);
66
67 // run the viewer
68 return viewer.run();
69}
70
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