]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarraybuilder.cpp
Add class MeshBuilder
[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         data(a.data),
16         array(a)
17 { }
18
19 VertexArrayBuilder::~VertexArrayBuilder()
20 {
21         array.update_data();
22 }
23
24 void VertexArrayBuilder::vertex_(float x, float y, float z, float w)
25 {
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                         data.push_back(x);
33                         data.push_back(y);
34                         if(size>=3) data.push_back(z);
35                         if(size>=4) data.push_back(w);
36                         break;
37                 case 4:
38                         data.push_back(nx);
39                         data.push_back(ny);
40                         data.push_back(nz);
41                         break;
42                 case 8:
43                         data.push_back(ts);
44                         if(size>=2) data.push_back(tt);
45                         if(size>=3) data.push_back(tr);
46                         if(size>=4) data.push_back(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                                 data.push_back(u.f);
57                         }
58                         else
59                         {
60                                 data.push_back(cr);
61                                 data.push_back(cg);
62                                 data.push_back(cb);
63                                 if(size>=4) data.push_back(ca);
64                         }
65                         break;
66                 }
67         }
68 }
69
70 } // namespace GL
71 } // namespace Msp