VertexBuffer.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 size, const void* data, GL::BufferUsage usage) {
  52. VertexBuffer::size = size;
  53. bindBuffer();
  54. GL::bufferData(size, data, usage);
  55. }
  56. void VertexBuffer::setData(const Buffer& buffer, GL::BufferUsage usage) {
  57. setData(buffer.getLength(), buffer, usage);
  58. }
  59. void VertexBuffer::updateData(int offset, int size, const void* data) {
  60. bindBuffer();
  61. GL::bufferSubData(offset, size, data);
  62. }
  63. void VertexBuffer::draw(int vertices, int offset) {
  64. bindArray();
  65. GL::drawTriangles(offset, vertices);
  66. }
  67. void VertexBuffer::drawStrip(int vertices, int offset) {
  68. bindArray();
  69. GL::drawTriangleStrip(offset, vertices);
  70. }
  71. void VertexBuffer::drawPoints(int vertices, int offset) {
  72. bindArray();
  73. GL::drawPoints(offset, vertices);
  74. }
  75. int VertexBuffer::getSize() const {
  76. return size;
  77. }