Browse Source

class cleanup of single use classes

Kajetan Johannes Hammerle 2 years ago
parent
commit
aec46492fc

+ 15 - 26
client/World.cpp

@@ -108,7 +108,8 @@ static bool isAir(int x, int y, int z) {
     return blocks.get(x, y, z) == 0;
 }
 
-static void addCube(TypedBuffer<Triangle>& buffer, float x, float y, float z) {
+static void addCube(TypedBuffer<Mesh::Triangle>& buffer, float x, float y,
+                    float z) {
     Vector3 v000(x, y, z);
     Vector3 v001(x, y, z + 1);
     Vector3 v010(x, y + 1, z);
@@ -125,46 +126,34 @@ static void addCube(TypedBuffer<Triangle>& buffer, float x, float y, float z) {
 
     if(isAir(x, y - 1, z)) {
         Vector2 tb(0.125f, 0.0625f);
-        buffer.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)),
-                            Vertex(v100, t1), Vertex(v001, tb)));
-        buffer.add(
-            Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
+        buffer.add({{v000, {0.125f, 0.0f}}, {v100, t1}, {v001, tb}});
+        buffer.add({{v100, t1}, {v101, t4}, {v001, tb}});
     }
     if(isAir(x, y + 1, z)) {
         Vector2 tt(0.3125f, 0.0f);
-        buffer.add(
-            Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
-        buffer.add(Triangle(Vertex(v110, tt), Vertex(v011, t3),
-                            Vertex(v111, Vector2(0.3125f, 0.0625f))));
+        buffer.add({{v010, t2}, {v011, t3}, {v110, tt}});
+        buffer.add({{v110, tt}, {v011, t3}, {v111, {0.3125f, 0.0625f}}});
     }
     if(isAir(x - 1, y, z)) {
-        buffer.add(
-            Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
-        buffer.add(
-            Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
+        buffer.add({{v000, t4}, {v001, t3}, {v010, t1}});
+        buffer.add({{v001, t3}, {v011, t2}, {v010, t1}});
     }
     if(isAir(x + 1, y, z)) {
-        buffer.add(
-            Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
-        buffer.add(
-            Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, t1)));
+        buffer.add({{v100, t3}, {v110, t2}, {v101, t4}});
+        buffer.add({{v101, t4}, {v110, t2}, {v111, t1}});
     }
     if(isAir(x, y, z + 1)) {
-        buffer.add(
-            Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
-        buffer.add(
-            Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
+        buffer.add({{v001, t4}, {v101, t3}, {v011, t1}});
+        buffer.add({{v111, t2}, {v011, t1}, {v101, t3}});
     }
     if(isAir(x, y, z - 1)) {
-        buffer.add(
-            Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
-        buffer.add(
-            Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
+        buffer.add({{v000, t3}, {v010, t2}, {v100, t4}});
+        buffer.add({{v110, t1}, {v100, t4}, {v010, t2}});
     }
 }
 
 static void rebuildRenderData() {
-    TypedBuffer<Triangle> buffer(100);
+    TypedBuffer<Mesh::Triangle> buffer(100);
     for(int x = 0; x < blocks.getSize(); x++) {
         for(int y = 0; y < blocks.getHeight(); y++) {
             for(int z = 0; z < blocks.getSize(); z++) {

+ 128 - 57
client/rendering/Engine.cpp

@@ -1,24 +1,31 @@
 #include "client/rendering/Engine.h"
 #include "client/Game.h"
-#include "client/rendering/Framebuffers.h"
 #include "client/rendering/Mesh.h"
-#include "client/rendering/NoiseTexture.h"
-#include "client/rendering/Shaders.h"
 #include "math/Frustum.h"
+#include "rendering/Framebuffer.h"
 #include "rendering/Window.h"
 #include "utils/Logger.h"
+#include "utils/Random.h"
 #include "wrapper/GL.h"
 
 static Window window;
-static Shaders shaders;
-static Framebuffers framebuffers;
+static Shader worldShader;
+static Shader ssaoShader;
+static Shader ssaoBlurShader;
+static Shader shadowShader;
+static Shader postWorldShader;
+static Shader overlayShader;
+static Framebuffer<5> worldBuffer;
+static Framebuffer<1> ssaoBuffer;
+static Framebuffer<1> ssaoBlurBuffer;
+static Framebuffer<1> shadowBuffer;
 static Size lastSize{0, 0};
 static Frustum frustum{60.0f, 0.1f, 1000.0f, window.getSize()};
 static MatrixStack<16> model;
 Renderer Engine::renderer;
 ShaderMatrix Engine::matrix{nullptr, model, nullptr};
 float Engine::lag = 0.0f;
-static NoiseTexture ssaoNoise;
+static Texture ssaoNoise;
 static Mesh rectangle;
 static Matrix worldProj;
 static Matrix worldView;
@@ -31,23 +38,86 @@ static float shadowRadius = 0.01f;
 static float shadowBias = 0.0002f;
 static bool running = true;
 
+static Error compileShader(Shader& s, const char* name) {
+    constexpr const char* prefix = "resources/shader/";
+    return s.compile(StringBuffer<50>(prefix).append(name).append(".vs"),
+                     StringBuffer<50>(prefix).append(name).append(".fs"));
+}
+
+static Error initShaders() {
+    Error error = compileShader(worldShader, "world");
+    if(error.has()) {
+        return error;
+    }
+    error = compileShader(ssaoShader, "ssao");
+    if(error.has()) {
+        return error;
+    }
+    error = compileShader(ssaoBlurShader, "ssaoBlur");
+    if(error.has()) {
+        return error;
+    }
+    error = compileShader(shadowShader, "worldShadow");
+    if(error.has()) {
+        return error;
+    }
+    error = compileShader(postWorldShader, "worldPost");
+    if(error.has()) {
+        return error;
+    }
+    return compileShader(overlayShader, "overlay");
+}
+
+static void resizeFramebuffers(const Size& size) {
+    worldBuffer.resize(size);
+    ssaoBuffer.resize(size);
+    ssaoBlurBuffer.resize(size);
+    shadowBuffer.resize(size);
+}
+
+static Error initFramebuffers(const Size& size) {
+    Error error = worldBuffer.init(
+        size, TextureFormat::float32(3), TextureFormat::float32(3),
+        TextureFormat::color8(4), TextureFormat::float32(1),
+        TextureFormat::depth32(true));
+    if(error.has()) {
+        return error;
+    }
+    error = ssaoBuffer.init(size, TextureFormat::float32(1));
+    if(error.has()) {
+        return error;
+    }
+    error = ssaoBlurBuffer.init(size, TextureFormat::float32(1));
+    if(error.has()) {
+        return error;
+    }
+    return shadowBuffer.init(size, TextureFormat::depth32());
+}
+
 static bool initRectangle() {
     if(rectangle.init()) {
         return true;
     }
-    TypedBuffer<Triangle> buffer(2);
-    buffer.add(
-        Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
-                 Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
-                 Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
-    buffer.add(
-        Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
-                 Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
-                 Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
-    rectangle.build(buffer);
+    rectangle.build(TypedBuffer<Mesh::Triangle>(2)
+                        .add({{{-1.0f, -1.0f, +0.0f}, {0.0f, 0.0f}},
+                              {{+1.0f, +1.0f, +0.0f}, {1.0f, 1.0f}},
+                              {{-1.0f, +1.0f, +0.0f}, {0.0f, 1.0f}}})
+                        .add({{{-1.0f, -1.0f, +0.0f}, {0.0f, 0.0f}},
+                              {{+1.0f, -1.0f, +0.0f}, {1.0f, 0.0f}},
+                              {{+1.0f, +1.0f, +0.0f}, {1.0f, 1.0f}}}));
     return false;
 }
 
+static void initNoise() {
+    ssaoNoise.init(TextureFormat::float32(3), 0);
+    Random r(1);
+    Array<float, 48> data;
+    for(int i = 0; i < 48; i++) {
+        data[i] = r.nextFloat() * 2.0f - 1.0f;
+    }
+    ssaoNoise.setData(4, 4, data.begin());
+}
+
 bool Engine::init() {
     WindowOptions options(4, 0, {1024, 620}, false, "test");
     Error error = window.open(options);
@@ -56,108 +126,109 @@ bool Engine::init() {
         return true;
     }
     lastSize = window.getSize();
-    error = shaders.init();
+    error = initShaders();
     if(error.has()) {
         LOG_ERROR(error.message);
         return true;
     }
-    error = framebuffers.init(window.getSize());
+    error = initFramebuffers(window.getSize());
     if(error.has()) {
         LOG_ERROR(error.message);
         return true;
     }
-    if(renderer.init() || ssaoNoise.init() || initRectangle()) {
+    initNoise();
+    if(renderer.init() || initRectangle()) {
         return true;
     }
     return false;
 }
 
 static void renderShadow() {
-    framebuffers.shadow.bindAndClear();
+    shadowBuffer.bindAndClear();
     GL::enableDepthTesting();
-    shaders.shadow.use();
+    shadowShader.use();
     worldShadowProjView = worldShadowProj;
     worldShadowProjView *= worldShadowView;
-    shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
+    shadowShader.setMatrix("projView", worldShadowProjView.getValues());
     model.clear();
-    shaders.shadow.setMatrix("model", model.peek().getValues());
-    Engine::matrix = {&shaders.shadow, model, &worldView};
+    shadowShader.setMatrix("model", model.peek().getValues());
+    Engine::matrix = {&shadowShader, model, &worldView};
     Game::renderWorld();
 }
 
 static void renderWorld() {
-    framebuffers.world.bindAndClear();
+    worldBuffer.bindAndClear();
     GL::enableDepthTesting();
-    shaders.world.use();
+    worldShader.use();
 
     Matrix rWorldShadowProjView;
     rWorldShadowProjView.scale(0.5f).translate(Vector3(0.5f, 0.5f, 0.5f));
     rWorldShadowProjView *= worldShadowProjView;
 
-    shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
-    shaders.world.setMatrix("proj", worldProj.getValues());
+    worldShader.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
+    worldShader.setMatrix("proj", worldProj.getValues());
     worldView = Matrix();
-    shaders.world.setMatrix("view", worldView.getValues());
+    worldShader.setMatrix("view", worldView.getValues());
     model.clear();
-    shaders.world.setMatrix("model", model.peek().getValues());
-    framebuffers.shadow.bindTextureTo(0, 1);
-    shaders.world.setInt("shadows", useShadows);
-    shaders.world.setFloat("radius", shadowRadius);
-    shaders.world.setFloat("zbias", shadowBias);
-    Engine::matrix = {&shaders.world, model, &worldView};
+    worldShader.setMatrix("model", model.peek().getValues());
+    shadowBuffer.bindTextureTo(0, 1);
+    worldShader.setInt("shadows", useShadows);
+    worldShader.setFloat("radius", shadowRadius);
+    worldShader.setFloat("zbias", shadowBias);
+    Engine::matrix = {&worldShader, model, &worldView};
     Game::renderWorld();
 }
 
 static void renderSSAO() {
-    shaders.ssao.use();
+    ssaoShader.use();
 
     Matrix rProj;
     rProj.scale(0.5f).translate(Vector3(0.5f, 0.5f, 0.5f));
     rProj *= worldProj;
 
-    shaders.ssao.setMatrix("proj", rProj.getValues());
+    ssaoShader.setMatrix("proj", rProj.getValues());
     const Size& size = window.getSize();
-    shaders.ssao.setInt("width", size.width);
-    shaders.ssao.setInt("height", size.height);
-    framebuffers.world.bindTextureTo(0, 0);
-    framebuffers.world.bindTextureTo(4, 1);
+    ssaoShader.setInt("width", size.width);
+    ssaoShader.setInt("height", size.height);
+    worldBuffer.bindTextureTo(0, 0);
+    worldBuffer.bindTextureTo(4, 1);
     ssaoNoise.bindTo(2);
-    framebuffers.ssao.bindAndClear();
+    ssaoBuffer.bindAndClear();
     rectangle.draw();
 
-    shaders.ssaoBlur.use();
-    framebuffers.ssao.bindTextureTo(0, 0);
-    framebuffers.ssaoBlur.bindAndClear();
+    ssaoBlurShader.use();
+    ssaoBuffer.bindTextureTo(0, 0);
+    ssaoBlurBuffer.bindAndClear();
     rectangle.draw();
 }
 
 static void renderPostWorld() {
     GL::bindMainFramebuffer();
     GL::clear();
-    shaders.postWorld.use();
-    framebuffers.world.bindTextureTo(2, 0);
-    framebuffers.ssaoBlur.bindTextureTo(0, 1);
-    framebuffers.world.bindTextureTo(3, 2);
-    framebuffers.world.bindTextureTo(1, 3);
-    shaders.postWorld.setInt("ssao", useSsao);
-    shaders.postWorld.setInt("shadows", useShadows);
+    postWorldShader.use();
+    worldBuffer.bindTextureTo(2, 0);
+    ssaoBlurBuffer.bindTextureTo(0, 1);
+    worldBuffer.bindTextureTo(3, 2);
+    worldBuffer.bindTextureTo(1, 3);
+    postWorldShader.setInt("ssao", useSsao);
+    postWorldShader.setInt("shadows", useShadows);
     rectangle.draw();
 }
 
 static void renderOverlay() {
     GL::disableDepthTesting();
-    shaders.overlay.use();
+    overlayShader.use();
 
     const Size& size = window.getSize();
     Matrix m;
     m.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f))
         .translate(Vector3(-1.0f, 1.0f, 0.0f));
-    shaders.overlay.setMatrix("view", m.getValues());
+    overlayShader.setMatrix("view", m.getValues());
     model.clear();
-    shaders.overlay.setMatrix("model", model.peek().getValues());
+    overlayShader.setMatrix("model", model.peek().getValues());
 
     GL::enableBlending();
-    Engine::matrix = {&shaders.overlay, model, &m};
+    Engine::matrix = {&overlayShader, model, &m};
     Game::renderOverlay();
     GL::disableBlending();
 }
@@ -197,7 +268,7 @@ static void startRender() {
     const Size& size = window.getSize();
     if(size.width != lastSize.width || size.height != lastSize.height) {
         GL::setViewport(size.width, size.height);
-        framebuffers.resize(size);
+        resizeFramebuffers(size);
         lastSize = size;
     }
     GL::printError("loop error");

+ 0 - 27
client/rendering/Framebuffers.cpp

@@ -1,27 +0,0 @@
-#include "client/rendering/Framebuffers.h"
-
-void Framebuffers::resize(const Size& size) {
-    world.resize(size);
-    ssao.resize(size);
-    ssaoBlur.resize(size);
-    shadow.resize(size);
-}
-
-Error Framebuffers::init(const Size& size) {
-    Error error =
-        world.init(size, TextureFormat::float32(3), TextureFormat::float32(3),
-                   TextureFormat::color8(4), TextureFormat::float32(1),
-                   TextureFormat::depth32(true));
-    if(error.has()) {
-        return error;
-    }
-    error = ssao.init(size, TextureFormat::float32(1));
-    if(error.has()) {
-        return error;
-    }
-    error = ssaoBlur.init(size, TextureFormat::float32(1));
-    if(error.has()) {
-        return error;
-    }
-    return shadow.init(size, TextureFormat::depth32());
-}

+ 0 - 17
client/rendering/Framebuffers.h

@@ -1,17 +0,0 @@
-#ifndef FRAMEBUFFERS_H
-#define FRAMEBUFFERS_H
-
-#include "rendering/Framebuffer.h"
-#include "utils/Size.h"
-
-struct Framebuffers final {
-    void resize(const Size& size);
-    Error init(const Size& size);
-
-    Framebuffer<5> world;
-    Framebuffer<1> ssao;
-    Framebuffer<1> ssaoBlur;
-    Framebuffer<1> shadow;
-};
-
-#endif

+ 7 - 0
client/rendering/Mesh.cpp

@@ -1,6 +1,13 @@
 #include "client/rendering/Mesh.h"
 #include "rendering/Attributes.h"
 
+Mesh::Triangle::Triangle(const Vertex& a, const Vertex& b, const Vertex& c)
+    : a(a), b(b), c(c) {
+    normalA = (b.position - a.position).cross(c.position - a.position);
+    normalB = normalA;
+    normalC = normalA;
+}
+
 Mesh::Mesh() : vertices(0) {
 }
 

+ 18 - 1
client/rendering/Mesh.h

@@ -1,7 +1,7 @@
 #ifndef MESH_H
 #define MESH_H
 
-#include "client/rendering/Triangle.h"
+#include "math/Vector.h"
 #include "rendering/VertexBuffer.h"
 #include "utils/TypedBuffer.h"
 
@@ -10,6 +10,23 @@ class Mesh final {
     int vertices;
 
 public:
+    struct Vertex final {
+        Vector3 position;
+        Vector2 texture;
+    };
+
+    class Triangle final {
+        Vertex a;
+        Vector3 normalA;
+        Vertex b;
+        Vector3 normalB;
+        Vertex c;
+        Vector3 normalC;
+
+    public:
+        Triangle(const Vertex& a, const Vertex& b, const Vertex& c);
+    };
+
     Mesh();
     bool init();
 

+ 0 - 18
client/rendering/NoiseTexture.cpp

@@ -1,18 +0,0 @@
-#include "client/rendering/NoiseTexture.h"
-#include "utils/Array.h"
-#include "utils/Random.h"
-
-bool NoiseTexture::init() {
-    texture.init(TextureFormat::float32(3), 0);
-    Random r(1);
-    Array<float, 48> data;
-    for(int i = 0; i < 48; i++) {
-        data[i] = r.nextFloat() * 2.0f - 1.0f;
-    }
-    texture.setData(4, 4, data.begin());
-    return false;
-}
-
-void NoiseTexture::bindTo(int index) const {
-    texture.bindTo(index);
-}

+ 0 - 14
client/rendering/NoiseTexture.h

@@ -1,14 +0,0 @@
-#ifndef NOISETEXTURE_H
-#define NOISETEXTURE_H
-
-#include "rendering/Texture.h"
-
-class NoiseTexture final {
-    Texture texture;
-
-public:
-    bool init();
-    void bindTo(int index) const;
-};
-
-#endif

+ 0 - 31
client/rendering/Shaders.cpp

@@ -1,31 +0,0 @@
-#include "client/rendering/Shaders.h"
-
-Error Shaders::init() {
-    Error error =
-        world.compile("resources/shader/world.vs", "resources/shader/world.fs");
-    if(error.has()) {
-        return error;
-    }
-    error =
-        ssao.compile("resources/shader/ssao.vs", "resources/shader/ssao.fs");
-    if(error.has()) {
-        return error;
-    }
-    error = ssaoBlur.compile("resources/shader/ssaoBlur.vs",
-                             "resources/shader/ssaoBlur.fs");
-    if(error.has()) {
-        return error;
-    }
-    error = shadow.compile("resources/shader/worldShadow.vs",
-                           "resources/shader/worldShadow.fs");
-    if(error.has()) {
-        return error;
-    }
-    error = postWorld.compile("resources/shader/worldPost.vs",
-                              "resources/shader/worldPost.fs");
-    if(error.has()) {
-        return error;
-    }
-    return overlay.compile("resources/shader/overlay.vs",
-                           "resources/shader/overlay.fs");
-}

+ 0 - 17
client/rendering/Shaders.h

@@ -1,17 +0,0 @@
-#ifndef SHADERS_H
-#define SHADERS_H
-
-#include "rendering/Shader.h"
-
-struct Shaders final {
-    Error init();
-
-    Shader world;
-    Shader ssao;
-    Shader ssaoBlur;
-    Shader shadow;
-    Shader postWorld;
-    Shader overlay;
-};
-
-#endif

+ 0 - 10
client/rendering/Triangle.cpp

@@ -1,10 +0,0 @@
-#include "client/rendering/Triangle.h"
-
-Triangle::Triangle() {
-}
-
-Triangle::Triangle(const Vertex& a, const Vertex& b, const Vertex& c) : a(a), b(b), c(c) {
-    normalA = static_cast<Vector3>(b.position - a.position).cross(c.position - a.position);
-    normalB = normalA;
-    normalC = normalA;
-}

+ 0 - 19
client/rendering/Triangle.h

@@ -1,19 +0,0 @@
-#ifndef TRIANGLE_H
-#define TRIANGLE_H
-
-#include "client/rendering/Vertex.h"
-
-class Triangle final {
-    Vertex a;
-    Vector3 normalA;
-    Vertex b;
-    Vector3 normalB;
-    Vertex c;
-    Vector3 normalC;
-
-public:
-    Triangle();
-    Triangle(const Vertex& a, const Vertex& b, const Vertex& c);
-};
-
-#endif

+ 0 - 7
client/rendering/Vertex.cpp

@@ -1,7 +0,0 @@
-#include "client/rendering/Vertex.h"
-
-Vertex::Vertex() {
-}
-
-Vertex::Vertex(const Vector3& position, const Vector2& texture) : position(position), texture(texture) {
-}

+ 0 - 14
client/rendering/Vertex.h

@@ -1,14 +0,0 @@
-#ifndef VERTEX_H
-#define VERTEX_H
-
-#include "math/Vector.h"
-
-struct Vertex final {
-    Vertex();
-    Vertex(const Vector3& position, const Vector2& texture);
-
-    Vector3 position;
-    Vector2 texture;
-};
-
-#endif

+ 0 - 5
meson.build

@@ -22,19 +22,14 @@ src_server = ['server/Main.cpp',
 ]
 
 src_client = ['client/Main.cpp',
-    'client/rendering/Framebuffers.cpp',
     'client/rendering/Engine.cpp',
-    'client/rendering/Shaders.cpp',
     'client/rendering/Mesh.cpp',
     'client/Game.cpp',
     'client/GameClient.cpp',
     'client/World.cpp', 
     'client/rendering/ShaderMatrix.cpp',
-    'client/rendering/NoiseTexture.cpp',
     'client/input/Controller.cpp',
     'client/rendering/Renderer.cpp',
-    'client/rendering/Vertex.cpp',
-    'client/rendering/Triangle.cpp',
     'client/gui/BaseGUI.cpp',
     'client/gui/StartGUI.cpp',
     'client/packets/WorldPackets.cpp',