Browse Source

experimental ssao shader

Kajetan Johannes Hammerle 4 years ago
parent
commit
3e19acc957

+ 97 - 11
client/Game.cpp

@@ -2,45 +2,131 @@
 
 #include "client/Game.h"
 
-Game::Game() : texture("resources/textures.png")
+Game::Game() : lengthAngle(0.0f), widthAngle(0.0f), texture("resources/textures.png")
 {
-    m.add({0, 0, 0.9, 0, 0, 1, 0, 0});
-    m.add({0, 1, 0.9, 0, 1, 1, 0, 0});
-    m.add({1, 0, 0.9, 1, 0, 1, 0, 0});
+    for(int x = -6; x <= 6; x++)
+    {
+        for(int y = -6; y <= 6; y++)
+        {
+            for(int z = -6; z <= 6; z++)
+            {
+                if(x * x + y * y + z * z < 16)
+                {
+                    addCube(x - 3, y - 5, z - 10);
+                }
+            }
+        }
+    }
     m.build();
 }
 
+void Game::addCube(float x, float y, float z)
+{
+    // bottom
+    m.add({x, y, z, 0.125f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y, z, 0.1875f, 0.0f, 1, 0, 0});
+    m.add({x, y, z + 1, 0.125f, 0.0625f, 1, 0, 0});
+    
+    m.add({x + 1, y, z, 0.1875f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y, z + 1, 0.1875f, 0.0625f, 1, 0, 0});
+    m.add({x, y, z + 1, 0.125f, 0.0625f, 1, 0, 0});
+    
+    // top
+    m.add({x, y + 1, z, 0.25f, 0.0f, 1, 0, 0});
+    m.add({x, y + 1, z + 1, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x + 1, y + 1, z, 0.3125f, 0.0f, 1, 0, 0});
+    
+    m.add({x + 1, y + 1, z, 0.3125f, 0.0f, 1, 0, 0});
+    m.add({x, y + 1, z + 1, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x + 1, y + 1, z + 1, 0.3125f, 0.0625f, 1, 0, 0});
+    
+    // left
+    m.add({x, y, z, 0.1875f, 0.0625f, 1, 0, 0});
+    m.add({x, y, z + 1, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x, y + 1, z, 0.1875f, 0.0f, 1, 0, 0});
+    
+    m.add({x, y, z + 1, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x, y + 1, z + 1, 0.25f, 0.0f, 1, 0, 0});
+    m.add({x, y + 1, z, 0.1875f, 0.0f, 1, 0, 0});
+    
+    // right
+    m.add({x + 1, y, z, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x + 1, y + 1, z, 0.25f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y, z + 1, 0.1875f, 0.0625f, 1, 0, 0});
+    
+    m.add({x + 1, y, z + 1, 0.1875f, 0.0625f, 1, 0, 0});
+    m.add({x + 1, y + 1, z, 0.25f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y + 1, z + 1, 0.1875f, 0.0f, 1, 0, 0});
+    
+    // front
+    m.add({x, y, z + 1, 0.1875f, 0.0625f, 1, 0, 0});
+    m.add({x + 1, y, z + 1, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x, y + 1, z + 1, 0.1875f, 0.0f, 1, 0, 0});
+    
+    m.add({x + 1, y + 1, z + 1, 0.25f, 0.0f, 1, 0, 0});
+    m.add({x, y + 1, z + 1, 0.1875f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y, z + 1, 0.25f, 0.0625f, 1, 0, 0});
+    
+    // back
+    m.add({x, y, z, 0.25f, 0.0625f, 1, 0, 0});
+    m.add({x, y + 1, z, 0.25f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y, z, 0.1875f, 0.0625f, 1, 0, 0});
+    
+    m.add({x + 1, y + 1, z, 0.1875f, 0.0f, 1, 0, 0});
+    m.add({x + 1, y, z, 0.1875f, 0.0625f, 1, 0, 0}); 
+    m.add({x, y + 1, z, 0.25f, 0.0f, 1, 0, 0});
+}
+
 void Game::tick(const Keys& keys, const MouseButtons& mButtons, Camera& cam)
 {
     (void) mButtons;
     
+    const float speed = 0.5f;
     if(keys.down.isDown())
     {
-        pos.addMul(cam.getFlatBack(), 0.1f);
+        pos.addMul(cam.getFlatBack(), speed);
     }
     if(keys.up.isDown())
     {
-        pos.addMul(cam.getFlatFront(), 0.1f);
+        pos.addMul(cam.getFlatFront(), speed);
     }
     if(keys.left.isDown())
     {
-        pos.addMul(cam.getFlatLeft(), 0.1f);
+        pos.addMul(cam.getFlatLeft(), speed);
     }
     if(keys.right.isDown())
     {
-        pos.addMul(cam.getFlatRight(), 0.1f);
+        pos.addMul(cam.getFlatRight(), speed);
     }
     if(keys.jump.isDown())
     {
-        pos.addMul(cam.getFlatUp(), 0.1f);
+        pos.addMul(cam.getFlatUp(), speed);
     }
     if(keys.sneak.isDown())
     {
-        pos.addMul(cam.getFlatDown(), 0.1f);
+        pos.addMul(cam.getFlatDown(), speed);
+    }
+    
+    const float rotation = 5.0f;
+    if(keys.camLeft.isDown())
+    {
+        lengthAngle += rotation;
+    }
+    if(keys.camRight.isDown())
+    {
+        lengthAngle -= rotation;
+    }
+    if(keys.camUp.isDown() && widthAngle - rotation > -90.0f)
+    {
+        widthAngle -= rotation;
+    }
+    if(keys.camDown.isDown() && widthAngle + rotation < 90.0f)
+    {
+        widthAngle += rotation;
     }
 
     cam.storePosition();
-    cam.setPosition(pos, 0, 0);
+    cam.setPosition(pos, lengthAngle, widthAngle);
 }
 
 void Game::renderWorld(float lag, MatrixStack& stack, Shader& sh)

+ 4 - 0
client/Game.h

@@ -20,6 +20,10 @@ public:
     void renderTextOverlay(float lag, MatrixStack& stack, Shader& sh, FontRenderer& fr);
     
 private:
+    void addCube(float x, float y, float z);
+    
+    float lengthAngle;
+    float widthAngle;
     Vector pos;
     Mesh m;
     Texture texture;

+ 52 - 15
client/GameClient.cpp

@@ -17,6 +17,7 @@
 #include "client/math/Matrix.h"
 #include "client/math/MatrixStack.h"
 #include "client/Game.h"
+#include "rendering/NoiseTexture.h"
 
 struct InternGameClient
 {
@@ -52,45 +53,60 @@ static MouseButtons mButtons;
 struct Shaders
 {
     Shaders() : 
-        world("resources/shader/testVertex.vs", "resources/shader/testFragment.fs"),
+        world("resources/shader/worldVertex.vs", "resources/shader/worldFragment.fs"),
+        ssao("resources/shader/ssaoVertex.vs", "resources/shader/ssaoFragment.fs"),
+        ssaoBlur("resources/shader/ssaoBlurVertex.vs", "resources/shader/ssaoBlurFragment.fs"),
         text("resources/shader/textVertex.vs", "resources/shader/textFragment.fs")
     {
     }
     
     Shader world;
+    Shader ssao;
+    Shader ssaoBlur;
     Shader text;
     
     bool isValid() const
     {
-        return world.isValid() && text.isValid();
+        return world.isValid() && ssao.isValid() && ssaoBlur.isValid() && text.isValid();
     }
 };
 
 struct Framebuffers
 {
-    Framebuffers(u32 w, u32 h) : worldBuffer(w, h, Framebuffer::COLOR)
+    Framebuffers(u32 w, u32 h) : worldBuffer(w, h, Framebuffer::POSITION | 
+        Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::DEPTH24_STENCIL8),
+        ssaoBuffer(w, h, Framebuffer::RED), ssaoBlurBuffer(w, h, Framebuffer::RED)
     {
     }
     
     void resize(u32 w, u32 h) const
     {
         worldBuffer.resize(w, h);
+        ssaoBuffer.resize(w, h);
+        ssaoBlurBuffer.resize(w, h);
     }
     
     bool isValid() const
     {
-        return worldBuffer.isValid();
+        return worldBuffer.isValid() && ssaoBuffer.isValid() && ssaoBlurBuffer.isValid();
     }
     
     Framebuffer worldBuffer;
+    Framebuffer ssaoBuffer;
+    Framebuffer ssaoBlurBuffer;
 };
 
 struct InternGame
 {
+    InternGame() : ssaoNoise(4, 4)
+    {
+    }
+    
     Game game;
     Camera cam;
     MatrixStack model;
     FontRenderer fontRenderer;
+    NoiseTexture ssaoNoise;
 };
 
 static u64 getTimeNanos()
@@ -197,7 +213,6 @@ static void tick(InternGame& game)
 
 static void renderWorld(float lag, Shaders& shaders, InternGame& game)
 {
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glEnable(GL_DEPTH_TEST);
     // use world program
     shaders.world.use();
@@ -232,9 +247,15 @@ static void renderWorld(float lag, Shaders& shaders, InternGame& game)
     shaders.world.setMatrix("model", game.model.get().getValues());
     // call high level world renderer
     game.game.renderWorld(lag, game.model, shaders.world);
+
+    shaders.ssao.use();
+    shaders.ssao.setMatrix("view", view);
+    shaders.ssao.setMatrix("proj", proj);
+    shaders.ssao.setInt("width", width);
+    shaders.ssao.setInt("height", height);
 }
 
-static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game)
+/*static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game)
 {
     glDisable(GL_DEPTH_TEST);
     shaders.text.use();
@@ -253,7 +274,7 @@ static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game)
     glBlendEquation(GL_FUNC_ADD);
     game.game.renderTextOverlay(lag, game.model, shaders.text, game.fontRenderer);
     glDisable(GL_BLEND);
-}
+}*/
 
 static void renderTick(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb)
 {
@@ -264,9 +285,6 @@ static void renderTick(float lag, Shaders& shaders, InternGame& game, Framebuffe
     }
     fb.worldBuffer.bind();
     renderWorld(lag, shaders, game);
-    glBindFramebuffer(GL_FRAMEBUFFER, 0);
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-    renderTextOverlay(lag, shaders, game);
 }
 
 static void loop()
@@ -276,8 +294,8 @@ static void loop()
     {
         return;
     }
-    Framebuffers framebuffers(width, height);
-    if(!framebuffers.isValid())
+    Framebuffers fb(width, height);
+    if(!fb.isValid())
     {
         return;
     }
@@ -286,8 +304,8 @@ static void loop()
     Shader sh("resources/shader/test2Vertex.vs", "resources/shader/test2Fragment.fs");
     Mesh m;
     m.add({-1, -1, 0, 0, 0, 0, 0, 0});
-    m.add({-1,  1, 0, 0, 1, 0, 0, 0});
     m.add({ 1,  1, 0, 1, 1, 0, 0, 0});
+    m.add({-1,  1, 0, 0, 1, 0, 0, 0});
     m.add({-1, -1, 0, 0, 0, 0, 0, 0});
     m.add({ 1, -1, 0, 1, 0, 0, 0, 0});
     m.add({ 1,  1, 0, 1, 1, 0, 0, 0});
@@ -297,9 +315,28 @@ static void loop()
     u64 lag = 0;
     while(!glfwWindowShouldClose(client.window))
     {
-        renderTick(lag * lagFactor, shaders, game, framebuffers);
-        framebuffers.worldBuffer.bindColorTexture(1);
+        renderTick(lag * lagFactor, shaders, game, fb);
+        
+        fb.worldBuffer.bindPositionTexture(0);
+        fb.worldBuffer.bindNormalTexture(1);
+        fb.worldBuffer.bindColorTexture(2);
+        fb.worldBuffer.bindDepthTexture(3);
+        game.ssaoNoise.bind(4);
+        fb.ssaoBuffer.bind();
+        m.draw();
+        
+        shaders.ssaoBlur.use();
+        fb.ssaoBuffer.bindRedTexture(0);
+        fb.worldBuffer.bindColorTexture(1);
+        fb.ssaoBlurBuffer.bind();
+        m.draw();
+
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
+        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+        fb.ssaoBlurBuffer.bindRedTexture(1);
         sh.use();
+        //glDisable(GL_CULL_FACE);
+        glDisable(GL_DEPTH_TEST);
         glEnable(GL_BLEND);
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         glBlendEquation(GL_FUNC_ADD);

+ 7 - 1
client/input/Keys.cpp

@@ -24,7 +24,9 @@ std::ostream& operator <<(std::ostream& os, const Keys::Key& k)
     return os;
 }
 
-Keys::Keys() : left(keys[0]), right(keys[1]), up(keys[2]), down(keys[3]), jump(keys[4]), sneak(keys[5])
+Keys::Keys() : left(keys[0]), right(keys[1]), up(keys[2]), down(keys[3]), 
+        jump(keys[4]), sneak(keys[5]),
+        camLeft(keys[6]), camRight(keys[7]), camUp(keys[8]), camDown(keys[9])
 {
     keys[0].glfwKey = GLFW_KEY_A;
     keys[1].glfwKey = GLFW_KEY_D;
@@ -32,6 +34,10 @@ Keys::Keys() : left(keys[0]), right(keys[1]), up(keys[2]), down(keys[3]), jump(k
     keys[3].glfwKey = GLFW_KEY_S;
     keys[4].glfwKey = GLFW_KEY_SPACE;
     keys[5].glfwKey = GLFW_KEY_LEFT_SHIFT;
+    keys[6].glfwKey = GLFW_KEY_LEFT;
+    keys[7].glfwKey = GLFW_KEY_RIGHT;
+    keys[8].glfwKey = GLFW_KEY_UP;
+    keys[9].glfwKey = GLFW_KEY_DOWN;
 }
 
 void Keys::release(int key)

+ 5 - 1
client/input/Keys.h

@@ -32,7 +32,7 @@ public:
     };
     
 private:
-   Key keys[6];
+   Key keys[10];
     
 public:
     Keys();
@@ -42,6 +42,10 @@ public:
     const Key& down;
     const Key& jump;
     const Key& sneak;
+    const Key& camLeft;
+    const Key& camRight;
+    const Key& camUp;
+    const Key& camDown;
     
     void release(int key);
     void press(int key);

+ 20 - 19
client/math/Camera.cpp

@@ -87,19 +87,19 @@ void Camera::setPosition(const Vector& pos, float length, float width)
 
 void Camera::update(float lag)
 {
-    // front
-    front.setAngles(interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
     // back
-    back.setInverse(front);
-    // left
-    left = front;
-    left.cross(0.0f, 1.0f, 0.0f);
-    left.normalize();
+    back.setAngles(interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
+    // front
+    front.setInverse(back);
     // right
-    right.setInverse(left);
+    right = front;
+    right.cross(0.0f, 1.0f, 0.0f);
+    right.normalize();
+    // left
+    left.setInverse(right);
     // up
     up = front;
-    up.cross(right);
+    up.cross(left);
     up.normalize();
     // down
     down.setInverse(up);
@@ -107,18 +107,19 @@ void Camera::update(float lag)
     interPosition = oldPosition;
     interPosition.addMul(position, lag);
     interPosition.addMul(oldPosition, -lag);
-    // flat front
-    flatFront = front;
-    flatFront.setY(0.0f);
-    flatFront.normalize();
+    
     // flat back
-    flatBack.setInverse(flatFront);
-    // flat left
-    flatLeft = flatFront;
-    flatLeft.cross(0.0f, 1.0f, 0.0f);
-    flatLeft.normalize();
+    flatBack = back;
+    flatBack.setY(0.0f);
+    flatBack.normalize();
+    // flat back
+    flatFront.setInverse(flatBack);
     // flat right
-    flatRight.setInverse(flatLeft);
+    flatRight = front;
+    flatRight.cross(0.0f, 1.0f, 0.0f);
+    flatRight.normalize();
+    // flat left
+    flatLeft.setInverse(flatRight);
     // flat up
     flatUp.set(0.0f, 1.0f, 0.0f);
     // flat down

+ 2 - 2
client/rendering/Framebuffer.cpp

@@ -15,7 +15,7 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode) : mode(mode), buffer(0
     {
         glGenTextures(1, &(textures[0]));
         glBindTexture(GL_TEXTURE_2D, textures[0]);
-        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -57,7 +57,7 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode) : mode(mode), buffer(0
     {
         glGenTextures(1, &textures[3]);
         glBindTexture(GL_TEXTURE_2D, textures[3]);
-        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

+ 45 - 0
client/rendering/NoiseTexture.cpp

@@ -0,0 +1,45 @@
+#include <png.h>
+#include <vector>
+#include <cstdlib>
+
+#include "client/rendering/NoiseTexture.h"
+
+NoiseTexture::NoiseTexture(u32 width, u32 height)
+{
+    glGenTextures(1, &texture);
+    glBindTexture(GL_TEXTURE_2D, texture);
+    
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+    std::vector<float> data;
+    for(u32 x = 0; x < width; x++)
+    {
+        for(u32 y = 0; y < height; y++)
+        {
+            data.push_back(getRandom());
+            data.push_back(getRandom());
+            data.push_back(getRandom());
+        }
+    }
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data.data());
+}
+
+NoiseTexture::~NoiseTexture()
+{
+    glDeleteTextures(1, &texture);
+}
+
+void NoiseTexture::bind(unsigned int index) const
+{
+    glActiveTexture(GL_TEXTURE0 + index);
+    glBindTexture(GL_TEXTURE_2D, texture);
+}
+
+float NoiseTexture::getRandom() const
+{
+    float r = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
+    return r * 2.0f - 1.0f;
+}

+ 32 - 0
client/rendering/NoiseTexture.h

@@ -0,0 +1,32 @@
+#ifndef NOISETEXTURE_H
+#define NOISETEXTURE_H
+
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+#include "common/utils/Types.h"
+
+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;
+    NoiseTexture& operator=(NoiseTexture&& other) = delete;
+    
+    void bind(unsigned int index) const;
+    
+private:
+    float getRandom() const;
+    
+    u32 width;
+    u32 height;
+    
+    GLuint texture = 0;
+};
+
+#endif
+

+ 6 - 1
client/rendering/Shader.cpp

@@ -110,4 +110,9 @@ void Shader::use() const
 void Shader::setMatrix(const GLchar* name, const float* data) const
 {
     glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, data);
-}
+}
+
+void Shader::setInt(const GLchar* name, GLint data) const
+{
+    glUniform1i(glGetUniformLocation(program, name), data);
+}

+ 1 - 0
client/rendering/Shader.h

@@ -15,6 +15,7 @@ public:
     bool isValid() const;
     void use() const;
     void setMatrix(const GLchar* name, const float* data) const;
+    void setInt(const GLchar* name, GLint data) const;
     
 private:
     Shader(const Shader& other) = delete;

+ 2 - 5
client/rendering/Texture.cpp

@@ -2,7 +2,7 @@
 
 #include "client/rendering/Texture.h"
 
-Texture::Texture(const char* path)
+Texture::Texture(const char* path) : data(nullptr), texture(0)
 {
     load(path);
 }
@@ -13,10 +13,7 @@ Texture::~Texture()
     {
         delete[] data;
     }
-    if(texture != 0)
-    {
-        glDeleteTextures(1, &texture);
-    }
+    glDeleteTextures(1, &texture);
 }
 
 void Texture::load(const char* path)

+ 1 - 1
client/rendering/Texture.h

@@ -29,7 +29,7 @@ private:
     u32 height;
     u32* data;
     
-    GLuint texture = 0;
+    GLuint texture;
 };
 
 #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/GameClient.cpp', 'client/input/Keys.cpp', 'client/rendering/Shader.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/Framebuffer.cpp']
+sourcesClient = ['client/Main.cpp', 'client/GameClient.cpp', 'client/input/Keys.cpp', 'client/rendering/Shader.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/Framebuffer.cpp', 'client/rendering/NoiseTexture.cpp']
 
 sourcesTest = ['tests/Main.cpp', 'server/commands/CommandUtils.cpp']
 

+ 6 - 9
resources/shader/ssaoBlurFragment.fs

@@ -1,13 +1,9 @@
 #version 430
 
-layout (binding = 1) uniform sampler2D worldPositionSamp;
-layout (binding = 2) uniform sampler2D worldNormalSamp;
-layout (binding = 3) uniform sampler2D worldColorSamp;
-layout (binding = 4) uniform sampler2D worldDepthSamp;
-layout (binding = 5) uniform sampler2D noiseSamp;
-layout (binding = 6) uniform sampler2D ssaoSamp;
+layout (binding = 0) uniform sampler2D ssaoSamp;
+layout (binding = 1) uniform sampler2D test;
 
-in vec2 varTextureCoord;
+in vec2 varTex;
 out float color;
 
 void main()
@@ -20,9 +16,10 @@ void main()
         for(int y = -radius; y < radius; y++) 
         {
             vec2 offset = vec2(float(x), float(y)) * texelSize;
-            result += texture(ssaoSamp, varTextureCoord + offset).r;
+            result += texture(ssaoSamp, varTex + offset).r;
         }
     }
     result /= (radius * radius * 4);
-    color = result;
+    color = result * texture(test, varTex).r;
+    //color = texture(ssaoSamp, varTex).r;
 }  

+ 4 - 5
resources/shader/ssaoBlurVertex.vs

@@ -1,14 +1,13 @@
 #version 430
 
 layout (location = 0) in vec3 position;
-layout (location = 1) in vec4 color;
-layout (location = 2) in vec2 textureCoord;
-layout (location = 3) in vec3 normal;
+layout (location = 1) in vec2 tex;
+layout (location = 2) in vec3 normal;
 
-out vec2 varTextureCoord;
+out vec2 varTex;
 
 void main(void)
 { 
     gl_Position = vec4(position, 1.0);
-    varTextureCoord = textureCoord;
+    varTex = tex;
 }

+ 33 - 65
resources/shader/ssaoFragment.fs

@@ -1,82 +1,50 @@
 #version 430
 
-layout (binding = 1) uniform sampler2D worldPositionSamp;
-layout (binding = 2) uniform sampler2D worldNormalSamp;
-layout (binding = 3) uniform sampler2D worldColorSamp;
-layout (binding = 4) uniform sampler2D worldDepthSamp;
-layout (binding = 5) uniform sampler2D noiseSamp;
-layout (binding = 6) uniform sampler2DShadow worldShadowSamp;
-
-const float bias = 0.025;
-uniform float radius;
-
-// ssao kernel is pre generated for simplicity and performance
-// for(int i = 0; i < ssaoKernelAmount; i++)
-// {
-//     vec3 v = (random(-1, 1), random(-1, 1), random(0, 1));
-//     normalize(v);
-//     v *= random(0, 1);
-// 
-//     float scale = (float) i / ssaoKernelAmount; 
-//     scale  = 0.1f + (scale * scale) * 0.9f;
-//     v *= scale;
-// }
-
-uniform int numberOfSamples;
-const vec3 ssaoKernel[64] =
+layout (binding = 0) uniform sampler2D worldPositionSamp;
+layout (binding = 1) uniform sampler2D worldNormalSamp;
+layout (binding = 2) uniform sampler2D worldColorSamp;
+layout (binding = 3) uniform sampler2D worldDepthSamp;
+layout (binding = 4) uniform sampler2D ssaoNoise;
+
+const int numberOfSamples = 32;
+const vec3 ssaoKernel[32] =
 {
-    vec3(0.0261425, -0.0376851, 4.00204e-07), vec3(-0.0517141, -0.0320865, 0.0304131), vec3(-0.0108267, 0.040398, 0.0315651), vec3(-0.0314388, -0.0327688, 0.0292523), vec3(-0.00132874, -0.0056109, 0.00382464), vec3(0.0297163, 0.062378, 0.0697156), vec3(-0.0489338, 0.00322945, 0.0507385), vec3(0.0689938, 0.0338294, 0.0349743), 
-    vec3(0.0167713, -0.0321483, 0.00932237), vec3(0.0331278, 0.0172987, 0.0213404), vec3(0.0374994, 0.0812687, 0.0208025), vec3(0.0266481, -0.0865055, 0.0659467), vec3(0.0547641, -0.0130673, 0.0280206), vec3(-0.0110478, -0.01767, 0.00933272), vec3(0.00571618, 0.00555481, 0.00339805), vec3(0.001716, 0.000476389, 0.0476425), 
-    vec3(-0.00607237, -0.000156404, 0.0128097), vec3(6.96767e-05, -0.0420028, 0.0466964), vec3(0.00540731, 0.0752215, 0.0251832), vec3(0.0514841, -0.0885747, 0.0926251), vec3(-0.0847474, -0.0655798, 0.0724548), vec3(0.0660382, 0.0362147, 0.121362), vec3(-0.0245497, 0.0357539, 0.0459818), vec3(0.084097, 0.0122515, 0.161992), 
-    vec3(-0.0471186, 0.069756, 0.0420852), vec3(-0.0261632, -0.0039447, 0.0330319), vec3(0.00775989, 0.0018387, 0.00197227), vec3(0.167912, -0.000560453, 0.0985381), vec3(0.0578085, 0.180922, 0.128394), vec3(0.0217065, -0.0290399, 0.00807598), vec3(-0.111954, -0.0507763, 0.0102483), vec3(0.0672952, 0.0322446, 0.00206515), 
-    vec3(0.187537, -0.0884211, 0.0438314), vec3(-0.0661418, 0.105045, 0.0435408), vec3(0.0592117, -0.239374, 0.169697), vec3(0.00562892, 0.045657, 0.0295806), vec3(-0.126569, -0.0322389, 0.173734), vec3(-0.105291, -0.0336205, 0.369015), vec3(-0.133294, -0.221413, 0.0464706), vec3(0.207733, 0.25832, 0.108203), 
-    vec3(-0.0605341, -0.0128894, 0.0677724), vec3(-0.0119915, 0.0655307, 0.00231462), vec3(-0.262367, -0.063802, 0.337014), vec3(-0.144795, -0.114031, 0.0155575), vec3(0.141442, -0.182272, 0.0632041), vec3(0.437972, -0.0676212, 0.24775), vec3(0.196835, -0.312091, 0.357106), vec3(-0.0222231, 0.279056, 0.0967121), 
-    vec3(0.141587, 0.175742, 0.166133), vec3(-0.0631059, 0.138069, 0.494785), vec3(0.479211, 0.198913, 0.33888), vec3(0.0271384, -0.152772, 0.308639), vec3(0.369594, -0.414181, 0.249154), vec3(0.38887, 0.241946, 0.231701), vec3(0.0066864, 0.00571323, 0.0110748), vec3(-0.00410669, 0.0027978, 0.0496037), 
-    vec3(0.228858, -0.015091, 0.486861), vec3(0.470433, 0.535686, 0.128289), vec3(-0.0906902, -0.659009, 0.49676), vec3(-0.372504, -0.109116, 0.217853), vec3(-0.0523188, -0.0971253, 0.710221), vec3(0.116336, -0.0672402, 0.0425562), vec3(-0.000688899, 0.000160498, 0.00043486), vec3(-0.151823, 0.190876, 0.324663)
+    vec3( 0.001,  0.000, -0.000), vec3(-0.002,  0.002, -0.003), vec3( 0.004, -0.007,  0.002), vec3(-0.002,  0.012,  0.010), 
+    vec3(-0.012,  0.018, -0.011), vec3(-0.013,  0.023, -0.023), vec3(-0.005, -0.021,  0.043), vec3( 0.034, -0.039,  0.034), 
+    vec3(-0.063, -0.040,  0.027), vec3( 0.095, -0.025, -0.001), vec3( 0.048, -0.089,  0.062), vec3(-0.084,  0.041, -0.105), 
+    vec3(-0.148, -0.047, -0.056), vec3(-0.039,  0.026, -0.186), vec3(-0.161,  0.065, -0.135), vec3(-0.203,  0.123, -0.079), 
+    vec3( 0.216,  0.091, -0.157), vec3( 0.066,  0.305, -0.051), vec3(-0.084, -0.332,  0.083), vec3(-0.050,  0.351, -0.165), 
+    vec3(-0.330,  0.127, -0.245), vec3(-0.020,  0.319,  0.348), vec3(-0.396,  0.281, -0.178), vec3(-0.376, -0.413, -0.069), 
+    vec3( 0.510,  0.153, -0.298), vec3(-0.451, -0.476, -0.074), vec3(-0.596, -0.302,  0.245), vec3( 0.311, -0.490, -0.499), 
+    vec3(-0.439,  0.464,  0.517), vec3( 0.397, -0.557,  0.551), vec3( 0.632, -0.677,  0.151), vec3( 0.574, -0.246,  0.781)
 };
-uniform mat4 projMatrix;
-
-in vec2 varTextureCoord;
-out float color;
+uniform mat4 proj;
+uniform mat4 view;
 
 uniform int width;
 uniform int height;
+const float radius = 0.25;
+
+in vec2 varTex;
+out float color;
 
 void main()
 {     
-    vec3 fragPos = texture(worldPositionSamp, varTextureCoord).xyz;
-    vec3 normal = texture(worldNormalSamp, varTextureCoord).rgb;
-    vec3 randomVec = texture(noiseSamp, varTextureCoord * vec2(width / 4.0, height / 4.0)).xyz; 
-
-    vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
-    vec3 bitangent = cross(normal, tangent);
-    mat3 TBN = mat3(tangent, bitangent, normal);  
+    vec3 fragPos = texture(worldPositionSamp, varTex).xyz;
+    vec3 random = texture(ssaoNoise, varTex * vec2(width * 0.25, height * 0.25)).xyz * radius * 0.5; 
 
     float occlusion = 0.0;
     for(int i = 0; i < numberOfSamples; i++)
     {
-        vec3 sampl = TBN * ssaoKernel[i]; // From tangent to view-space
-        sampl = fragPos + sampl * radius; 
-
-        vec4 offset = vec4(sampl, 1.0);
-        offset = projMatrix * offset; // from view to clip-space
-        offset.xyz /= offset.w; // perspective divide
-        offset.xyz = offset.xyz * 0.5 + 0.5; // transform to range 0.0 - 1.0  
-
-        float sampleDepth = texture(worldPositionSamp, offset.xy).z; 
-
-        float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth));
-        occlusion += float(sampleDepth >= sampl.z + bias) * rangeCheck;   
-    }  
-    
-    occlusion /= numberOfSamples;
-    occlusion = 1 - occlusion;
-    color = occlusion;
+        vec4 projFragPos = proj * vec4(fragPos + ssaoKernel[i] * radius + random, 1.0);
+        projFragPos.xyz /= projFragPos.w;
+        projFragPos.xyz = projFragPos.xyz * 0.5 + 0.5;
 
-    //color = texture(worldDepthSamp, varTextureCoord).x;
+        float depth1 = texture(worldDepthSamp, projFragPos.xy).x;
+        float depth2 = projFragPos.z;
 
-    /*vec4 w = projMatrix * viewMatrix * vec4(texture(worldPositionSamp, varTextureCoord).xyz, 1.0);
-    w = w * 0.5 + 0.5;
-    float inShadow = textureProj(worldShadowSamp, w);
-    color = 1 - inShadow * 0.5;*/
+        float rangeCheck = float(abs(depth2 - depth1) < radius);
+        occlusion += float(depth2 > depth1) * rangeCheck;
+    }
+    color = 1 - occlusion / numberOfSamples;
 } 

+ 4 - 5
resources/shader/ssaoVertex.vs

@@ -1,14 +1,13 @@
 #version 430
 
 layout (location = 0) in vec3 position;
-layout (location = 1) in vec4 color;
-layout (location = 2) in vec2 textureCoord;
-layout (location = 3) in vec3 normal;
+layout (location = 1) in vec2 tex;
+layout (location = 2) in vec3 normal;
 
-out vec2 varTextureCoord;
+out vec2 varTex;
 
 void main(void)
 { 
     gl_Position = vec4(position, 1.0);
-    varTextureCoord = textureCoord;
+    varTex = tex;
 }

+ 2 - 2
resources/shader/test2Fragment.fs

@@ -8,6 +8,6 @@ out vec4 color;
 
 void main()
 {
-    color = texture(samp, varTex);
-    //color = vec4(1, 0, 1, 1);
+    float f = texture(samp, varTex).x;
+    color = vec4(f, f, f, 1.0);
 }  

+ 5 - 8
resources/shader/worldFragment.fs

@@ -6,20 +6,17 @@ layout (location = 2) out vec4 worldColor;
 
 layout (binding = 0) uniform sampler2D samp;
 
-uniform mat4 projMatrix;
-uniform mat4 viewMatrix;
-uniform mat4 modelMatrix;
+uniform mat4 proj;
+uniform mat4 view;
+uniform mat4 model;
 
 in vec3 varPosition;
-in vec2 varTextureCoord;
-in vec4 varColor;
+in vec2 varTex;
 in vec3 varNormal;
 
 void main(void)
 {
     worldPosition = varPosition;
     worldNormal = normalize(varNormal);
-    //worldColor = vec4(1, 0, 0, 1);
-    worldColor = texture(samp, varTextureCoord);
-    //worldColor = varColor;
+    worldColor = texture(samp, varTex);
 }

+ 12 - 27
resources/shader/worldVertex.vs

@@ -1,41 +1,26 @@
 #version 430
 
 layout (location = 0) in vec3 position;
-layout (location = 1) in vec4 color;
-layout (location = 2) in vec2 textureCoord;
-layout (location = 3) in vec3 normal;
+layout (location = 1) in vec2 tex;
+layout (location = 2) in vec3 normal;
 
-uniform mat4 projMatrix;
-uniform mat4 viewMatrix;
-uniform mat4 modelMatrix;
+uniform mat4 proj;
+uniform mat4 view;
+uniform mat4 model;
 
 out vec3 varPosition;
-out vec2 varTextureCoord;
-out vec4 varColor;
+out vec2 varTex;
 out vec3 varNormal;
 
 void main(void)
 { 
     // transforming normals must not use the fourth dimension
-    varNormal = (viewMatrix * modelMatrix * vec4(normal, 0.0)).xyz;
+    // should be the inverse transposed matrix
+    varNormal = (view * model * vec4(normal, 0.0)).xyz;
     
-    varTextureCoord = textureCoord; 
-    varColor = vec4(color.xyz, 1);
-
-    /*if(abs(normal.x) == 1)
-    {
-        varColor = varColor * 0.8;
-    }
-    if(abs(normal.z) == 1)
-    {
-        varColor = varColor * 0.9;
-    }*/
+    varTex = tex; 
     
-    vec4 viewPos = viewMatrix * modelMatrix * vec4(position, 1.0);
-    varPosition = viewPos.xyz;
-    gl_Position = projMatrix * viewPos;
-
-    /*vec4 modelPos = modelMatrix * vec4(position, 1.0);
-    varPosition = modelPos.xyz;
-    gl_Position = projMatrix * viewMatrix * modelPos;*/
+    vec4 worldPos = view * model * vec4(position, 1.0);
+    varPosition = worldPos.xyz;
+    gl_Position = proj * worldPos;
 }