]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.h
Use RED format for ambient occlusion render target
[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
16 /**
17 Raw mesh data, consisting of a VertexArray and one or more Batches.  Though a
18 Mesh can draw itself, it's usually used as part of Renderables rather than on
19 its own.
20 */
21 class Mesh: public Bindable<Mesh>, public Resource
22 {
23         friend class MeshBuilder;
24
25 public:
26         class Loader: public DataFile::ObjectLoader<Mesh>
27         {
28         public:
29                 Loader(Mesh &);
30         private:
31                 void vertices(const std::vector<VertexComponent> &);
32                 void batch(PrimitiveType);
33                 void winding(FaceWinding);
34         };
35
36 private:
37         class AsyncLoader: public Resource::AsyncLoader
38         {
39         private:
40                 Mesh &mesh;
41                 IO::Seekable &io;
42                 Bufferable::AsyncUpdater *vertex_updater;
43                 Bufferable::AsyncUpdater *index_updater;
44                 unsigned phase;
45
46         public:
47                 AsyncLoader(Mesh &, IO::Seekable &);
48                 ~AsyncLoader();
49
50                 virtual bool needs_sync() const;
51                 virtual bool process();
52         };
53
54         VertexArray vertices;
55         std::vector<Batch> batches;
56         Buffer *vbuf;
57         Buffer *ibuf;
58         unsigned vao_id;
59         bool defer_buffers;
60         mutable bool dirty;
61         bool disallow_rendering;
62         const WindingTest *winding;
63
64 public:
65         Mesh(ResourceManager * = 0);
66         Mesh(const VertexFormat &, ResourceManager * = 0);
67 private:
68         void init(ResourceManager *);
69 public:
70         ~Mesh();
71
72         void clear();
73         void use_buffers(bool);
74 private:
75         void create_buffers();
76         void setup_vao() const;
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