VertexBuffer.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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::setStaticData(int size, const void* data) {
  52. VertexBuffer::size = size;
  53. bindBuffer();
  54. GL::bufferDataStatic(size, data);
  55. }
  56. void VertexBuffer::setStreamData(int size, const void* data) {
  57. VertexBuffer::size = size;
  58. bindBuffer();
  59. GL::bufferDataStream(size, data);
  60. }
  61. void VertexBuffer::setDynamicData(int size, const void* data) {
  62. VertexBuffer::size = size;
  63. bindBuffer();
  64. GL::bufferDataDynamic(size, data);
  65. }
  66. void VertexBuffer::updateData(int offset, int size, const void* data) {
  67. bindBuffer();
  68. GL::bufferSubData(offset, size, data);
  69. }
  70. void VertexBuffer::draw(int vertices, int offset) {
  71. bindArray();
  72. GL::drawTriangles(offset, vertices);
  73. }
  74. void VertexBuffer::drawStrip(int vertices, int offset) {
  75. bindArray();
  76. GL::drawTriangleStrip(offset, vertices);
  77. }
  78. void VertexBuffer::drawPoints(int vertices, int offset) {
  79. bindArray();
  80. GL::drawPoints(offset, vertices);
  81. }
  82. int VertexBuffer::getSize() const {
  83. return size;
  84. }