]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.h
Always use VertexSetup in Mesh
[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 Bindable<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         VertexArray vertices;
56         std::vector<Batch> batches;
57         Buffer *vbuf;
58         Buffer *ibuf;
59         VertexSetup vtx_setup;
60         bool defer_buffers;
61         mutable bool dirty;
62         bool disallow_rendering;
63         const WindingTest *winding;
64
65 public:
66         Mesh(ResourceManager * = 0);
67         Mesh(const VertexFormat &, ResourceManager * = 0);
68 private:
69         void init(ResourceManager *);
70 public:
71         ~Mesh();
72
73         void clear();
74 private:
75         void create_buffers();
76
77 public:
78         const VertexArray &get_vertices() const { return vertices; }
79         const VertexSetup &get_vertex_setup() const { return vtx_setup; }
80         const Buffer *get_index_buffer() const { return ibuf; }
81         unsigned get_n_vertices() const;
82         float *modify_vertex(unsigned);
83
84         void add_batch(const Batch &b);
85         const std::vector<Batch> &get_batches() const { return batches; }
86
87         void set_winding(const WindingTest *);
88
89         void draw() const;
90         void draw(Renderer &) const;
91         void draw_instanced(Renderer &, const VertexSetup &, unsigned) const;
92
93         /** Binds the mesh for rendering.  The vertex array is applied using generic
94         attributes only.  Uses vertex array object if possible. */
95         void bind() const;
96
97         static void unbind();
98
99         virtual int get_load_priority() const { return 1; }
100         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
101         virtual UInt64 get_data_size() const;
102         virtual void unload();
103 };
104
105 } // namespace GL
106 } // namespace Msp
107
108 #endif