]> git.tdb.fi Git - libs/gl.git/blob - source/builders/vertexarraybuilder.cpp
Rearrange vertex attributes
[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 *a=format.begin(); a!=format.end(); ++a)
16         {
17                 unsigned sem = get_attribute_semantic(*a);
18                 unsigned sz = get_attribute_size(*a);
19                 if(sem>=attr.size())
20                         ptr += sz;
21                 else if(*a==COLOR4_UBYTE)
22                 {
23                         union { unsigned char c[4]; float f; } u;
24                         u.c[0] = static_cast<unsigned char>(attr[sem].x*255);
25                         u.c[1] = static_cast<unsigned char>(attr[sem].y*255);
26                         u.c[2] = static_cast<unsigned char>(attr[sem].z*255);
27                         u.c[3] = static_cast<unsigned char>(attr[sem].w*255);
28                         *ptr++ = u.f;
29                 }
30                 else
31                 {
32                         const Vector4 &v = (sem==0 ? vtx : attr[sem]);
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