1 #include "arb_multitexture.h"
2 #include "arb_vertex_shader.h"
6 #include "vertexarray.h"
13 VertexArray::VertexArray(const VertexFormat &f):
20 VertexArray::~VertexArray()
23 void VertexArray::reset(const VertexFormat &f)
27 stride = get_stride(format);
31 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
33 unsigned slot = get_array_slot(*c);
34 if(slot>=arrays.size())
35 arrays.resize(slot+1);
40 unsigned VertexArray::get_array_slot(unsigned char comp)
42 unsigned t = get_component_type(comp);
43 if(t==get_component_type(VERTEX3))
45 else if(t==get_component_type(NORMAL3))
47 else if(t==get_component_type(COLOR4_FLOAT))
49 else if(comp>=TEXCOORD1 && comp<=TEXCOORD4+12)
51 t -= get_component_type(TEXCOORD1);
53 static Require _req(ARB_multitexture);
58 static Require _req(ARB_vertex_shader);
60 t -= get_component_type(ATTRIB1);
65 void VertexArray::use_vertex_buffer()
70 vbuf = new Buffer(ARRAY_BUFFER);
75 void VertexArray::use_vertex_buffer(Buffer *b)
83 void VertexArray::clear()
88 void VertexArray::reserve(unsigned n)
90 data.reserve(n*stride);
93 float *VertexArray::append()
95 data.insert(data.end(), stride, 0.0f);
97 return &*(data.end()-stride);
100 float *VertexArray::modify(unsigned i)
103 return &data[0]+i*stride;
106 void VertexArray::set_dirty()
111 vbuf = new Buffer(ARRAY_BUFFER);
116 void VertexArray::apply() const
119 throw invalid_operation("VertexArray::apply");
121 const VertexArray *old = current();
122 if(!set_current(this))
127 vbuf->bind_to(ARRAY_BUFFER);
130 vbuf->data(data.size()*sizeof(float), &data[0]);
135 const float *base = (vbuf ? 0 : &data[0]);
137 unsigned stride_bytes = stride*sizeof(float);
138 unsigned active_tex = 0;
139 unsigned n_arrays = arrays.size();
141 n_arrays = max(n_arrays, old->arrays.size());
142 for(unsigned i=0; i<n_arrays; ++i)
144 unsigned char arr = (i<arrays.size() ? arrays[i] : 0);
145 unsigned char old_arr = (old && i<old->arrays.size() ? old->arrays[i] : 0);
149 unsigned char comp = (arr ? arr : old_arr);
150 unsigned sz = get_component_size(comp);
151 unsigned t = get_component_type(comp);
152 GLenum array_type = 0;
153 if(t==get_component_type(VERTEX3))
156 glVertexPointer(sz, GL_FLOAT, stride_bytes, base+offset);
157 array_type = GL_VERTEX_ARRAY;
159 else if(t==get_component_type(NORMAL3))
162 glNormalPointer(GL_FLOAT, stride_bytes, base+offset);
163 array_type = GL_NORMAL_ARRAY;
165 else if(t==get_component_type(COLOR4_FLOAT))
170 glColorPointer(4, GL_UNSIGNED_BYTE, stride_bytes, base+offset);
172 glColorPointer(sz, GL_FLOAT, stride_bytes, base+offset);
174 array_type = GL_COLOR_ARRAY;
176 else if(comp>=TEXCOORD1 && comp<=TEXCOORD4+12)
178 t -= get_component_type(TEXCOORD1);
179 if(t>0 || active_tex)
181 glClientActiveTexture(GL_TEXTURE0+t);
185 glTexCoordPointer(sz, GL_FLOAT, stride_bytes, base+offset);
186 array_type = GL_TEXTURE_COORD_ARRAY;
190 if(t>=get_component_type(ATTRIB1))
191 t -= get_component_type(ATTRIB1);
193 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride_bytes, base+offset);
196 // Only change enable state if needed
200 glEnableClientState(array_type);
202 glEnableVertexAttribArray(t);
204 else if(old_arr && !arr)
207 glDisableClientState(array_type);
209 glDisableVertexAttribArray(t);
217 glClientActiveTexture(GL_TEXTURE0);
220 Buffer::unbind_from(ARRAY_BUFFER);
224 VertexArray::Loader::Loader(VertexArray &a):
225 VertexArrayBuilder(a)
227 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
228 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
229 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
230 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
231 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
232 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
233 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
234 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
235 add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
236 add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
237 add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
238 add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
239 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
240 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
241 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
242 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
243 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
244 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
245 add("tangent3", static_cast<void (Loader::*)(float, float, float)>(&Loader::tangent));
246 add("binormal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::binormal));