X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fbuffer.cpp;h=51f4873f76274d2f37305ba8bbc3045eb00406a6;hb=f9b2b3330ad7234721ad3f971a7cbc0226a017d7;hp=b4b71c3a0130efadcaea894f0fd921b9e833629b;hpb=76e338af116120d93d082ad247591ec9adad9233;p=libs%2Fgl.git diff --git a/source/buffer.cpp b/source/buffer.cpp index b4b71c3a..51f4873f 100644 --- a/source/buffer.cpp +++ b/source/buffer.cpp @@ -28,42 +28,44 @@ Buffer::~Buffer() glDeleteBuffersARB(1, &id); } -void Buffer::bind(BufferType t) const -{ - glBindBufferARB(t, id); - binding(t)=this; -} - -void Buffer::maybe_bind() const -{ - if(binding(type)!=this) - bind(); -} - void Buffer::set_usage(BufferUsage u) { - usage=u; + usage = u; } void Buffer::data(unsigned size, const void *d) { - maybe_bind(); + const Buffer *old = current(type); + bind(); glBufferDataARB(type, size, d, usage); + restore(old, type); } void Buffer::sub_data(unsigned offset, unsigned size, const void *d) { - maybe_bind(); + const Buffer *old = current(type); + bind(); glBufferSubDataARB(type, offset, size, d); + restore(old, type); +} + +void Buffer::bind_to(BufferType t) const +{ + const Buffer *&ptr = binding(t); + if(ptr!=this) + { + glBindBufferARB(t, id); + ptr = this; + } } -void Buffer::unbind(BufferType type) +void Buffer::unbind_from(BufferType type) { - const Buffer *&ptr=binding(type); + const Buffer *&ptr = binding(type); if(ptr) { glBindBufferARB(type, 0); - ptr=0; + ptr = 0; } } @@ -79,7 +81,18 @@ const Buffer *&Buffer::binding(BufferType type) } } -const Buffer *Buffer::bound[4]={ 0, 0, 0, 0 }; +void Buffer::restore(const Buffer *buf, BufferType type) +{ + if(buf!=current(type)) + { + if(buf) + buf->bind_to(type); + else + unbind_from(type); + } +} + +const Buffer *Buffer::bound[4] = { 0, 0, 0, 0 }; } // namespace GL } // namespace Msp