Browse Source

vertex data moved to triangle and vertex class, normals are calculated

Kajetan Johannes Hammerle 3 years ago
parent
commit
7c3a4fe538

+ 17 - 8
client/rendering/Engine.cpp

@@ -9,24 +9,27 @@
 
 Engine::Engine(Shaders& shaders, Framebuffers& fb, const WindowSize& size, RenderSettings& renderSettings) :
 shaders(shaders), fb(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
-    rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
-    rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
-    rectangle.add( {-1, 1, 0, 0, 1, 0, 0, 0});
-    rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
-    rectangle.add( {1, -1, 0, 1, 0, 0, 0, 0});
-    rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
+    rectangle.add(Triangle(
+            Vertex(Vector3(-1, -1, 0), Vector2(0, 0)),
+            Vertex(Vector3(1, 1, 0), Vector2(1, 1)),
+            Vertex(Vector3(-1, 1, 0), Vector2(0, 1))));
+    rectangle.add(Triangle(
+            Vertex(Vector3(-1, -1, 0), Vector2(0, 0)),
+            Vertex(Vector3(1, -1, 0), Vector2(1, 0)),
+            Vertex(Vector3(1, 1, 0), Vector2(1, 1))));
     rectangle.build();
 }
 
 void Engine::renderTick(float lag, const Game& game) {
     updateWorldProjection();
     updateWorldView();
-
+    
     if(renderSettings.shadows) {
         renderShadow(lag, game);
     }
     renderWorld(lag, game);
     if(renderSettings.ssao) {
+
         renderSSAO();
     }
     renderPostWorld();
@@ -34,6 +37,7 @@ void Engine::renderTick(float lag, const Game& game) {
 }
 
 void Engine::renderShadow(float lag, const Game& game) {
+
     fb.shadow.bind();
     GLWrapper::enableDepthTesting();
     shaders.shadow.use();
@@ -46,6 +50,7 @@ void Engine::renderShadow(float lag, const Game& game) {
 }
 
 void Engine::renderWorld(float lag, const Game& game) {
+
     fb.world.bind();
     GLWrapper::enableDepthTesting();
     shaders.world.use();
@@ -67,6 +72,7 @@ void Engine::renderWorld(float lag, const Game& game) {
 }
 
 void Engine::renderSSAO() {
+
     shaders.ssao.use();
 
     Matrix rProj;
@@ -90,6 +96,7 @@ void Engine::renderSSAO() {
 }
 
 void Engine::renderPostWorld() {
+
     GLWrapper::prepareMainFramebuffer();
     shaders.postWorld.use();
     fb.world.bindColorTexture(0);
@@ -102,6 +109,7 @@ void Engine::renderPostWorld() {
 }
 
 void Engine::renderTextOverlay(float lag, const Game& game) {
+
     GLWrapper::disableDepthTesting();
     shaders.text.use();
 
@@ -120,8 +128,9 @@ void Engine::renderTextOverlay(float lag, const Game& game) {
 
 void Engine::updateWorldProjection() {
     frustum.setProjection(worldProj, size.width, size.height);
-    
+
     if(!renderSettings.shadows) {
+
         return;
     }
     worldShadowProj.setToIdentity();

+ 3 - 3
client/rendering/Mesh.cpp

@@ -7,7 +7,7 @@ Mesh::Mesh() {
     vertexBuffer.setFloatAttribute(2, 3, 5, 8);
 }
 
-void Mesh::add(const VertexData& data) {
+void Mesh::add(const Triangle& data) {
     buffer.add(data);
 }
 
@@ -17,10 +17,10 @@ void Mesh::clear() {
 
 void Mesh::build() {
     vertexBuffer.bindBuffer();
-    vertexBuffer.setData(sizeof (VertexData) * buffer.getLength(), buffer.getData());
+    vertexBuffer.setData(sizeof (Triangle) * buffer.getLength(), buffer.getData());
 }
 
 void Mesh::draw() const {
     vertexBuffer.bindArray();
-    vertexBuffer.draw(buffer.getLength());
+    vertexBuffer.draw(buffer.getLength() * 3);
 }

+ 3 - 14
client/rendering/Mesh.h

@@ -3,24 +3,13 @@
 
 #include "client/rendering/wrapper/VertexBuffer.h"
 #include "common/utils/List.h"
+#include "client/rendering/Triangle.h"
 
 class Mesh final {
 public:
-
-    struct VertexData final {
-        float x;
-        float y;
-        float z;
-        float tx;
-        float ty;
-        float nx;
-        float ny;
-        float nz;
-    };
-
     Mesh();
 
-    void add(const VertexData& data);
+    void add(const Triangle& data);
 
     void clear();
     void build();
@@ -28,7 +17,7 @@ public:
 
 private:
     VertexBuffer vertexBuffer;
-    List<VertexData, 65536 * 2> buffer;
+    List<Triangle, 20000> buffer;
 };
 
 #endif

+ 8 - 20
client/rendering/Triangle.cpp

@@ -1,24 +1,12 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/* 
- * File:   Triangle.cpp
- * Author: kajetan
- * 
- * Created on November 12, 2020, 8:07 PM
- */
-
-#include "Triangle.h"
+#include "client/rendering/Triangle.h"
 
 Triangle::Triangle() {
 }
 
-Triangle::Triangle(const Triangle& orig) {
-}
-
-Triangle::~Triangle() {
-}
-
+Triangle::Triangle(const Vertex& a, const Vertex& b, const Vertex& c) : a(a), b(b), c(c) {
+    Vector3 ac = c.position - a.position;
+    Vector3 ab = b.position - a.position;
+    normalA = ab.cross(ac);
+    normalB = normalA;
+    normalC = normalA;
+}

+ 14 - 5
client/rendering/Triangle.h

@@ -1,13 +1,22 @@
 #ifndef TRIANGLE_H
 #define TRIANGLE_H
 
-class Triangle {
-public:
+#include "client/rendering/Vertex.h"
+
+struct Triangle final {
     Triangle();
-    Triangle(const Triangle& orig);
-    virtual ~Triangle();
-private:
+    Triangle(const Vertex& a, const Vertex& b, const Vertex& c);
 
+private:
+    Vertex a;
+    Vector3 normalA;
+    //Vector3 tangentA;
+    Vertex b;
+    Vector3 normalB;
+    //Vector3 tangentB;
+    Vertex c;
+    Vector3 normalC;
+    //Vector3 tangentC;
 };
 
 #endif

+ 3 - 20
client/rendering/Vertex.cpp

@@ -1,24 +1,7 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/* 
- * File:   Vertex.cpp
- * Author: kajetan
- * 
- * Created on November 12, 2020, 8:10 PM
- */
-
-#include "Vertex.h"
+#include "client/rendering/Vertex.h"
 
 Vertex::Vertex() {
 }
 
-Vertex::Vertex(const Vertex& orig) {
-}
-
-Vertex::~Vertex() {
-}
-
+Vertex::Vertex(const Vector3& position, const Vector2& texture) : position(position), texture(texture) {
+}

+ 4 - 8
client/rendering/Vertex.h

@@ -4,15 +4,11 @@
 #include "client/math/Vector.h"
 
 struct Vertex final {
-public:
     Vertex();
-    Vertex(const Vertex& orig);
-    virtual ~Vertex();
-private:
-    
-    Vector position;
-    float tx;
-    float ty;
+    Vertex(const Vector3& position, const Vector2& texture);
+
+    Vector3 position;
+    Vector2 texture;
 };
 
 #endif

+ 28 - 43
client/rendering/renderer/WorldRenderer.cpp

@@ -1,5 +1,4 @@
 #include "client/rendering/renderer/WorldRenderer.h"
-#include <iostream>
 
 WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resources/textures.png"),
 normalTexture("resources/textures.png") {
@@ -31,59 +30,45 @@ bool WorldRenderer::isAir(uint x, uint y, uint z) const {
 }
 
 void WorldRenderer::addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back) {
-    const float ERROR = 1.0f / 1024.0f;
-    if(bottom) {
-        mesh.add({x - ERROR, y - ERROR, z - ERROR, 0.125f, 0.0f, 0, -1, 0});
-        mesh.add({x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
-        mesh.add({x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
+    Vector3 v000(x, y, z);
+    Vector3 v001(x, y, z + 1);
+    Vector3 v010(x, y + 1, z);
+    Vector3 v011(x, y + 1, z + 1);
+    Vector3 v100(x + 1, y, z);
+    Vector3 v101(x + 1, y, z + 1);
+    Vector3 v110(x + 1, y + 1, z);
+    Vector3 v111(x + 1, y + 1, z + 1);
+
+    Vector2 t1(0.1875f, 0.0f);
+    Vector2 t2(0.25f, 0.0f);
+    Vector2 t3(0.25f, 0.0625f);
+    Vector2 t4(0.1875f, 0.0625f);
 
-        mesh.add({x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
-        mesh.add({x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, -1, 0});
-        mesh.add({x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
+    if(bottom) {
+        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)));
     }
     if(top) {
-        mesh.add({x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 1, 0});
-        mesh.add({x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
-
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
-        mesh.add({x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.3125f, 0.0625f, 0, 1, 0});
+        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))));
     }
     if(left) {
-        mesh.add({x - ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, -1, 0, 0});
-        mesh.add({x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
-        mesh.add({x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
-
-        mesh.add({x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
-        mesh.add({x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, -1, 0, 0});
-        mesh.add({x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
+        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(right) {
-        mesh.add({x + 1 + ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 1, 0, 0});
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
-        mesh.add({x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
-
-        mesh.add({x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 1, 0, 0});
+        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(front) {
-        mesh.add({x - ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, 0, 1});
-        mesh.add({x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
-        mesh.add({x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
-
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, 0, 0, 1});
-        mesh.add({x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
-        mesh.add({x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
+        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(back) {
-        mesh.add({x - ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 0, 0, -1});
-        mesh.add({x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
-        mesh.add({x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
+        mesh.add(Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
 
-        mesh.add({x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, 0, 0, -1});
-        mesh.add({x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
-        mesh.add({x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
+        mesh.add(Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
     }
 }

+ 1 - 1
meson.build

@@ -6,7 +6,7 @@ sourcesCommon = ['common/network/Packet.cpp', 'common/block/BlockBuilder.cpp', '
 
 sourcesServer = ['server/Main.cpp', 'server/network/Server.cpp', 'server/network/Client.cpp', 'server/GameServer.cpp', 'server/commands/ServerCommands.cpp', 'server/commands/CommandManager.cpp', 'server/commands/ConsoleEditor.cpp', 'server/Clock.cpp']
 
-sourcesClient = ['client/Main.cpp', 'client/rendering/WindowSize.cpp', 'client/math/Frustum.cpp', 'client/rendering/Framebuffers.cpp', 'client/rendering/wrapper/GLFWWrapper.cpp', 'client/rendering/wrapper/Window.cpp', 'client/rendering/Engine.cpp', 'client/input/Keys.cpp', 'client/rendering/wrapper/Shader.cpp', 'client/rendering/Shaders.cpp', 'client/utils/Utils.cpp', 'client/rendering/Mesh.cpp', 'client/math/Matrix.cpp', 'client/math/MatrixStack.cpp', 'client/math/Quaternion.cpp', 'client/math/Plane.cpp', 'client/Game.cpp', 'client/input/MouseButtons.cpp', 'client/rendering/FileTexture.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', 'client/rendering/wrapper/Texture.cpp', 'client/utils/PNGReader.cpp', 'client/rendering/wrapper/GLWrapper.cpp', 'client/rendering/Renderer.cpp', 'client/rendering/renderer/WorldRenderer.cpp', 'client/rendering/NormalTexture.cpp', 'client/math/Vector.cpp']
+sourcesClient = ['client/Main.cpp', 'client/rendering/WindowSize.cpp', 'client/math/Frustum.cpp', 'client/rendering/Framebuffers.cpp', 'client/rendering/wrapper/GLFWWrapper.cpp', 'client/rendering/wrapper/Window.cpp', 'client/rendering/Engine.cpp', 'client/input/Keys.cpp', 'client/rendering/wrapper/Shader.cpp', 'client/rendering/Shaders.cpp', 'client/utils/Utils.cpp', 'client/rendering/Mesh.cpp', 'client/math/Matrix.cpp', 'client/math/MatrixStack.cpp', 'client/math/Quaternion.cpp', 'client/math/Plane.cpp', 'client/Game.cpp', 'client/input/MouseButtons.cpp', 'client/rendering/FileTexture.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', 'client/rendering/wrapper/Texture.cpp', 'client/utils/PNGReader.cpp', 'client/rendering/wrapper/GLWrapper.cpp', 'client/rendering/Renderer.cpp', 'client/rendering/renderer/WorldRenderer.cpp', 'client/rendering/NormalTexture.cpp', 'client/math/Vector.cpp', 'client/rendering/Vertex.cpp', 'client/rendering/Triangle.cpp']
 
 sourcesTest = ['tests/Main.cpp', 'common/utils/String.cpp', 'common/utils/SplitString.cpp', 'client/math/Matrix.cpp', 'common/utils/HashedString.cpp', 'common/utils/Random.cpp']
 

+ 3 - 2
resources/shader/worldFragment.fs

@@ -58,7 +58,8 @@ void main(void) {
         worldShadow = 1.0;
     }
 
-    worldColor = vec4(worldNormal, 1);
-    //worldColor = texture(normalSamp, varTex);
+    //worldColor = vec4(1, 0, 0, 1);
+    //worldColor = vec4(worldNormal, 1);
+    worldColor = texture(samp, varTex);
     //worldColor = texture(normalSamp, varTex);
 }

+ 2 - 2
resources/shader/worldPostFragment.fs

@@ -25,6 +25,6 @@ void main() {
         float f = diffuseLight * texture(shadowSamp, varTex).r;
         color *= f * 0.5 + 0.5;
     }
-    float diffuseLight = max(dot(texture(worldNormalSamp, varTex).xyz, -light), 0.0);
-    color *= diffuseLight;
+    //float diffuseLight = max(dot(texture(worldNormalSamp, varTex).xyz, -light), 0.0);
+    //color *= diffuseLight;
 }