]> git.tdb.fi Git - libs/gl.git/blob - source/core/mesh.h
98a028dbc88ea8f231c039ce97dcc48834e3c347
[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 Stores mesh data using a VertexArray and one or more Batches.
21
22 Meshes can be created at runtime using the MeshBuilder class.
23
24 The Object class provides a higher-level interface which associates a Mesh with
25 a Technique and is usually the appropriate way to of rendering geometry.
26 */
27 class Mesh: public Resource
28 {
29         friend class MeshBuilder;
30
31 public:
32         class Loader: public DataFile::ObjectLoader<Mesh>
33         {
34         private:
35                 bool allow_gl_calls;
36
37         public:
38                 Loader(Mesh &, bool = true);
39         private:
40                 void storage(const std::vector<VertexAttribute> &);
41                 void vertices();
42                 void vertices_with_format(const std::vector<VertexAttribute> &);
43                 void batch(PrimitiveType);
44         };
45
46 private:
47         class AsyncLoader: public Resource::AsyncLoader
48         {
49         private:
50                 Mesh &mesh;
51                 IO::Seekable &io;
52                 Bufferable::AsyncUpdater *vertex_updater = 0;
53                 Bufferable::AsyncUpdater *index_updater = 0;
54                 unsigned phase = 0;
55
56         public:
57                 AsyncLoader(Mesh &, IO::Seekable &);
58                 ~AsyncLoader();
59
60                 virtual bool needs_sync() const;
61                 virtual bool process();
62         };
63
64         enum BufferMask
65         {
66                 VERTEX_BUFFER = 1,
67                 INDEX_BUFFER = 2
68         };
69
70         VertexArray vertices;
71         std::vector<Batch> batches;
72         Buffer *vbuf = 0;
73         Buffer *ibuf = 0;
74         VertexSetup vtx_setup;
75         mutable unsigned short dirty = 0;
76         bool disallow_rendering = false;
77         FaceWinding face_winding = NON_MANIFOLD;
78         std::string debug_name;
79
80 public:
81         Mesh() = default;
82         Mesh(const VertexFormat &);
83         ~Mesh();
84
85         /** Sets the vertex format for the mesh.  It cannot be changed once set. */
86         void storage(const VertexFormat &);
87
88         /** Clears all vertices and batches.  Vertex format is retained. */
89         void clear();
90 private:
91         void check_buffers(unsigned);
92
93 public:
94         const VertexArray &get_vertices() const { return vertices; }
95         const VertexSetup &get_vertex_setup() const { return vtx_setup; }
96         const Buffer *get_index_buffer() const { return ibuf; }
97         std::size_t get_n_vertices() const;
98
99         /** Returns a pointer to a vertex.  Offsets of individual attributes can be
100         queried from VertexFormat. */
101         char *modify_vertex(std::size_t);
102
103         /** Adds a batch to the mesh.  It may be combined with the last existing
104         batch if the primitive types are compatible. */
105         void add_batch(const Batch &b);
106
107         const std::vector<Batch> &get_batches() const { return batches; }
108
109         void set_winding(FaceWinding);
110
111         void draw(Renderer &) const;
112         
113         /** Draws multiple instances of the mesh.  The supplied VertexSetup must use
114         the mesh's vertex array. */
115         void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
116
117 private:
118         void draw(Renderer &, const VertexSetup *, unsigned) const;
119         void resize_buffers() const;
120
121 public:
122         virtual int get_load_priority() const { return 1; }
123         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
124         virtual std::uint64_t get_data_size() const;
125         virtual void unload();
126
127         void set_debug_name(const std::string &);
128 };
129
130 } // namespace GL
131 } // namespace Msp
132
133 #endif