]> git.tdb.fi Git - libs/gl.git/blob - source/core/mesh.h
Make VertexFormat capable of storing type information
[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 "cullface.h"
7 #include "resource.h"
8 #include "vertexarray.h"
9 #include "vertexsetup.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 storage(const std::vector<VertexAttribute> &);
36                 void vertices();
37                 void vertices_with_format(const std::vector<VertexAttribute> &);
38                 void batch(PrimitiveType);
39         };
40
41 private:
42         class AsyncLoader: public Resource::AsyncLoader
43         {
44         private:
45                 Mesh &mesh;
46                 IO::Seekable &io;
47                 Bufferable::AsyncUpdater *vertex_updater;
48                 Bufferable::AsyncUpdater *index_updater;
49                 unsigned phase;
50
51         public:
52                 AsyncLoader(Mesh &, IO::Seekable &);
53                 ~AsyncLoader();
54
55                 virtual bool needs_sync() const;
56                 virtual bool process();
57         };
58
59         enum BufferMask
60         {
61                 VERTEX_BUFFER = 1,
62                 INDEX_BUFFER = 2
63         };
64
65         VertexArray vertices;
66         std::vector<Batch> batches;
67         Buffer *vbuf;
68         Buffer *ibuf;
69         VertexSetup vtx_setup;
70         mutable unsigned short dirty;
71         bool disallow_rendering;
72         FaceWinding face_winding;
73         std::string debug_name;
74
75 public:
76         Mesh(ResourceManager * = 0);
77         Mesh(const VertexFormat &, ResourceManager * = 0);
78 private:
79         void init(ResourceManager *);
80 public:
81         ~Mesh();
82
83         void storage(const VertexFormat &);
84
85         void clear();
86 private:
87         void check_buffers(unsigned);
88
89 public:
90         const VertexArray &get_vertices() const { return vertices; }
91         const VertexSetup &get_vertex_setup() const { return vtx_setup; }
92         const Buffer *get_index_buffer() const { return ibuf; }
93         unsigned get_n_vertices() const;
94         char *modify_vertex(unsigned);
95
96         void add_batch(const Batch &b);
97         const std::vector<Batch> &get_batches() const { return batches; }
98
99         void set_winding(FaceWinding);
100
101         void draw(Renderer &) const;
102         void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
103 private:
104         void draw(Renderer &, const VertexSetup *, unsigned) const;
105         void resize_buffers() const;
106
107 public:
108         virtual int get_load_priority() const { return 1; }
109         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
110         virtual UInt64 get_data_size() const;
111         virtual void unload();
112
113         void set_debug_name(const std::string &);
114 };
115
116 } // namespace GL
117 } // namespace Msp
118
119 #endif