2 #include "vertexarray.h"
3 #include "vertexbuffer.h"
10 uint get_stride(VertexFormat f)
13 for(uint fmt=f; fmt; fmt>>=4)
15 return stride*sizeof(float);
19 VertexArrayBuilder::VertexArrayBuilder(VertexArray &a, vector<float> &d):
22 format(array.get_format()),
23 stride(get_stride(format)),
24 cr(1), cg(1), cb(1), ca(1),
25 ts(0), tt(0), tr(0), tq(0),
29 void VertexArrayBuilder::vertex(float x, float y, float z, float w)
31 for(uint fmt=format; fmt; fmt>>=4)
39 if(size>=3) data.push_back(z);
40 if(size>=4) data.push_back(w);
49 if(size>=2) data.push_back(tt);
50 if(size>=3) data.push_back(tr);
51 if(size>=4) data.push_back(tq);
56 union { ubyte c[4]; float f; } u;
57 u.c[0]=(ubyte)(cr*255);
58 u.c[1]=(ubyte)(cg*255);
59 u.c[2]=(ubyte)(cb*255);
60 u.c[3]=(ubyte)(ca*255);
68 if(size>=4) data.push_back(ca);
73 //cout<<"Added vertex with "<<data.size()-old_size<<" floats\n";
76 VertexArrayBuilder::~VertexArrayBuilder()
82 VertexArray::VertexArray(VertexFormat f):
84 stride(get_stride(f)),
88 // Reverse the format so the first item is in lowest bits
89 for(uint fmt=f; fmt; fmt>>=4)
90 format=(format, static_cast<VertexFormat>(fmt&15));
93 VertexArray::~VertexArray()
99 void VertexArray::use_vertex_buffer()
103 vbuf=new VertexBuffer();
108 void VertexArray::use_vertex_buffer(VertexBuffer *b)
116 void VertexArray::clear()
121 void VertexArray::apply() const
123 if(vbuf) vbuf->bind();
125 const float *base=vbuf?0:&data[0];
128 for(uint fmt=format; fmt; fmt>>=4)
134 glVertexPointer(size, GL_FLOAT, stride, base+offset);
137 glNormalPointer(GL_FLOAT, stride, base+offset);
140 glTexCoordPointer(size, GL_FLOAT, stride, base+offset);
144 glColorPointer(4, GL_UNSIGNED_BYTE, stride, base+offset);
146 glColorPointer(size, GL_FLOAT, stride, base+offset);
149 found|=1<<((fmt&12)>>2);
153 set_array(GL_VERTEX_ARRAY, found&1, 1);
154 set_array(GL_NORMAL_ARRAY, found&2, 2);
155 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
156 set_array(GL_COLOR_ARRAY, found&8, 8);
158 VertexBuffer::unbind();
162 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
164 void VertexArray::update_data()
167 vbuf->data(data.size()*sizeof(float), &data[0]);
170 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
172 if((enabled_arrays&mask) && !bit)
174 glDisableClientState(array);
175 enabled_arrays&=~mask;
177 else if(!(enabled_arrays&mask) && bit)
179 glEnableClientState(array);
180 enabled_arrays|=mask;
184 unsigned VertexArray::enabled_arrays=0;