]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Add vertex arrays and buffers
[libs/gl.git] / source / vertexarray.h
1 #ifndef MSP_GL_VERTEXARRAY_H_
2 #define MSP_GL_VERTEXARRAY_H_
3
4 #include <vector>
5 #include <msp/core/refptr.h>
6 #include "types.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class VertexArray;
12 class VertexBuffer;
13
14 enum VertexFormat
15 {
16         NODATA=0,
17         VERTEX2=1,
18         VERTEX3,
19         VERTEX4,
20         NORMAL3=6,
21         TEXCOORD1=8,
22         TEXCOORD2,
23         TEXCOORD3,
24         TEXCOORD4,
25         COLOR4_UBYTE=12,
26         COLOR3_FLOAT=14,
27         COLOR4_FLOAT,
28 };
29
30 inline VertexFormat operator,(VertexFormat a, VertexFormat b) { return VertexFormat((a<<4)|b); }
31 uint get_stride(VertexFormat);
32
33 class VertexArrayBuilder
34 {
35 public:
36         std::vector<float> &data;
37
38         VertexArrayBuilder(VertexArray &, std::vector<float> &);
39         void vertex(float x, float y)                     { vertex(x, y, 0, 1); }
40         void vertex(float x, float y, float z)            { vertex(x, y, z, 1); }
41         void vertex(float, float, float, float);
42         void normal(float x, float y, float z)            { nx=x; ny=y; nz=z; }
43         void texcoord(float s)                            { texcoord(s, 0, 0, 1); }
44         void texcoord(float s, float t)                   { texcoord(s, t, 0, 1); }
45         void texcoord(float s, float t, float r)          { texcoord(s, t, r, 1); }
46         void texcoord(float s, float t, float r, float q) { ts=s; tt=t; tr=r; tq=q; }
47         void color(ubyte r, ubyte g, ubyte b)             { color(r, g, b, 255); }
48         void color(ubyte r, ubyte g, ubyte b, ubyte a)    { color(r/255.f, g/255.f, b/255.f, a/255.f); }
49         void color(float r, float g, float b)             { color(r, g, b, 1); }
50         void color(float r, float g, float b, float a)    { cr=r; cg=g; cb=b; ca=a; }
51         ~VertexArrayBuilder();
52 private:
53         VertexArray  &array;
54         VertexFormat format;
55         uint         stride;
56
57         float cr, cg, cb, ca;  // Color
58         float ts, tt, tr, tq;  // TexCoord
59         float nx, ny, nz;     // Normal
60 };
61
62 class VertexArray
63 {
64 public:
65         VertexArray(VertexFormat);
66         ~VertexArray();
67
68         VertexFormat get_format() const { return format; }
69         const std::vector<float> &get_data() const { return data; }
70         void         use_vertex_buffer();
71         void         use_vertex_buffer(VertexBuffer *);
72         void         clear();
73         RefPtr<VertexArrayBuilder> modify() { return new VertexArrayBuilder(*this, data); }
74         void         apply() const;
75         void         update_data();
76 private:
77         VertexFormat format;
78         std::vector<float> data;
79         uint         stride;
80         VertexBuffer *vbuf;
81         bool         own_vbuf;
82
83         void set_array(unsigned, unsigned, unsigned) const;
84
85         static unsigned enabled_arrays;
86 };
87
88 } // namespace GL
89 } // namespace Msp
90
91 #endif