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