Easy3D 2.6.1
Loading...
Searching...
No Matches
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 <memory>
33#include <functional>
34
35#include <easy3d/core/types.h>
36#include <easy3d/renderer/state.h>
37
38namespace easy3d {
39
40 class Model;
41 class Camera;
42 class Manipulator;
44
57
58 class Drawable : public State {
59 public:
63 enum Type {
64 DT_POINTS = 0x0000,
65 DT_LINES = 0x0001,
66 DT_TRIANGLES = 0x0004
67 };
68
69 public:
75 explicit Drawable(const std::string &name = "unknown", Model *model = nullptr);
76
80 ~Drawable() override;
81
86 virtual Type type() const = 0;
87
92 const std::string &name() const { return name_; }
97 void set_name(const std::string& n) { name_ = n; }
98
103 Model *model() { return model_; }
108 const Model *model() const { return model_; }
113 void set_model(Model *m) { model_ = m; }
114
119 const Box3 &bounding_box() const;
120
125 State& state() { return *this; }
130 const State& state() const { return *this; }
135 void set_state(const State& s) { state() = s; }
136
141 void buffer_stats(std::ostream &output) const;
142
145
149 unsigned int vertex_buffer() const { return vertex_buffer_; }
154 unsigned int color_buffer() const { return color_buffer_; }
159 unsigned int normal_buffer() const { return normal_buffer_; }
164 unsigned int texcoord_buffer() const { return texcoord_buffer_; }
169 unsigned int element_buffer() const { return element_buffer_; }
170
176 void update_vertex_buffer(const std::vector<vec3> &vertices, bool dynamic = false);
182 void update_color_buffer(const std::vector<vec3> &colors, bool dynamic = false);
188 void update_normal_buffer(const std::vector<vec3> &normals, bool dynamic = false);
194 void update_texcoord_buffer(const std::vector<vec2> &texcoords, bool dynamic = false);
204 void update_element_buffer(const std::vector<unsigned int> &elements);
211 void update_element_buffer(const std::vector< std::vector<unsigned int> > &elements);
212
219
221
225 std::size_t num_vertices() const { return num_vertices_; }
226
229
234 virtual void draw(const Camera *camera) const = 0;
235
241 void gl_draw() const;
242
255 void update();
256
267 void set_update_func(const std::function<void(Model*, Drawable*)>& func) { update_func_ = func; }
268
270
273
286 const Manipulator* manipulator() const;
287
293 void set_manipulator(std::shared_ptr<Manipulator> manip) { manipulator_ = manip; }
294
299 mat4 manipulated_matrix() const;
301
306 VertexArrayObject *vao() { return vao_.get(); }
311 const VertexArrayObject *vao() const { return vao_.get(); }
312
313 protected:
314 // actual update of the rendering buffers are here
315 virtual void update_buffers_internal();
316
317 // clears all buffers
318 void clear();
319
320 protected:
321 std::string name_;
322 Model *model_;
323 Box3 bbox_;
324
325 // vertex array object
326 std::shared_ptr<VertexArrayObject> vao_;
327
328 std::size_t num_vertices_;
329 std::size_t num_indices_;
330
331 bool update_needed_;
332 std::function<void(Model*, Drawable*)> update_func_;
333
334 unsigned int vertex_buffer_;
335 unsigned int color_buffer_;
336 unsigned int normal_buffer_;
337 unsigned int texcoord_buffer_;
338 unsigned int element_buffer_;
339
340 // drawables not attached to a model can also be manipulated
341 std::shared_ptr<Manipulator> manipulator_;
342 };
343
344}
345
346
347#endif // EASY3D_RENDERER_DRAWABLE_H
A perspective or orthographic camera.
Definition camera.h:113
The base class for drawable objects. A drawable represent a set of points, line segments,...
Definition drawable.h:58
unsigned int color_buffer() const
Returns the color buffer ID.
Definition drawable.h:154
void update_color_buffer(const std::vector< vec3 > &colors, bool dynamic=false)
Creates/Updates the color buffer.
Definition drawable.cpp:164
Manipulator * manipulator()
Returns the manipulator attached to this drawable.
Definition drawable.cpp:252
virtual Type type() const =0
Returns the type of the drawable.
State & state()
Returns the state of the drawable.
Definition drawable.h:125
Type
The type of the drawable.
Definition drawable.h:63
@ DT_TRIANGLES
Triangles drawable (GL_TRIANGLES).
Definition drawable.h:66
@ DT_LINES
Lines drawable (GL_LINES).
Definition drawable.h:65
@ DT_POINTS
Points drawable (GL_POINTS).
Definition drawable.h:64
const std::string & name() const
Returns the name of the drawable.
Definition drawable.h:92
Model * model()
Returns the model to which the drawable is attached.
Definition drawable.h:103
void set_update_func(const std::function< void(Model *, Drawable *)> &func)
Sets the update function for the drawable.
Definition drawable.h:267
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates the vertex buffer.
Definition drawable.cpp:139
const Model * model() const
Returns the model to which the drawable is attached (const version).
Definition drawable.h:108
unsigned int normal_buffer() const
Returns the normal buffer ID.
Definition drawable.h:159
void set_manipulator(std::shared_ptr< Manipulator > manip)
Attaches a manipulator to this drawable.
Definition drawable.h:293
unsigned int element_buffer() const
Returns the element buffer ID.
Definition drawable.h:169
void update_texcoord_buffer(const std::vector< vec2 > &texcoords, bool dynamic=false)
Updates the texture coordinate buffer.
Definition drawable.cpp:181
void gl_draw() const
Draws the drawable using OpenGL.
Definition drawable.cpp:221
void set_state(const State &s)
Sets the state of the drawable.
Definition drawable.h:135
VertexArrayObject * vao()
Returns the vertex array object of this drawable.
Definition drawable.h:306
void disable_element_buffer()
Disables the use of the element buffer.
Definition drawable.cpp:112
Drawable(const std::string &name="unknown", Model *model=nullptr)
Constructor that initializes the drawable with a name and an optional model.
Definition drawable.cpp:46
void update_element_buffer(const std::vector< unsigned int > &elements)
Updates the element buffer.
Definition drawable.cpp:190
void set_model(Model *m)
Sets the model to which the drawable is attached.
Definition drawable.h:113
~Drawable() override
Destructor.
Definition drawable.cpp:55
unsigned int vertex_buffer() const
Returns the vertex buffer ID.
Definition drawable.h:149
void buffer_stats(std::ostream &output) const
Prints statistics of the buffers to an output stream.
Definition drawable.cpp:68
void update()
Requests an update of the OpenGL buffers.
Definition drawable.cpp:93
mat4 manipulated_matrix() const
Returns the manipulation matrix.
Definition drawable.cpp:272
const Box3 & bounding_box() const
Returns the bounding box of the drawable.
Definition drawable.cpp:60
const State & state() const
Returns the state of the drawable (const version).
Definition drawable.h:130
std::size_t num_vertices() const
Returns the number of vertices.
Definition drawable.h:225
void update_normal_buffer(const std::vector< vec3 > &normals, bool dynamic=false)
Updates the normal buffer.
Definition drawable.cpp:173
const VertexArrayObject * vao() const
Returns the vertex array object of this drawable (const version).
Definition drawable.h:311
virtual void draw(const Camera *camera) const =0
Draws the drawable.
void set_name(const std::string &n)
Sets the name of the drawable.
Definition drawable.h:97
unsigned int texcoord_buffer() const
Returns the texture coordinate buffer ID.
Definition drawable.h:164
A manipulator is for manipulation of an object.
Definition manipulator.h:58
The base class of renderable 3D models.
Definition model.h:50
State()
Default constructor.
Definition state.cpp:42
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:108
Mat4< float > mat4
A 4 by 4 matrix of float type.
Definition types.h:67