X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fvertexarray.cpp;h=c166e98718338c55bcfb175dde856d7c44e41c74;hb=16f6f15328b3a6eec87b1b5e5822368966d44a38;hp=a2d3936c35f9532b3cc73087b24f9a5e19fe8a55;hpb=f71aee8c20ff85e4857e4dfad0c20ce606ea3717;p=libs%2Fgl.git diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index a2d3936c..c166e987 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -5,7 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ -#include +#include "gl.h" #include "vertexarray.h" #include "vertexbuffer.h" @@ -33,21 +33,30 @@ VertexArray::~VertexArray() 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(); @@ -62,11 +71,6 @@ void VertexArray::reset(VertexFormat f) stride=get_stride(format); } -RefPtr VertexArray::modify() -{ - return new VertexArrayBuilder(*this, data); -} - void VertexArray::apply() const { if(format==NODATA) @@ -78,29 +82,30 @@ void VertexArray::apply() const 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); @@ -108,7 +113,8 @@ void VertexArray::apply() const set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4); set_array(GL_COLOR_ARRAY, found&8, 8); - VertexBuffer::unbind(); + if(vbuf) + VertexBuffer::unbind(); } /** @@ -117,7 +123,16 @@ Updates the VertexArray data to the VertexBuffer tied to the array, if any. 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 @@ -138,7 +153,7 @@ unsigned VertexArray::enabled_arrays=0; VertexArray::Loader::Loader(VertexArray &a): - VertexArrayBuilder(a, a.data) + VertexArrayBuilder(a) { add("vertex2", static_cast(&Loader::vertex)); add("vertex3", static_cast(&Loader::vertex));