VertexBuffer.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "rendering/VertexBuffer.h"
  2. VertexBuffer::Attributes& VertexBuffer::Attributes::addFloat(int count) {
  3. attributes.add(GL::Attribute::newFloat(count));
  4. return *this;
  5. }
  6. VertexBuffer::Attributes& VertexBuffer::Attributes::addColor4() {
  7. attributes.add(GL::Attribute::newColor(4));
  8. return *this;
  9. }
  10. VertexBuffer::Attributes& VertexBuffer::Attributes::addSpacer() {
  11. attributes.add(GL::Attribute::newDummy());
  12. return *this;
  13. }
  14. void VertexBuffer::Attributes::set() const {
  15. int size = 0;
  16. for(const GL::Attribute& a : attributes) {
  17. size += a.getSize();
  18. }
  19. int index = 0;
  20. int offset = 0;
  21. for(const GL::Attribute& a : attributes) {
  22. if(!a.isDummy()) {
  23. GL::vertexAttribPointer(index, a, size, offset);
  24. }
  25. offset += a.getSize();
  26. index++;
  27. }
  28. }
  29. VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0), size(0) {
  30. }
  31. VertexBuffer::~VertexBuffer() {
  32. GL::deleteBuffer(vertexBuffer);
  33. GL::deleteVertexArray(vertexArray);
  34. }
  35. void VertexBuffer::bindArray() const {
  36. GL::bindVertexArray(vertexArray);
  37. }
  38. void VertexBuffer::bindBuffer() const {
  39. GL::bindBuffer(vertexBuffer);
  40. }
  41. void VertexBuffer::init(const Attributes& attributes) {
  42. if(vertexArray != 0 || vertexBuffer != 0) {
  43. return;
  44. }
  45. vertexArray = GL::genVertexArray();
  46. vertexBuffer = GL::genBuffer();
  47. bindArray();
  48. bindBuffer();
  49. attributes.set();
  50. }
  51. void VertexBuffer::setData(int dataSize, const void* data,
  52. GL::BufferUsage usage) {
  53. size = dataSize;
  54. bindBuffer();
  55. GL::bufferData(size, data, usage);
  56. }
  57. void VertexBuffer::setData(const Buffer& buffer, GL::BufferUsage usage) {
  58. setData(buffer.getLength(), buffer, usage);
  59. }
  60. void VertexBuffer::updateData(int offset, int dataSize, const void* data) {
  61. bindBuffer();
  62. GL::bufferSubData(offset, dataSize, data);
  63. }
  64. void VertexBuffer::draw(int vertices, int offset) {
  65. bindArray();
  66. GL::drawTriangles(offset, vertices);
  67. }
  68. void VertexBuffer::drawStrip(int vertices, int offset) {
  69. bindArray();
  70. GL::drawTriangleStrip(offset, vertices);
  71. }
  72. void VertexBuffer::drawPoints(int vertices, int offset) {
  73. bindArray();
  74. GL::drawPoints(offset, vertices);
  75. }
  76. int VertexBuffer::getSize() const {
  77. return size;
  78. }