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