Distributed under the LGPL
*/
-#include <GL/gl.h>
+#include "extension.h"
+#include "gl.h"
+#include "version_1_2.h"
#include "vertexarray.h"
#include "vertexbuffer.h"
void VertexArray::use_vertex_buffer()
{
- if(vbuf) return;
+ if(vbuf && own_vbuf)
+ return;
vbuf=new VertexBuffer();
own_vbuf=true;
+
update_data();
}
void VertexArray::use_vertex_buffer(VertexBuffer *b)
{
- if(vbuf) return;
-
+ if(own_vbuf)
+ delete vbuf;
vbuf=b;
+ own_vbuf=false;
+
update_data();
}
+void VertexArray::reserve(unsigned n)
+{
+ data.reserve(n*stride);
+}
+
void VertexArray::clear()
{
data.clear();
stride=get_stride(format);
}
-RefPtr<VertexArrayBuilder> VertexArray::modify()
-{
- return new VertexArrayBuilder(*this, data);
-}
-
void VertexArray::apply() const
{
- if(vbuf) vbuf->bind();
+ if(format==NODATA)
+ throw InvalidState("Trying to apply a vertex apply of format NODATA");
+
+ if(vbuf)
+ vbuf->bind();
const float *base=vbuf?0:&data[0];
uint offset=0;
uint found=0;
+ uint bpv=stride*sizeof(float);
for(uint fmt=format; fmt; fmt>>=4)
{
- uint size=(fmt&3)+1;
+ uint sz=(fmt&3)+1;
switch(fmt&12)
{
case 0:
- glVertexPointer(size, GL_FLOAT, stride, base+offset);
+ glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
break;
case 4:
- glNormalPointer(GL_FLOAT, stride, base+offset);
+ glNormalPointer(GL_FLOAT, bpv, base+offset);
break;
case 8:
- glTexCoordPointer(size, GL_FLOAT, stride, base+offset);
+ glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
break;
case 12:
- if(size==1)
- glColorPointer(4, GL_UNSIGNED_BYTE, stride, base+offset);
+ if(sz==1)
+ glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
else
- glColorPointer(size, GL_FLOAT, stride, base+offset);
+ glColorPointer(sz, GL_FLOAT, bpv, base+offset);
break;
}
found|=1<<((fmt&12)>>2);
- offset+=size;
+ offset+=sz;
}
set_array(GL_VERTEX_ARRAY, found&1, 1);
set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
set_array(GL_COLOR_ARRAY, found&8, 8);
- VertexBuffer::unbind();
+ if(vbuf)
+ VertexBuffer::unbind();
}
/**
void VertexArray::update_data()
{
if(vbuf)
+ {
vbuf->data(data.size()*sizeof(float), &data[0]);
+ VertexBuffer::unbind();
+ }
+}
+
+float *VertexArray::append()
+{
+ data.insert(data.end(), stride, 0.0f);
+ return &*data.end()-stride;
}
void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
VertexArray::Loader::Loader(VertexArray &a):
- VertexArrayBuilder(a, a.data)
+ VertexArrayBuilder(a)
{
add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
}
+
+void array_element(int i)
+{
+ glArrayElement(i);
+}
+
+void draw_arrays(PrimitiveType mode, int first, sizei count)
+{
+ glDrawArrays(mode, first, count);
+}
+
+void draw_elements(PrimitiveType mode, sizei count, DataType type, const void *indices)
+{
+ glDrawElements(mode, count, type, indices);
+}
+
+void draw_range_elements(PrimitiveType mode, uint low, uint high, sizei count, DataType type, const void *indices)
+{
+ static RequireVersion _ver(1, 2);
+ glDrawRangeElements(mode, low, high, count, type, indices);
+}
+
} // namespace GL
} // namespace Msp