VertexBuffer.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "wrapper/VertexBuffer.h"
  2. VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0) {
  3. glGenVertexArrays(1, &vertexArray);
  4. glGenBuffers(1, &vertexBuffer);
  5. }
  6. VertexBuffer::~VertexBuffer() {
  7. glDeleteBuffers(1, &vertexBuffer);
  8. glDeleteVertexArrays(1, &vertexArray);
  9. }
  10. void VertexBuffer::bindArray() const {
  11. glBindVertexArray(vertexArray);
  12. }
  13. void VertexBuffer::bindBuffer() const {
  14. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  15. }
  16. void VertexBuffer::setAttributes(const Attributes& attributes) {
  17. bindArray();
  18. bindBuffer();
  19. attributes.set();
  20. }
  21. void VertexBuffer::setData(int size, const void* data, int flag) {
  22. bindBuffer();
  23. glBufferData(GL_ARRAY_BUFFER, size, data, flag);
  24. }
  25. void VertexBuffer::setStaticData(int size, const void* data) {
  26. setData(size, data, GL_STATIC_DRAW);
  27. }
  28. void VertexBuffer::setStreamData(int size, const void* data) {
  29. setData(size, data, GL_STREAM_DRAW);
  30. }
  31. void VertexBuffer::setDynamicData(int size, const void* data) {
  32. setData(size, data, GL_DYNAMIC_DRAW);
  33. }
  34. void VertexBuffer::updateData(int offset, int size, const void* data) {
  35. bindBuffer();
  36. glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
  37. }
  38. void VertexBuffer::draw(int vertices, int mode, int offset) {
  39. bindArray();
  40. glDrawArrays(mode, offset, vertices);
  41. }
  42. void VertexBuffer::draw(int vertices, int offset) {
  43. draw(vertices, GL_TRIANGLES, offset);
  44. }
  45. void VertexBuffer::drawStrip(int vertices, int offset) {
  46. draw(vertices, GL_TRIANGLE_STRIP, offset);
  47. }