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