]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.cpp
Standard vertex components for tangent and binormal vectors
[libs/gl.git] / source / vertexarray.cpp
1 #include "arb_multitexture.h"
2 #include "arb_vertex_shader.h"
3 #include "buffer.h"
4 #include "error.h"
5 #include "gl.h"
6 #include "vertexarray.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 VertexArray::ArrayMask VertexArray::enabled_arrays;
14
15 VertexArray::VertexArray(const VertexFormat &f):
16         defer_vbuf(true),
17         dirty(false)
18 {
19         reset(f);
20 }
21
22 VertexArray::~VertexArray()
23 { }
24
25 void VertexArray::reset(const VertexFormat &f)
26 {
27         clear();
28         format = f;
29         stride = get_stride(format);
30
31         bool has_multitex = false;
32         bool has_gen_attrs = false;
33         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
34         {
35                 if(*c>=TEXCOORD1+4 && *c<=TEXCOORD4+12)
36                         has_multitex = true;
37                 if(*c==TANGENT3 || *c==BINORMAL3 || *c>=ATTRIB1)
38                         has_gen_attrs = true;
39         }
40         if(has_multitex)
41                 static Require _req(ARB_multitexture);
42         if(has_gen_attrs)
43                 static Require _req(ARB_vertex_shader);
44 }
45
46 void VertexArray::use_vertex_buffer()
47 {
48         if(vbuf)
49                 return;
50
51         vbuf = new Buffer(ARRAY_BUFFER);
52         defer_vbuf = false;
53         dirty = true;
54 }
55
56 void VertexArray::use_vertex_buffer(Buffer *b)
57 {
58         vbuf = b;
59         vbuf.keep();
60         defer_vbuf = false;
61         dirty = true;
62 }
63
64 void VertexArray::clear()
65 {
66         data.clear();
67 }
68
69 void VertexArray::reserve(unsigned n)
70 {
71         data.reserve(n*stride);
72 }
73
74 float *VertexArray::append()
75 {
76         data.insert(data.end(), stride, 0.0f);
77         set_dirty();
78         return &*(data.end()-stride);
79 }
80
81 float *VertexArray::modify(unsigned i)
82 {
83         set_dirty();
84         return &data[0]+i*stride;
85 }
86
87 void VertexArray::set_dirty()
88 {
89         dirty = true;
90         if(defer_vbuf)
91         {
92                 vbuf = new Buffer(ARRAY_BUFFER);
93                 defer_vbuf = false;
94         }
95 }
96
97 void VertexArray::apply() const
98 {
99         if(format.empty())
100                 throw invalid_operation("VertexArray::apply");
101
102         if(vbuf)
103         {
104                 vbuf->bind_to(ARRAY_BUFFER);
105                 if(dirty)
106                 {
107                         vbuf->data(data.size()*sizeof(float), &data[0]);
108                         dirty = false;
109                 }
110         }
111
112         const float *base = (vbuf ? 0 : &data[0]);
113         unsigned offset = 0;
114         ArrayMask found;
115         unsigned bpv = stride*sizeof(float);
116         unsigned active_tex = 0;
117         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
118         {
119                 unsigned sz = get_component_size(*c);
120                 unsigned t = get_component_type(*c);
121                 bool en = enabled_arrays.is_set(t);
122                 if(t==get_component_type(VERTEX3))
123                 {
124                         glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
125                         if(!en)
126                                 glEnableClientState(GL_VERTEX_ARRAY);
127                 }
128                 else if(t==get_component_type(NORMAL3))
129                 {
130                         glNormalPointer(GL_FLOAT, bpv, base+offset);
131                         if(!en)
132                                 glEnableClientState(GL_NORMAL_ARRAY);
133                 }
134                 else if(t==get_component_type(COLOR4_FLOAT))
135                 {
136                         if(sz==1)
137                                 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
138                         else
139                                 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
140                         if(!en)
141                                 glEnableClientState(GL_COLOR_ARRAY);
142                 }
143                 else if(*c>=TEXCOORD1 && *c<=TEXCOORD4+12)
144                 {
145                         t -= get_component_type(TEXCOORD1);
146                         if(t>0 || active_tex)
147                         {
148                                 glClientActiveTexture(GL_TEXTURE0+t);
149                                 active_tex = t;
150                         }
151                         glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
152                         if(!en)
153                                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
154                 }
155                 else
156                 {
157                         if(t>=get_component_type(ATTRIB1))
158                                 t -= get_component_type(ATTRIB1);
159                         glVertexAttribPointer(t, sz, GL_FLOAT, false, bpv, base+offset);
160                         if(!en)
161                                 glEnableVertexAttribArray(t);
162                 }
163                 found.set(t);
164                 offset += sz;
165         }
166
167         for(unsigned i=0; i<64; ++i)
168                 if(enabled_arrays.is_set(i) && !found.is_set(i))
169                 {
170                         if(i==get_component_type(VERTEX3))
171                                 glDisableClientState(GL_VERTEX_ARRAY);
172                         else if(i==get_component_type(NORMAL3))
173                                 glDisableClientState(GL_NORMAL_ARRAY);
174                         else if(i==get_component_type(COLOR4_FLOAT))
175                                 glDisableClientState(GL_COLOR_ARRAY);
176                         else if(i>=get_component_type(TEXCOORD1) && i<=get_component_type(TEXCOORD1)+3)
177                         {
178                                 unsigned j = i-get_component_type(TEXCOORD1);
179                                 if(j>0 || active_tex)
180                                         glClientActiveTexture(GL_TEXTURE0+j);
181                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
182                                 active_tex = j;
183                         }
184                         else if(i<get_component_type(ATTRIB1))
185                                 glDisableVertexAttribArray(i);
186                         else
187                                 glDisableVertexAttribArray(i-get_component_type(ATTRIB1));
188                 }
189
190         enabled_arrays = found;
191
192         if(active_tex)
193                 glClientActiveTexture(GL_TEXTURE0);
194
195         if(vbuf)
196                 Buffer::unbind_from(ARRAY_BUFFER);
197 }
198
199
200 VertexArray::ArrayMask::ArrayMask()
201 {
202         for(unsigned i=0; i<N; ++i)
203                 mask[i] = 0;
204 }
205
206 void VertexArray::ArrayMask::set(unsigned bit)
207 {
208         mask[bit/B] |= 1<<(bit%B);
209 }
210
211 bool VertexArray::ArrayMask::is_set(unsigned bit) const
212 {
213         return mask[bit/B]&(1<<(bit%B));
214 }
215
216
217 VertexArray::Loader::Loader(VertexArray &a):
218         VertexArrayBuilder(a)
219 {
220         add("vertex2",   static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
221         add("vertex3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
222         add("vertex4",   static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
223         add("normal3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
224         add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
225         add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
226         add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
227         add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
228         add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
229         add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
230         add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
231         add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
232         add("color3",    static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
233         add("color4",    static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
234         add("attrib1",   static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
235         add("attrib2",   static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
236         add("attrib3",   static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
237         add("attrib4",   static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
238         add("tangent3",  static_cast<void (Loader::*)(float, float, float)>(&Loader::tangent));
239         add("binormal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::binormal));
240 }
241
242 } // namespace GL
243 } // namespace Msp