This example shows how to create depth images from the rendering.
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_DEPTH_MAP_H
28#define EASY3D_TUTORIAL_DEPTH_MAP_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33
34
36 class FramebufferObject;
37}
38
39
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
An implementation of framebuffer object (FBO).
Definition framebuffer_object.h:122
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
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
41
42
43
44TutorialDepthMap::TutorialDepthMap(const std::string& 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
58
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;
75 }
76 if (!program)
77 return;
78
79 fbo_->bind();
80 fbo_->deactivate_draw_buffers();
81 glClear(GL_DEPTH_BUFFER_BIT);
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 }
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
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);
119}
120
121
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
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/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
48
49
50int main(int argc, char **argv) {
51
53
54 TutorialDepthMap viewer(EXAMPLE_TITLE);
55
56
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
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