VertexBuffer.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "rendering/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::setFloatAttribute(int index, int length, int offset, int step) {
  11. glVertexAttribPointer(index, length, GL_FLOAT, false, step, static_cast<char*> (0) + offset);
  12. glEnableVertexAttribArray(index);
  13. }
  14. void VertexBuffer::setColorAttribute(int index, int offset, int step) {
  15. glVertexAttribPointer(index, GL_BGRA, GL_UNSIGNED_BYTE, true, step, static_cast<char*> (0) + offset);
  16. glEnableVertexAttribArray(index);
  17. }
  18. void VertexBuffer::setData(int size, const void* data) {
  19. glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
  20. }
  21. void VertexBuffer::bindArray() const {
  22. glBindVertexArray(vertexArray);
  23. }
  24. void VertexBuffer::bindBuffer() const {
  25. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  26. }
  27. void VertexBuffer::bind() const {
  28. bindArray();
  29. bindBuffer();
  30. }
  31. void VertexBuffer::draw(int vertices) const {
  32. glDrawArrays(GL_TRIANGLES, 0, vertices);
  33. }