GLVertexBuffer::GLVertexBuffer(GraphicBuffer::Access access, GraphicBuffer::Usage usage, uint32 desired_count, const void* initData, const vertex_elements_type& format): GraphicBuffer(access, usage), mFormat(format), mMaped(false) { glGenBuffers(1, &mId); glBindBuffer(GL_ARRAY_BUFFER, mId); if(initData) { CHECK_GL_CALL(glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(desired_count * GetVertexElementsTotalSize(format)), static_cast<const GLvoid*>(initData), _usage_to_gl(usage))); } else { CHECK_GL_CALL(glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(desired_count * GetVertexElementsTotalSize(format)), 0, _usage_to_gl(usage))); } glBindBuffer(GL_ARRAY_BUFFER, 0); mCount = desired_count; }
void GLVertexBuffer::resize(uint32 desired_count) { glBindBuffer(GL_ARRAY_BUFFER, mId); CHECK_GL_CALL(glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(desired_count * GetVertexElementsTotalSize(mFormat)), 0, usage() == Dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW)); mCount = desired_count; glBindBuffer(GL_ARRAY_BUFFER, 0); }
VertexMapper::VertexMapper(const GraphicBufferPtr& _buffer, const vertex_elements_type& _elements): buffer(_buffer), elements(_elements) { if(buffer) data = buffer->map(); else log_error(L"VertexMapper: invalid graphic buffer ptr"); if(!data) log_error(L"VertexMapper:: failed to map vertex data"); element_size = GetVertexElementsTotalSize(elements); }
void GLVertexBuffer::copyBuffer(const GraphicBufferPtr& to) { if(to) { GLVertexBuffer* dest = checked_cast<GLVertexBuffer*>(to.get()); if(glCopyBufferSubData) { glBindBuffer(GL_COPY_READ_BUFFER, this->getGLBuffer()); glBindBuffer(GL_COPY_WRITE_BUFFER, dest->getGLBuffer()); CHECK_GL_CALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, this->count() * GetVertexElementsTotalSize(this->format()))); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); } else { log_error(L"GLVertexBuffer::copyBuffer: GL Copy Buffer Sub Data not supported"); } } }