]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.h
db0163e17ef3521ef471109758b9d846c6649782
[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 "windingtest.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Buffer;
14 class Renderer;
15 class VertexSetup;
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         void use_buffers(bool);
75 private:
76         void create_buffers();
77
78 public:
79         const VertexArray &get_vertices() const { return vertices; }
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
92         /** Binds the mesh for rendering.  The vertex array is applied using generic
93         attributes only.  Uses vertex array object if possible. */
94         void bind() const;
95
96         static void unbind();
97
98         virtual int get_load_priority() const { return 1; }
99         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
100         virtual UInt64 get_data_size() const;
101         virtual void unload();
102 };
103
104 } // namespace GL
105 } // namespace Msp
106
107 #endif