]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarraybuilder.cpp
44b677f07061af87aaf1bf62779f709f4a73d9cc
[libs/gl.git] / source / vertexarraybuilder.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 "vertexarray.h"
9 #include "vertexarraybuilder.h"
10
11 namespace Msp {
12 namespace GL {
13
14 VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
15         array(a)
16 { }
17
18 void VertexArrayBuilder::vertex_(const Vector4 &v)
19 {
20         float *ptr = array.append();
21         for(const unsigned char *c=array.get_format().begin(); c!=array.get_format().end(); ++c)
22         {
23                 unsigned sz = (*c&3)+1;
24                 unsigned t = *c>>2;
25                 switch(t)
26                 {
27                 case 0:
28                         *ptr++ = v.x;
29                         *ptr++ = v.y;
30                         if(sz>=3) *ptr++ = v.z;
31                         if(sz>=4) *ptr++ = v.w;
32                         break;
33                 case 1:
34                         *ptr++ = nor.x;
35                         *ptr++ = nor.y;
36                         *ptr++ = nor.z;
37                         break;
38                 case 2:
39                         if(sz==1)
40                         {
41                                 union { unsigned char c[4]; float f; } u;
42                                 u.c[0] = static_cast<unsigned char>(col.r*255);
43                                 u.c[1] = static_cast<unsigned char>(col.g*255);
44                                 u.c[2] = static_cast<unsigned char>(col.b*255);
45                                 u.c[3] = static_cast<unsigned char>(col.a*255);
46                                 *ptr++ = u.f;
47                         }
48                         else
49                         {
50                                 *ptr++ = col.r;
51                                 *ptr++ = col.g;
52                                 *ptr++ = col.b;
53                                 if(sz>=4) *ptr++ = col.a;
54                         }
55                         break;
56                 default:
57                         const Vector4 &a = (t<11 ? texc[t-3] : attr[t-11]);
58                         *ptr++ = a.x;
59                         if(sz>=2) *ptr++ = a.y;
60                         if(sz>=3) *ptr++ = a.z;
61                         if(sz>=4) *ptr++ = a.w;
62                         break;
63                 }
64         }
65 }
66
67 } // namespace GL
68 } // namespace Msp