3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include "vertexarray.h"
10 #include "vertexbuffer.h"
17 VertexArray::VertexArray(VertexFormat f):
19 stride(get_stride(f)),
23 // Reverse the format so the first item is in lowest bits. This makes handling in bind() easier.
24 for(uint fmt=f; fmt; fmt>>=4)
25 format=(format, static_cast<VertexFormat>(fmt&15));
28 VertexArray::~VertexArray()
34 void VertexArray::use_vertex_buffer()
39 vbuf=new VertexBuffer();
45 void VertexArray::use_vertex_buffer(VertexBuffer *b)
55 void VertexArray::reserve(unsigned n)
57 data.reserve(n*stride);
60 void VertexArray::clear()
65 void VertexArray::reset(VertexFormat f)
69 for(uint fmt=f; fmt; fmt>>=4)
70 format=(format, static_cast<VertexFormat>(fmt&15));
71 stride=get_stride(format);
74 void VertexArray::apply() const
77 throw InvalidState("Trying to apply a vertex apply of format NODATA");
82 const float *base=vbuf?0:&data[0];
85 uint bpv=stride*sizeof(float);
86 for(uint fmt=format; fmt; fmt>>=4)
92 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
95 glNormalPointer(GL_FLOAT, bpv, base+offset);
98 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
102 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
104 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
107 found|=1<<((fmt&12)>>2);
111 set_array(GL_VERTEX_ARRAY, found&1, 1);
112 set_array(GL_NORMAL_ARRAY, found&2, 2);
113 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
114 set_array(GL_COLOR_ARRAY, found&8, 8);
117 VertexBuffer::unbind();
121 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
123 void VertexArray::update_data()
127 vbuf->data(data.size()*sizeof(float), &data[0]);
128 VertexBuffer::unbind();
132 float *VertexArray::append()
134 data.insert(data.end(), stride, 0.0f);
135 return &*data.end()-stride;
138 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
140 if((enabled_arrays&mask) && !bit)
142 glDisableClientState(array);
143 enabled_arrays&=~mask;
145 else if(!(enabled_arrays&mask) && bit)
147 glEnableClientState(array);
148 enabled_arrays|=mask;
152 unsigned VertexArray::enabled_arrays=0;
155 VertexArray::Loader::Loader(VertexArray &a):
156 VertexArrayBuilder(a)
158 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
159 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
160 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
161 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
162 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
163 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
164 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
165 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
166 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
167 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));