]> git.tdb.fi Git - libs/gl.git/blob - source/core/mesh.h
Set OpenGL debug labels on various objects loaded from Resources
[libs/gl.git] / source / core / mesh.h
1 #ifndef MSP_GL_MESH_H_
2 #define MSP_GL_MESH_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "batch.h"
6 #include "resource.h"
7 #include "vertexarray.h"
8 #include "vertexsetup.h"
9 #include "windingtest.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Buffer;
15 class Renderer;
16
17 /**
18 Raw mesh data, consisting of a VertexArray and one or more Batches.  Though a
19 Mesh can draw itself, it's usually used as part of Renderables rather than on
20 its own.
21 */
22 class Mesh: public Resource
23 {
24         friend class MeshBuilder;
25
26 public:
27         class Loader: public DataFile::ObjectLoader<Mesh>
28         {
29         private:
30                 bool allow_gl_calls;
31
32         public:
33                 Loader(Mesh &, bool = true);
34         private:
35                 void vertices(const std::vector<VertexAttribute> &);
36                 void batch(PrimitiveType);
37                 void winding(FaceWinding);
38         };
39
40 private:
41         class AsyncLoader: public Resource::AsyncLoader
42         {
43         private:
44                 Mesh &mesh;
45                 IO::Seekable &io;
46                 Bufferable::AsyncUpdater *vertex_updater;
47                 Bufferable::AsyncUpdater *index_updater;
48                 unsigned phase;
49
50         public:
51                 AsyncLoader(Mesh &, IO::Seekable &);
52                 ~AsyncLoader();
53
54                 virtual bool needs_sync() const;
55                 virtual bool process();
56         };
57
58         enum BufferMask
59         {
60                 VERTEX_BUFFER = 1,
61                 INDEX_BUFFER = 2
62         };
63
64         VertexArray vertices;
65         std::vector<Batch> batches;
66         Buffer *vbuf;
67         Buffer *ibuf;
68         VertexSetup vtx_setup;
69         mutable unsigned short dirty;
70         bool disallow_rendering;
71         const WindingTest *winding;
72         std::string debug_name;
73
74 public:
75         Mesh(ResourceManager * = 0);
76         Mesh(const VertexFormat &, ResourceManager * = 0);
77 private:
78         void init(ResourceManager *);
79 public:
80         ~Mesh();
81
82         void clear();
83 private:
84         void check_buffers(unsigned);
85
86 public:
87         const VertexArray &get_vertices() const { return vertices; }
88         const VertexSetup &get_vertex_setup() const { return vtx_setup; }
89         const Buffer *get_index_buffer() const { return ibuf; }
90         unsigned get_n_vertices() const;
91         float *modify_vertex(unsigned);
92
93         void add_batch(const Batch &b);
94         const std::vector<Batch> &get_batches() const { return batches; }
95
96         void set_winding(const WindingTest *);
97
98         void draw(Renderer &) const;
99         void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
100 private:
101         void draw(Renderer &, const VertexSetup *, unsigned) const;
102         void resize_buffers() const;
103
104 public:
105         virtual int get_load_priority() const { return 1; }
106         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
107         virtual UInt64 get_data_size() const;
108         virtual void unload();
109
110         void set_debug_name(const std::string &);
111 };
112
113 } // namespace GL
114 } // namespace Msp
115
116 #endif