Browse Source

refactoring

Kajetan Johannes Hammerle 3 years ago
parent
commit
ea2781d793

+ 13 - 71
client/rendering/FontRenderer.cpp

@@ -1,47 +1,19 @@
 #include "client/rendering/FontRenderer.h"
 
-const u64 FontRenderer::BUFFER_LENGTH = 8 * 1024 * 1024;
-
-static void setVertexAttribute(uint index, uint length, uint offset) {
-    glVertexAttribPointer(index, length, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0) + offset);
-    glEnableVertexAttribArray(index);
-}
-
-FontRenderer::FontRenderer() : tex("resources/font8x8.png"), offset(BUFFER_LENGTH), vertexArray(0), vertexBuffer(0) {
-    glGenVertexArrays(1, &vertexArray);
-    glBindVertexArray(vertexArray);
-
-    glGenBuffers(1, &vertexBuffer);
-    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
-
-    setVertexAttribute(0, 2, 0);
-    setVertexAttribute(1, 2, 2);
-    setVertexAttribute(2, 3, 4);
-}
-
-FontRenderer::~FontRenderer() {
-    glDeleteBuffers(1, &vertexBuffer);
-    glDeleteVertexArrays(1, &vertexArray);
+FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024), tex("resources/font8x8.png") {
+    vertexBuffer.bind();
+    vertexBuffer.setFloatAttribute(0, 2, 0, 7);
+    vertexBuffer.setFloatAttribute(1, 2, 2, 7);
+    vertexBuffer.setFloatAttribute(2, 3, 4, 7);
 }
 
 void FontRenderer::drawString(float x, float y, const char* text) {
-    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
+    vertexBuffer.bind();
 
     const u64 maxIndex = 256;
-    const u64 maxLength = maxIndex * 4 * sizeof (float) * 7;
-
-    if(offset + maxLength >= BUFFER_LENGTH) {
-        offset = 0;
-        glBufferData(GL_ARRAY_BUFFER, BUFFER_LENGTH, nullptr, GL_STREAM_DRAW);
-    }
-
-    float* buffer = static_cast<float*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, maxLength, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
-    if(buffer == nullptr) {
-        return;
-    }
+    buffer.reset(maxIndex * 4 * sizeof (float) * 7);
 
     u64 index = 0;
-    u64 i = 0;
     float r = 1.0f;
     float g = 1.0f;
     float b = 1.0f;
@@ -64,45 +36,15 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
         float maxY = minY + (1.0f / 16.0f);
 
-        buffer[i++] = x;
-        buffer[i++] = y;
-        buffer[i++] = minX;
-        buffer[i++] = minY;
-        buffer[i++] = r;
-        buffer[i++] = g;
-        buffer[i++] = b;
-
-        buffer[i++] = x;
-        buffer[i++] = y + 8;
-        buffer[i++] = minX;
-        buffer[i++] = maxY;
-        buffer[i++] = r;
-        buffer[i++] = g;
-        buffer[i++] = b;
-
-        buffer[i++] = x + 6;
-        buffer[i++] = y;
-        buffer[i++] = maxX;
-        buffer[i++] = minY;
-        buffer[i++] = r;
-        buffer[i++] = g;
-        buffer[i++] = b;
-
-        buffer[i++] = x + 6;
-        buffer[i++] = y + 8;
-        buffer[i++] = maxX;
-        buffer[i++] = maxY;
-        buffer[i++] = r;
-        buffer[i++] = g;
-        buffer[i++] = b;
+        buffer.add(x).add(y).add(minX).add(minY).add(r).add(g).add(b);
+        buffer.add(x).add(y + 8).add(minX).add(maxY).add(r).add(g).add(b);
+        buffer.add(x + 6).add(y).add(maxX).add(minY).add(r).add(g).add(b);
+        buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).add(r).add(g).add(b);
 
         x += 6;
         index++;
     }
-
-    glUnmapBuffer(GL_ARRAY_BUFFER);
-    glBindVertexArray(vertexArray);
+    
     tex.bind(0);
-    glDrawArrays(GL_TRIANGLE_STRIP, offset / (sizeof (float) * 7), i / 7);
-    offset += maxLength;
+    buffer.draw(7);
 }

+ 4 - 14
client/rendering/FontRenderer.h

@@ -1,30 +1,20 @@
 #ifndef FONTRENDERER_H
 #define FONTRENDERER_H
 
-#include <GL/glew.h>
-
-#include "common/utils/Types.h"
+#include "client/rendering/wrapper/VertexBuffer.h"
+#include "client/rendering/wrapper/StreamBuffer.h"
 #include "client/rendering/Texture.h"
 
 class FontRenderer final {
 public:
     FontRenderer();
-    ~FontRenderer();
-    FontRenderer(const FontRenderer& other) = delete;
-    FontRenderer(FontRenderer&& other) = delete;
-    FontRenderer& operator=(const FontRenderer& other) = delete;
-    FontRenderer& operator=(FontRenderer&& other) = delete;
 
     void drawString(float x, float y, const char* text);
 
 private:
-    static const u64 BUFFER_LENGTH;
-    
+    VertexBuffer vertexBuffer;
+    StreamBuffer buffer;
     Texture tex;
-    u64 offset;
-
-    GLuint vertexArray;
-    GLuint vertexBuffer;
 };
 
 #endif

+ 10 - 25
client/rendering/Mesh.cpp

@@ -1,25 +1,10 @@
 #include "client/rendering/Mesh.h"
 
-Mesh::Mesh() : vba(0), vbo(0) {
-    glGenVertexArrays(1, &vba);
-    glBindVertexArray(vba);
-
-    glGenBuffers(1, &vbo);
-    glBindBuffer(GL_ARRAY_BUFFER, vbo);
-
-    glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof (VertexData), static_cast<GLfloat*> (0));
-    glEnableVertexAttribArray(0);
-
-    glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof (VertexData), static_cast<GLfloat*> (0) + 3);
-    glEnableVertexAttribArray(1);
-
-    glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof (VertexData), static_cast<GLfloat*> (0) + 5);
-    glEnableVertexAttribArray(2);
-}
-
-Mesh::~Mesh() {
-    glDeleteVertexArrays(1, &vba);
-    glDeleteBuffers(1, &vbo);
+Mesh::Mesh() {
+    vertexBuffer.bind();
+    vertexBuffer.setFloatAttribute(0, 3, 0, 8);
+    vertexBuffer.setFloatAttribute(1, 2, 3, 8);
+    vertexBuffer.setFloatAttribute(2, 3, 5, 8);
 }
 
 void Mesh::add(const VertexData& data) {
@@ -30,12 +15,12 @@ void Mesh::clear() {
     buffer.clear();
 }
 
-void Mesh::build() const {
-    glBindBuffer(GL_ARRAY_BUFFER, vbo);
-    glBufferData(GL_ARRAY_BUFFER, sizeof (VertexData) * buffer.size(), buffer.data(), GL_STATIC_DRAW);
+void Mesh::build() {
+    vertexBuffer.bindBuffer();
+    vertexBuffer.setData(sizeof (VertexData) * buffer.size(), buffer.data());
 }
 
 void Mesh::draw() const {
-    glBindVertexArray(vba);
-    glDrawArrays(GL_TRIANGLES, 0, buffer.size());
+    vertexBuffer.bindArray();
+    vertexBuffer.draw(buffer.size());
 }

+ 4 - 10
client/rendering/Mesh.h

@@ -1,9 +1,10 @@
 #ifndef MESH_H
 #define MESH_H
 
-#include <GL/glew.h>
 #include <vector>
 
+#include "client/rendering/wrapper/VertexBuffer.h"
+
 class Mesh final {
 public:
 
@@ -19,22 +20,15 @@ public:
     };
 
     Mesh();
-    ~Mesh();
 
     void add(const VertexData& data);
 
     void clear();
-    void build() const;
+    void build();
     void draw() const;
 
 private:
-    Mesh(const Mesh& other) = delete;
-    Mesh(Mesh&& other) = delete;
-    Mesh& operator=(const Mesh& other) = delete;
-    Mesh& operator=(Mesh&& other) = delete;
-
-    GLuint vba;
-    GLuint vbo;
+    VertexBuffer vertexBuffer;
     std::vector<VertexData> buffer;
 };
 

+ 0 - 1
client/rendering/NoiseTexture.h

@@ -10,7 +10,6 @@ class NoiseTexture final {
 public:
     NoiseTexture(u32 width, u32 height);
     ~NoiseTexture();
-
     NoiseTexture(const NoiseTexture& other) = delete;
     NoiseTexture(NoiseTexture&& other) = delete;
     NoiseTexture& operator=(const NoiseTexture& other) = delete;

+ 1 - 1
client/rendering/Texture.cpp

@@ -105,7 +105,7 @@ void Texture::initGL() {
     data = nullptr;
 }
 
-void Texture::bind(unsigned int index) const {
+void Texture::bind(uint index) const {
     glActiveTexture(GL_TEXTURE0 + index);
     glBindTexture(GL_TEXTURE_2D, texture);
 }

+ 1 - 2
client/rendering/Texture.h

@@ -11,13 +11,12 @@ class Texture final {
 public:
     Texture(const char* path);
     ~Texture();
-
     Texture(const Texture& other) = delete;
     Texture(Texture&& other) = delete;
     Texture& operator=(const Texture& other) = delete;
     Texture& operator=(Texture&& other) = delete;
 
-    void bind(unsigned int index) const;
+    void bind(uint index) const;
 
 private:
     void load(const char* path);

+ 28 - 0
client/rendering/wrapper/StreamBuffer.cpp

@@ -0,0 +1,28 @@
+#include <GL/glew.h>
+#include <cassert>
+
+#include "client/rendering/wrapper/StreamBuffer.h"
+
+StreamBuffer::StreamBuffer(u64 size) : bufferSize(size), offset(size), index(0), buffer(nullptr) {
+}
+
+void StreamBuffer::reset(u64 size) {
+    if(offset + size >= bufferSize) {
+        offset = 0;
+        glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
+    }
+    buffer = static_cast<float*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
+    assert(buffer != nullptr);
+    index = 0;
+}
+
+StreamBuffer& StreamBuffer::add(float f) {
+    buffer[index++] = f;
+    return *this;
+}
+
+void StreamBuffer::draw(uint floatPerVertex) {
+    glUnmapBuffer(GL_ARRAY_BUFFER);
+    glDrawArrays(GL_TRIANGLE_STRIP, offset / (sizeof (float) * floatPerVertex), index / floatPerVertex);
+    offset += index * sizeof(float);
+}

+ 26 - 0
client/rendering/wrapper/StreamBuffer.h

@@ -0,0 +1,26 @@
+#ifndef STREAMBUFFER_H
+#define STREAMBUFFER_H
+
+#include "common/utils/Types.h"
+
+class StreamBuffer final {
+public:
+    StreamBuffer(u64 size);
+    StreamBuffer(const StreamBuffer& other) = delete;
+    StreamBuffer(StreamBuffer&& other) = delete;
+    StreamBuffer& operator=(const StreamBuffer& other) = delete;
+    StreamBuffer& operator=(StreamBuffer&& other) = delete;
+
+    void reset(u64 size);
+    StreamBuffer& add(float f);
+    void draw(uint floatPerVertex);
+
+private:
+    u64 bufferSize;
+    u64 offset;
+
+    u64 index;
+    float* buffer;
+};
+
+#endif

+ 37 - 0
client/rendering/wrapper/VertexBuffer.cpp

@@ -0,0 +1,37 @@
+#include "VertexBuffer.h"
+
+VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0) {
+    glGenVertexArrays(1, &vertexArray);
+    glGenBuffers(1, &vertexBuffer);
+}
+
+VertexBuffer::~VertexBuffer() {
+    glDeleteBuffers(1, &vertexBuffer);
+    glDeleteVertexArrays(1, &vertexArray);
+}
+
+void VertexBuffer::setFloatAttribute(uint index, uint length, uint offset, uint step) {
+    glVertexAttribPointer(index, length, GL_FLOAT, false, sizeof (float) * step, static_cast<float*> (0) + offset);
+    glEnableVertexAttribArray(index);
+}
+
+void VertexBuffer::setData(u64 size, const void* data) {
+    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
+}
+
+void VertexBuffer::bindArray() const {
+    glBindVertexArray(vertexArray);
+}
+
+void VertexBuffer::bindBuffer() const {
+    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
+}
+
+void VertexBuffer::bind() const {
+    bindArray();
+    bindBuffer();
+}
+
+void VertexBuffer::draw(uint vertices) const {
+    glDrawArrays(GL_TRIANGLES, 0, vertices);
+}

+ 31 - 0
client/rendering/wrapper/VertexBuffer.h

@@ -0,0 +1,31 @@
+#ifndef VERTEXBUFFER_H
+#define VERTEXBUFFER_H
+
+#include <GL/glew.h>
+
+#include "common/utils/Types.h"
+
+class VertexBuffer final {
+public:
+    VertexBuffer();
+    ~VertexBuffer();
+    VertexBuffer(const VertexBuffer& other) = delete;
+    VertexBuffer(VertexBuffer&& other) = delete;
+    VertexBuffer& operator=(const VertexBuffer& other) = delete;
+    VertexBuffer& operator=(VertexBuffer&& other) = delete;
+    
+    void setFloatAttribute(uint index, uint length, uint offset, uint step);
+    void setData(u64 size, const void* data);
+    
+    void bindArray() const;
+    void bindBuffer() const;
+    void bind() const;
+    
+    void draw(uint vertices) const;
+
+private:
+    GLuint vertexArray;
+    GLuint vertexBuffer;
+};
+
+#endif

+ 3 - 0
common/utils/Types.h

@@ -2,6 +2,7 @@
 #define TYPES_H
 
 #include <cstdint>
+#include <cstddef>
 
 typedef unsigned int uint;
 
@@ -15,4 +16,6 @@ typedef int16_t s16;
 typedef int32_t s32;
 typedef int64_t s64;
 
+static_assert(sizeof(u64) == sizeof(size_t), "size_t is not 64 bit");
+
 #endif

+ 1 - 1
meson.build

@@ -4,7 +4,7 @@ sourcesCommon = ['common/block/BlockRegistry.cpp', 'common/block/Block.cpp', 'co
 
 sourcesServer = ['server/GameServer.cpp', 'server/commands/CommandUtils.cpp', 'server/commands/ServerCommands.cpp', 'server/commands/CommandManager.cpp', 'server/network/Server.cpp', 'server/Main.cpp']
 
-sourcesClient = ['client/Main.cpp', 'client/rendering/WindowSize.cpp', 'client/math/Frustum.cpp', 'client/math/Ray.cpp', 'client/rendering/Framebuffers.cpp', 'client/rendering/wrapper/GLFWWrapper.cpp', 'client/rendering/wrapper/Window.cpp', 'client/Engine.cpp', 'client/input/Keys.cpp', 'client/rendering/wrapper/Shader.cpp', 'client/rendering/Shaders.cpp', 'client/Utils.cpp', 'client/rendering/Mesh.cpp', 'client/math/Matrix.cpp', 'client/math/MatrixStack.cpp', 'client/math/Vector.cpp', 'client/math/Camera.cpp', 'client/math/Plane.cpp', 'client/Game.cpp', 'client/input/MouseButtons.cpp', 'client/rendering/Texture.cpp', 'client/rendering/FontRenderer.cpp', 'client/rendering/wrapper/Framebuffer.cpp', 'client/rendering/NoiseTexture.cpp', 'client/utils/Clock.cpp', 'client/input/Control.cpp', 'client/rendering/RenderSettings.cpp']
+sourcesClient = ['client/Main.cpp', 'client/rendering/WindowSize.cpp', 'client/math/Frustum.cpp', 'client/math/Ray.cpp', 'client/rendering/Framebuffers.cpp', 'client/rendering/wrapper/GLFWWrapper.cpp', 'client/rendering/wrapper/Window.cpp', 'client/Engine.cpp', 'client/input/Keys.cpp', 'client/rendering/wrapper/Shader.cpp', 'client/rendering/Shaders.cpp', 'client/Utils.cpp', 'client/rendering/Mesh.cpp', 'client/math/Matrix.cpp', 'client/math/MatrixStack.cpp', 'client/math/Vector.cpp', 'client/math/Camera.cpp', 'client/math/Plane.cpp', 'client/Game.cpp', 'client/input/MouseButtons.cpp', 'client/rendering/Texture.cpp', 'client/rendering/FontRenderer.cpp', 'client/rendering/wrapper/Framebuffer.cpp', 'client/rendering/NoiseTexture.cpp', 'client/utils/Clock.cpp', 'client/input/Control.cpp', 'client/rendering/RenderSettings.cpp', 'client/rendering/wrapper/VertexBuffer.cpp', 'client/rendering/wrapper/StreamBuffer.cpp']
 
 sourcesTest = ['tests/Main.cpp', 'server/commands/CommandUtils.cpp']