]> git.tdb.fi Git - libs/gl.git/blob - source/core/mesh.h
Rearrange soucre files into subdirectories
[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<VertexComponent> &);
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
73 public:
74         Mesh(ResourceManager * = 0);
75         Mesh(const VertexFormat &, ResourceManager * = 0);
76 private:
77         void init(ResourceManager *);
78 public:
79         ~Mesh();
80
81         void clear();
82 private:
83         void check_buffers(unsigned);
84
85 public:
86         const VertexArray &get_vertices() const { return vertices; }
87         const VertexSetup &get_vertex_setup() const { return vtx_setup; }
88         const Buffer *get_index_buffer() const { return ibuf; }
89         unsigned get_n_vertices() const;
90         float *modify_vertex(unsigned);
91
92         void add_batch(const Batch &b);
93         const std::vector<Batch> &get_batches() const { return batches; }
94
95         void set_winding(const WindingTest *);
96
97         void draw(Renderer &) const;
98         void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
99 private:
100         void draw(Renderer &, const VertexSetup *, unsigned) const;
101         void resize_buffers() const;
102
103 public:
104         virtual int get_load_priority() const { return 1; }
105         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
106         virtual UInt64 get_data_size() const;
107         virtual void unload();
108 };
109
110 } // namespace GL
111 } // namespace Msp
112
113 #endif