Easy3D 2.5.3
drawable.h
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_RENDERER_DRAWABLE_H
28#define EASY3D_RENDERER_DRAWABLE_H
29
30#include <string>
31#include <vector>
32#include <functional>
33
34#include <easy3d/core/types.h>
35#include <easy3d/renderer/state.h>
36
37namespace easy3d {
38
39 class Model;
40 class Camera;
41 class Manipulator;
42 class VertexArrayObject;
43
56 class Drawable : public State {
57 public:
58 // three types of drawables
59 enum Type {
60 DT_POINTS = 0x0000, // == GL_POINTS
61 DT_LINES = 0x0001, // == GL_LINES
62 DT_TRIANGLES = 0x0004 // == GL_TRIANGLES
63 };
64
65 public:
66 // a drawable can be stand-alone or attached to a model
67 explicit Drawable(const std::string &name = "unknown", Model *model = nullptr);
68 ~Drawable() override;
69
71 virtual Type type() const = 0;
72
73 const std::string &name() const { return name_; }
74 void set_name(const std::string& n) { name_ = n; }
75
77 Model *model() { return model_; }
78 const Model *model() const { return model_; }
79 void set_model(Model *m) { model_ = m; }
80
81 const Box3 &bounding_box() const;
82
83 State& state() { return *this; };
84 const State& state() const { return *this; };
85 void set_state(const State& s) { state() = s; };
86
88 void buffer_stats(std::ostream &output) const;
89
92 unsigned int vertex_buffer() const { return vertex_buffer_; }
93 unsigned int color_buffer() const { return color_buffer_; }
94 unsigned int normal_buffer() const { return normal_buffer_; }
95 unsigned int texcoord_buffer() const { return texcoord_buffer_; }
96 unsigned int element_buffer() const { return element_buffer_; }
97
106 void update_vertex_buffer(const std::vector<vec3> &vertices, bool dynamic = false);
107 void update_color_buffer(const std::vector<vec3> &colors, bool dynamic = false);
108 void update_normal_buffer(const std::vector<vec3> &normals, bool dynamic = false);
109 void update_texcoord_buffer(const std::vector<vec2> &texcoords, bool dynamic = false);
110 void update_element_buffer(const std::vector<unsigned int> &elements);
116 void update_element_buffer(const std::vector< std::vector<unsigned int> > &elements);
117
123
125
126 std::size_t num_vertices() const { return num_vertices_; }
127
130
132 virtual void draw(const Camera *camera) const = 0;
133
137 void gl_draw() const;
138
151 void update();
152
162 void set_update_func(const std::function<void(Model*, Drawable*)>& func) { update_func_ = func; }
163
165
168
179 const Manipulator* manipulator() const;
180
185 void set_manipulator(Manipulator* manip) { manipulator_ = manip; }
186
188 mat4 manipulated_matrix() const;
190
192 VertexArrayObject *vao() { return vao_; }
194 const VertexArrayObject *vao() const { return vao_; }
195
196 protected:
197 // actual update of the rendering buffers are here
198 virtual void update_buffers_internal();
199
200 void clear();
201
202 protected:
203 std::string name_;
204 Model *model_;
205 Box3 bbox_;
206
207 // vertex array object
208 VertexArrayObject *vao_;
209
210 std::size_t num_vertices_;
211 std::size_t num_indices_;
212
213 bool update_needed_;
214 std::function<void(Model*, Drawable*)> update_func_;
215
216 unsigned int vertex_buffer_;
217 unsigned int color_buffer_;
218 unsigned int normal_buffer_;
219 unsigned int texcoord_buffer_;
220 unsigned int element_buffer_;
221
222 // drawables not attached to a model can also be manipulated
223 Manipulator* manipulator_; // for manipulation
224 };
225
226}
227
228
229#endif // EASY3D_RENDERER_DRAWABLE_H
A perspective or orthographic camera.
Definition: camera.h:116
The base class for drawable objects. A drawable represent a set of points, line segments,...
Definition: drawable.h:56
Manipulator * manipulator()
Gets the manipulator attached to this drawable.
Definition: drawable.cpp:250
virtual Type type() const =0
Returns the type of the drawable.
Model * model()
the model to which the drawable is attached to (can be NULL).
Definition: drawable.h:77
void set_update_func(const std::function< void(Model *, Drawable *)> &func)
Setups how a drawable updates its rendering buffers.
Definition: drawable.h:162
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates a single buffer.
Definition: drawable.cpp:142
void set_manipulator(Manipulator *manip)
Attaches a manipulator to this model.
Definition: drawable.h:185
void gl_draw() const
Definition: drawable.cpp:224
VertexArrayObject * vao()
Returns the vertex array object of this drawable.
Definition: drawable.h:192
void disable_element_buffer()
Disables the use of the element buffer.
Definition: drawable.cpp:114
void buffer_stats(std::ostream &output) const
print statistics (e.g., num vertices, memory usage) of the buffers to an output stream (e....
Definition: drawable.cpp:70
void update()
Requests an update of the OpenGL buffers.
Definition: drawable.cpp:95
mat4 manipulated_matrix() const
Returns the manipulation matrix.
Definition: drawable.cpp:270
const VertexArrayObject * vao() const
Returns the vertex array object of this drawable.
Definition: drawable.h:194
virtual void draw(const Camera *camera) const =0
The draw method.
A manipulator is for manipulation of an object.
Definition: manipulator.h:62
The base class of renderable 3D models.
Definition: model.h:49
Class representing the rendering state of a drawable.
Definition: state.h:45
A thin wrapper around an OpenGL Vertex Array Object (VAO).
Definition: vertex_array_object.h:52
Definition: collider.cpp:182
GenericBox< 3, float > Box3
A 3D axis-aligned bounding box of float type.
Definition: types.h:109