]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarraybuilder.cpp
8e7e2cb5dd4433625747545cea073fee6715e89c
[libs/gl.git] / source / vertexarraybuilder.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  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 VertexArrayBuilder::~VertexArrayBuilder()
19 {
20         array.update_data();
21 }
22
23 void VertexArrayBuilder::vertex_(float x, float y, float z, float w)
24 {
25         float *ptr=array.append();
26         for(const unsigned char *c=array.get_format().begin(); c!=array.get_format().end(); ++c)
27         {
28                 uint size=(*c&3)+1;
29                 switch(*c>>2)
30                 {
31                 case 0:
32                         *ptr++=x;
33                         *ptr++=y;
34                         if(size>=3) *ptr++=z;
35                         if(size>=4) *ptr++=w;
36                         break;
37                 case 1:
38                         *ptr++=nx;
39                         *ptr++=ny;
40                         *ptr++=nz;
41                         break;
42                 case 2:
43                         *ptr++=ts;
44                         if(size>=2) *ptr++=tt;
45                         if(size>=3) *ptr++=tr;
46                         if(size>=4) *ptr++=tq;
47                         break;
48                 case 3:
49                         if(size==1)
50                         {
51                                 union { ubyte c[4]; float f; } u;
52                                 u.c[0]=static_cast<ubyte>(cr*255);
53                                 u.c[1]=static_cast<ubyte>(cg*255);
54                                 u.c[2]=static_cast<ubyte>(cb*255);
55                                 u.c[3]=static_cast<ubyte>(ca*255);
56                                 *ptr++=u.f;
57                         }
58                         else
59                         {
60                                 *ptr++=cr;
61                                 *ptr++=cg;
62                                 *ptr++=cb;
63                                 if(size>=4) *+ptr++=ca;
64                         }
65                         break;
66                 default:
67                         const Attrib &a=av[(*c>>2)-4];
68                         *ptr++=a.x;
69                         if(size>=2) *ptr++=a.y;
70                         if(size>=3) *ptr++=a.z;
71                         if(size>=4) *ptr++=a.w;
72                         break;
73                 }
74         }
75 }
76
77 } // namespace GL
78 } // namespace Msp