StreamBuffer.cpp 859 B

12345678910111213141516171819202122232425262728
  1. #include <GL/glew.h>
  2. #include <cassert>
  3. #include "rendering/wrapper/StreamBuffer.h"
  4. StreamBuffer::StreamBuffer(u64 size) : bufferSize(size), offset(size), index(0), buffer(nullptr) {
  5. }
  6. void StreamBuffer::reset(u64 size) {
  7. if(offset + size >= bufferSize) {
  8. offset = 0;
  9. glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
  10. }
  11. buffer = static_cast<float*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
  12. assert(buffer != nullptr);
  13. index = 0;
  14. }
  15. StreamBuffer& StreamBuffer::add(float f) {
  16. buffer[index++] = f;
  17. return *this;
  18. }
  19. void StreamBuffer::draw(uint floatPerVertex) {
  20. glUnmapBuffer(GL_ARRAY_BUFFER);
  21. glDrawArrays(GL_TRIANGLE_STRIP, offset / (sizeof (float) * floatPerVertex), index / floatPerVertex);
  22. offset += index * sizeof(float);
  23. }