#include #include "rendering/Renderer.h" static void setFloatAttribute(uint index, uint length, uint offset, uint step) { glVertexAttribPointer(index, length, GL_FLOAT, false, sizeof (float) * step, static_cast (0) + offset); glEnableVertexAttribArray(index); } Renderer::Renderer() : vertexArray(0), vertexBuffer(0), bufferSize(8 * 1024 * 1024), offset(bufferSize), index(0), buffer(nullptr) { glGenVertexArrays(1, &vertexArray); glGenBuffers(1, &vertexBuffer); bind(); setFloatAttribute(0, 2, 0, 5); setFloatAttribute(1, 3, 2, 5); } Renderer::~Renderer() { glDeleteBuffers(1, &vertexBuffer); glDeleteVertexArrays(1, &vertexArray); } void Renderer::setPointSize(float size) { glPointSize(size); } void Renderer::drawPoint(float x, float y, uint color) { bind(); reset(sizeof (float) * 5); add(x).add(y).add(getColorChannel(color, 16)).add(getColorChannel(color, 8)).add(getColorChannel(color, 0)); draw(GL_POINTS); } void Renderer::drawLine(float x, float y, float x2, float y2, uint color) { bind(); reset(sizeof (float) * 10); add(x).add(y).add(getColorChannel(color, 16)).add(getColorChannel(color, 8)).add(getColorChannel(color, 0)); add(x2).add(y2).add(getColorChannel(color, 16)).add(getColorChannel(color, 8)).add(getColorChannel(color, 0)); draw(GL_LINES); } void Renderer::bind() { glBindVertexArray(vertexArray); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); } void Renderer::reset(u64 size) { if(offset + size >= bufferSize) { offset = 0; glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW); } constexpr uint bits = GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT; buffer = static_cast (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, bits)); assert(buffer != nullptr); index = 0; } Renderer& Renderer::add(float f) { buffer[index++] = f; return *this; } void Renderer::draw(uint type) { glUnmapBuffer(GL_ARRAY_BUFFER); glDrawArrays(type, offset / (sizeof (float) * 5), index / 5); offset += index * sizeof (float); } float Renderer::getColorChannel(uint color, uint shifts) const { return ((color >> shifts) & 0xFF) * (1.0f / 255.0f); }