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