Browse Source

usage of new typed buffer

Kajetan Johannes Hammerle 3 years ago
parent
commit
f646e22cc7

+ 1 - 1
client/Main.cpp

@@ -50,7 +50,7 @@ int main() {
     Controller controller(buttons);
     Clock fps;
     Clock tps;
-    static Game game(controller, fps, tps, renderSettings, size);
+    Game game(controller, fps, tps, renderSettings, size);
 
     window.show();
 

+ 4 - 3
client/rendering/Engine.cpp

@@ -4,15 +4,16 @@
 
 Engine::Engine(Shaders& shaders, Framebuffers& fb, const Size& size, RenderSettings& renderSettings) :
 shaders(shaders), framebuffers(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 1000.0f, size) {
-    rectangle.add(Triangle(
+    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))));
-    rectangle.add(Triangle(
+    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();
+    rectangle.build(buffer);
 }
 
 void Engine::renderTick(float lag, Game& game) {

+ 5 - 14
client/rendering/Mesh.cpp

@@ -1,24 +1,15 @@
-#include <fstream>
-
 #include "client/rendering/Mesh.h"
 #include "gaming-core/wrapper/Attributes.h"
 
-Mesh::Mesh() {
+Mesh::Mesh() : vertices(0) {
     vertexBuffer.setAttributes(Attributes().addFloat(3).addFloat(2).addFloat(3));
 }
 
-void Mesh::add(const Triangle& data) {
-    buffer.add(data);
-}
-
-void Mesh::clear() {
-    buffer.clear();
-}
-
-void Mesh::build() {
-    vertexBuffer.setStaticData(sizeof (Triangle) * buffer.getLength(), buffer.begin());
+void Mesh::build(const TypedBuffer<Triangle>& buffer) {
+    vertices = buffer.getLength() * 3;
+    vertexBuffer.setStaticData(buffer.getByteLength(), buffer);
 }
 
 void Mesh::draw() {
-    vertexBuffer.draw(buffer.getLength() * 3);
+    vertexBuffer.draw(vertices);
 }

+ 3 - 6
client/rendering/Mesh.h

@@ -2,22 +2,19 @@
 #define MESH_H
 
 #include "gaming-core/wrapper/VertexBuffer.h"
-#include "gaming-core/utils/List.h"
+#include "gaming-core/utils/TypedBuffer.h"
 #include "client/rendering/Triangle.h"
 
 class Mesh final {
 public:
     Mesh();
 
-    void add(const Triangle& data);
-
-    void clear();
-    void build();
+    void build(const TypedBuffer<Triangle>& buffer);
     void draw();
 
 private:
     VertexBuffer vertexBuffer;
-    List<Triangle, 20000> buffer;
+    int vertices;
 };
 
 #endif

+ 22 - 25
client/rendering/renderer/WorldRenderer.cpp

@@ -1,19 +1,17 @@
 #include "client/rendering/renderer/WorldRenderer.h"
 
 WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resources/textures.png") {
+    TypedBuffer<Triangle> buffer(100);
     for(int x = 0; x < World::WORLD_SIZE; x++) {
         for(int y = 0; y < World::WORLD_SIZE; y++) {
             for(int z = 0; z < World::WORLD_SIZE; z++) {
                 if(world.getBlock(x, y, z).getId() != 0) {
-                    addCube(x, y, z,
-                            isAir(x, y - 1, z), isAir(x, y + 1, z),
-                            isAir(x - 1, y, z), isAir(x + 1, y, z),
-                            isAir(x, y, z + 1), isAir(x, y, z - 1));
+                    addCube(buffer, x, y, z);
                 }
             }
         }
     }
-    mesh.build();
+    mesh.build(buffer);
 }
 
 void WorldRenderer::render(float lag, Renderer& renderer) {
@@ -27,7 +25,7 @@ bool WorldRenderer::isAir(int x, int y, int z) const {
     return world.getBlock(x, y, z).getId() == 0;
 }
 
-void WorldRenderer::addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back) {
+void WorldRenderer::addCube(TypedBuffer<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);
@@ -42,31 +40,30 @@ void WorldRenderer::addCube(float x, float y, float z, bool bottom, bool top, bo
     Vector2 t3(0.25f, 0.0625f);
     Vector2 t4(0.1875f, 0.0625f);
 
-    if(bottom) {
+    if(isAir(x, y - 1, z)) {
         Vector2 tb(0.125f, 0.0625f);
-        mesh.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)), Vertex(v100, t1), Vertex(v001, tb)));
-        mesh.add(Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
+        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)));
     }
-    if(top) {
+    if(isAir(x, y + 1, z)) {
         Vector2 tt(0.3125f, 0.0f);
-        mesh.add(Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
-        mesh.add(Triangle(Vertex(v110, tt), Vertex(v011, t3), Vertex(v111, Vector2(0.3125f, 0.0625f))));
+        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))));
     }
-    if(left) {
-        mesh.add(Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
-        mesh.add(Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
+    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)));
     }
-    if(right) {
-        mesh.add(Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
-        mesh.add(Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, 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)));
     }
-    if(front) {
-        mesh.add(Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
-        mesh.add(Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
+    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)));
     }
-    if(back) {
-        mesh.add(Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
-
-        mesh.add(Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
+    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)));
     }
 }

+ 1 - 1
client/rendering/renderer/WorldRenderer.h

@@ -11,7 +11,7 @@ public:
     WorldRenderer(const World& world);
 
     void render(float lag, Renderer& renderer);
-    void addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back);
+    void addCube(TypedBuffer<Triangle>& buffer, float x, float y, float z);
     bool isAir(int x, int y, int z) const;
 
 private:

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 1afdb909090ce8398de997bde094f1fcda3a2e4b
+Subproject commit ce80f29511be8945a77424db9d5962198c241fa8