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