]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarraybuilder.cpp
Add append() method and and operator[] to VertexArray
[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(uint fmt=array.get_format(); fmt; fmt>>=4)
27         {
28                 uint size=(fmt&3)+1;
29                 switch(fmt&12)
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 4:
38                         *+ptr++=nx;
39                         *+ptr++=ny;
40                         *+ptr++=nz;
41                         break;
42                 case 8:
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 12:
49                         if(size==1)
50                         {
51                                 union { ubyte c[4]; float f; } u;
52                                 u.c[0]=(ubyte)(cr*255);
53                                 u.c[1]=(ubyte)(cg*255);
54                                 u.c[2]=(ubyte)(cb*255);
55                                 u.c[3]=(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                 }
67         }
68 }
69
70 } // namespace GL
71 } // namespace Msp