3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "version_1_2.h"
11 #include "vertexarray.h"
12 #include "vertexbuffer.h"
19 VertexArray::VertexArray(VertexFormat f):
21 stride(get_stride(f)),
25 // Reverse the format so the first item is in lowest bits. This makes handling in bind() easier.
26 for(uint fmt=f; fmt; fmt>>=4)
27 format=(format, static_cast<VertexFormat>(fmt&15));
30 VertexArray::~VertexArray()
36 void VertexArray::use_vertex_buffer()
41 vbuf=new VertexBuffer();
47 void VertexArray::use_vertex_buffer(VertexBuffer *b)
57 void VertexArray::reserve(unsigned n)
59 data.reserve(n*stride);
62 void VertexArray::clear()
67 void VertexArray::reset(VertexFormat f)
71 for(uint fmt=f; fmt; fmt>>=4)
72 format=(format, static_cast<VertexFormat>(fmt&15));
73 stride=get_stride(format);
76 void VertexArray::apply() const
79 throw InvalidState("Trying to apply a vertex apply of format NODATA");
84 const float *base=vbuf?0:&data[0];
87 uint bpv=stride*sizeof(float);
88 for(uint fmt=format; fmt; fmt>>=4)
94 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
97 glNormalPointer(GL_FLOAT, bpv, base+offset);
100 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
104 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
106 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
109 found|=1<<((fmt&12)>>2);
113 set_array(GL_VERTEX_ARRAY, found&1, 1);
114 set_array(GL_NORMAL_ARRAY, found&2, 2);
115 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
116 set_array(GL_COLOR_ARRAY, found&8, 8);
119 VertexBuffer::unbind();
123 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
125 void VertexArray::update_data()
129 vbuf->data(data.size()*sizeof(float), &data[0]);
130 VertexBuffer::unbind();
134 float *VertexArray::append()
136 data.insert(data.end(), stride, 0.0f);
137 return &*data.end()-stride;
140 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
142 if((enabled_arrays&mask) && !bit)
144 glDisableClientState(array);
145 enabled_arrays&=~mask;
147 else if(!(enabled_arrays&mask) && bit)
149 glEnableClientState(array);
150 enabled_arrays|=mask;
154 unsigned VertexArray::enabled_arrays=0;
157 VertexArray::Loader::Loader(VertexArray &a):
158 VertexArrayBuilder(a)
160 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
161 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
162 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
163 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
164 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
165 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
166 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
167 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
168 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
169 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
173 void array_element(int i)
178 void draw_arrays(PrimitiveType mode, int first, sizei count)
180 glDrawArrays(mode, first, count);
183 void draw_elements(PrimitiveType mode, sizei count, DataType type, const void *indices)
185 glDrawElements(mode, count, type, indices);
188 void draw_range_elements(PrimitiveType mode, uint low, uint high, sizei count, DataType type, const void *indices)
190 static RequireVersion _ver(1, 2);
191 glDrawRangeElements(mode, low, high, count, type, indices);