]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarraybuilder.cpp
Support multiple sets of texture coordinates in VertexArrays
[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 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                 unsigned sz = (*c&3)+1;
29                 unsigned t = *c>>2;
30                 switch(t)
31                 {
32                 case 0:
33                         *ptr++ = x;
34                         *ptr++ = y;
35                         if(sz>=3) *ptr++ = z;
36                         if(sz>=4) *ptr++ = w;
37                         break;
38                 case 1:
39                         *ptr++ = nor.x;
40                         *ptr++ = nor.y;
41                         *ptr++ = nor.z;
42                         break;
43                 case 2:
44                         if(sz==1)
45                         {
46                                 union { unsigned char c[4]; float f; } u;
47                                 u.c[0] = static_cast<unsigned char>(col.r*255);
48                                 u.c[1] = static_cast<unsigned char>(col.g*255);
49                                 u.c[2] = static_cast<unsigned char>(col.b*255);
50                                 u.c[3] = static_cast<unsigned char>(col.a*255);
51                                 *ptr++ = u.f;
52                         }
53                         else
54                         {
55                                 *ptr++ = col.r;
56                                 *ptr++ = col.g;
57                                 *ptr++ = col.b;
58                                 if(sz>=4) *ptr++ = col.a;
59                         }
60                         break;
61                 default:
62                         const Vector4 &a = (t<11 ? texc[t-3] : attr[t-11]);
63                         *ptr++ = a.x;
64                         if(sz>=2) *ptr++ = a.y;
65                         if(sz>=3) *ptr++ = a.z;
66                         if(sz>=4) *ptr++ = a.w;
67                         break;
68                 }
69         }
70 }
71
72 } // namespace GL
73 } // namespace Msp