]> git.tdb.fi Git - libs/gl.git/blob - source/builders/vertexarraybuilder.cpp
Refactor vertex builders
[libs/gl.git] / source / builders / vertexarraybuilder.cpp
1 #include "vertexarray.h"
2 #include "vertexarraybuilder.h"
3
4 namespace Msp {
5 namespace GL {
6
7 VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
8         array(a)
9 { }
10
11 void VertexArrayBuilder::vertex_(const Vector4 &vtx)
12 {
13         float *ptr = array.append();
14         const VertexFormat &format = array.get_format();
15         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
16         {
17                 unsigned sz = get_component_size(*c);
18                 unsigned t = get_component_type(*c);
19                 if(t>=attr.size())
20                         ptr += sz;
21                 else if(*c==COLOR4_UBYTE)
22                 {
23                         union { unsigned char c[4]; float f; } u;
24                         u.c[0] = static_cast<unsigned char>(attr[t].x*255);
25                         u.c[1] = static_cast<unsigned char>(attr[t].y*255);
26                         u.c[2] = static_cast<unsigned char>(attr[t].z*255);
27                         u.c[3] = static_cast<unsigned char>(attr[t].w*255);
28                         *ptr++ = u.f;
29                 }
30                 else
31                 {
32                         const Vector4 &v = (t==0 ? vtx : attr[t]);
33                         *ptr++ = v.x;
34                         if(sz>=2) *ptr++ = v.y;
35                         if(sz>=3) *ptr++ = v.z;
36                         if(sz>=4) *ptr++ = v.w;
37                 }
38         }
39 }
40
41 } // namespace GL
42 } // namespace Msp