]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.cpp
Include version_1_3.h for multitexturing
[libs/gl.git] / source / vertexarray.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "arb_vertex_program.h"
9 #include "extension.h"
10 #include "gl.h"
11 #include "version_1_2.h"
12 #include "version_1_3.h"
13 #include "vertexarray.h"
14 #include "vertexbuffer.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 VertexArray::VertexArray(const VertexFormat &f):
22         vbuf(0),
23         own_vbuf(false)
24 {
25         reset(f);
26 }
27
28 VertexArray::~VertexArray()
29 {
30         if(own_vbuf)
31                 delete vbuf;
32 }
33
34 void VertexArray::use_vertex_buffer()
35 {
36         if(vbuf && own_vbuf)
37                 return;
38
39         vbuf = new Buffer(ARRAY_BUFFER);
40         own_vbuf = true;
41
42         update_data();
43 }
44
45 void VertexArray::use_vertex_buffer(Buffer *b)
46 {
47         if(own_vbuf)
48                 delete vbuf;
49         vbuf = b;
50         own_vbuf = false;
51
52         update_data();
53 }
54
55 void VertexArray::reserve(unsigned n)
56 {
57         data.reserve(n*stride);
58 }
59
60 void VertexArray::clear()
61 {
62         data.clear();
63 }
64
65 void VertexArray::reset(const VertexFormat &f)
66 {
67         clear();
68         format = f;
69         stride = get_stride(format);
70
71         bool has_multitex = false;
72         bool has_gen_attrs = false;
73         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
74         {
75                 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
76                         has_multitex = true;
77                 if(*c>=ATTRIB1)
78                         has_gen_attrs = true;
79         }
80         if(has_multitex)
81                 static RequireVersion _ver(1, 3);
82         if(has_gen_attrs)
83                 static RequireExtension _ext("GL_ARB_vertex_program");
84 }
85
86 void VertexArray::apply() const
87 {
88         if(format.empty())
89                 throw InvalidState("Trying to apply a vertex array with no data");
90
91         if(vbuf)
92                 vbuf->bind();
93
94         const float *base = vbuf?0:&data[0];
95         unsigned offset = 0;
96         ArrayMask found;
97         unsigned bpv = stride*sizeof(float);
98         unsigned active_tex = 0;
99         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
100         {
101                 unsigned sz = (*c&3)+1;
102                 unsigned t = *c>>2;
103                 bool en = enabled_arrays.is_set(t);
104                 switch(t)
105                 {
106                 case 0:
107                         glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
108                         if(!en)
109                                 glEnableClientState(GL_VERTEX_ARRAY);
110                         break;
111                 case 1:
112                         glNormalPointer(GL_FLOAT, bpv, base+offset);
113                         if(!en)
114                                 glEnableClientState(GL_NORMAL_ARRAY);
115                         break;
116                 case 2:
117                         if(sz==1)
118                                 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
119                         else
120                                 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
121                         if(!en)
122                                 glEnableClientState(GL_COLOR_ARRAY);
123                         break;
124                 default:
125                         if(t<11)
126                         {
127                                 if(t>3 || active_tex)
128                                 {
129                                         glClientActiveTexture(GL_TEXTURE0+(t-3));
130                                         active_tex = t-3;
131                                 }
132                                 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
133                                 if(!en)
134                                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
135                         }
136                         else
137                         {
138                                 glVertexAttribPointerARB(t-11, sz, GL_FLOAT, false, bpv, base+offset);
139                                 if(!en)
140                                         glEnableVertexAttribArrayARB(t-11);
141                         }
142                         break;
143                 }
144                 found.set(t);
145                 offset += sz;
146         }
147
148         for(unsigned i=0; i<64; ++i)
149                 if(enabled_arrays.is_set(i) && !found.is_set(i))
150                 {
151                         if(i==0)
152                                 glDisableClientState(GL_VERTEX_ARRAY);
153                         else if(i==1)
154                                 glDisableClientState(GL_NORMAL_ARRAY);
155                         else if(i==2)
156                                 glDisableClientState(GL_COLOR_ARRAY);
157                         else if(i>=3 && i<11)
158                         {
159                                 if(i>3 || active_tex)
160                                         glClientActiveTexture(GL_TEXTURE0+(i-3));
161                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
162                         }
163                         else
164                                 glDisableVertexAttribArrayARB(i-11);
165                 }
166
167         enabled_arrays = found;
168
169         if(active_tex)
170                 glClientActiveTexture(GL_TEXTURE0);
171
172         if(vbuf)
173                 vbuf->unbind();
174 }
175
176 /**
177 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
178 */
179 void VertexArray::update_data()
180 {
181         if(vbuf)
182         {
183                 vbuf->data(data.size()*sizeof(float), &data[0]);
184                 vbuf->unbind();
185         }
186 }
187
188 float *VertexArray::append()
189 {
190         data.insert(data.end(), stride, 0.0f);
191         return &*data.end()-stride;
192 }
193
194 VertexArray::ArrayMask VertexArray::enabled_arrays;
195
196
197 VertexArray::ArrayMask::ArrayMask()
198 {
199         for(unsigned i=0; i<N; ++i)
200                 mask[i] = 0;
201 }
202
203 void VertexArray::ArrayMask::set(unsigned bit)
204 {
205         mask[bit/B] |= 1<<(bit%B);
206 }
207
208 bool VertexArray::ArrayMask::is_set(unsigned bit) const
209 {
210         return mask[bit/B]&(1<<(bit%B));
211 }
212
213
214 VertexArray::Loader::Loader(VertexArray &a):
215         VertexArrayBuilder(a)
216 {
217         add("vertex2",   static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
218         add("vertex3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
219         add("vertex4",   static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
220         add("normal3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
221         add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
222         add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
223         add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
224         add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
225         add("color3",    static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
226         add("color4",    static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
227         add("attrib1",   static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
228         add("attrib2",   static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
229         add("attrib3",   static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
230         add("attrib4",   static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
231 }
232
233
234 void array_element(int i)
235 {
236         glArrayElement(i);
237 }
238
239 void draw_arrays(PrimitiveType mode, int first, unsigned count)
240 {
241         glDrawArrays(mode, first, count);
242 }
243
244 void draw_elements(PrimitiveType mode, unsigned count, DataType type, const void *indices)
245 {
246         glDrawElements(mode, count, type, indices);
247 }
248
249 void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, DataType type, const void *indices)
250 {
251         static RequireVersion _ver(1, 2);
252         glDrawRangeElements(mode, low, high, count, type, indices);
253 }
254
255 } // namespace GL
256 } // namespace Msp