12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include "rendering/VertexBuffer.h"
- VertexBuffer::Attributes& VertexBuffer::Attributes::addFloat(int count) {
- attributes.add(GL::Attribute::newFloat(count));
- return *this;
- }
- VertexBuffer::Attributes& VertexBuffer::Attributes::addColor4() {
- attributes.add(GL::Attribute::newColor(4));
- return *this;
- }
- VertexBuffer::Attributes& VertexBuffer::Attributes::addSpacer() {
- attributes.add(GL::Attribute::newDummy());
- return *this;
- }
- void VertexBuffer::Attributes::set() const {
- int size = 0;
- for(const GL::Attribute& a : attributes) {
- size += a.getSize();
- }
- int index = 0;
- int offset = 0;
- for(const GL::Attribute& a : attributes) {
- if(!a.isDummy()) {
- GL::vertexAttribPointer(index, a, size, offset);
- }
- offset += a.getSize();
- index++;
- }
- }
- VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0), size(0) {
- }
- VertexBuffer::~VertexBuffer() {
- GL::deleteBuffer(vertexBuffer);
- GL::deleteVertexArray(vertexArray);
- }
- void VertexBuffer::bindArray() const {
- GL::bindVertexArray(vertexArray);
- }
- void VertexBuffer::bindBuffer() const {
- GL::bindBuffer(vertexBuffer);
- }
- void VertexBuffer::init(const Attributes& attributes) {
- if(vertexArray != 0 || vertexBuffer != 0) {
- return;
- }
- vertexArray = GL::genVertexArray();
- vertexBuffer = GL::genBuffer();
- bindArray();
- bindBuffer();
- attributes.set();
- }
- void VertexBuffer::setData(int dataSize, const void* data,
- GL::BufferUsage usage) {
- size = dataSize;
- bindBuffer();
- GL::bufferData(size, data, usage);
- }
- void VertexBuffer::setData(const Buffer& buffer, GL::BufferUsage usage) {
- setData(buffer.getLength(), buffer, usage);
- }
- void VertexBuffer::updateData(int offset, int dataSize, const void* data) {
- bindBuffer();
- GL::bufferSubData(offset, dataSize, data);
- }
- void VertexBuffer::draw(int vertices, int offset) {
- bindArray();
- GL::drawTriangles(offset, vertices);
- }
- void VertexBuffer::drawStrip(int vertices, int offset) {
- bindArray();
- GL::drawTriangleStrip(offset, vertices);
- }
- void VertexBuffer::drawPoints(int vertices, int offset) {
- bindArray();
- GL::drawPoints(offset, vertices);
- }
- int VertexBuffer::getSize() const {
- return size;
- }
|