Browse Source

smooth shadow experiments

Kajetan Johannes Hammerle 3 years ago
parent
commit
4c9282f3e0

+ 65 - 57
client/Game.cpp

@@ -7,76 +7,84 @@ Game::Game() : lengthAngle(0.0f), widthAngle(0.0f), texture("resources/textures.
         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);
+                    addCube(x - 3, y - 5, z - 10,
+                            x * x + (y - 1) * (y - 1) + z * z >= 16,
+                            x * x + (y + 1) * (y + 1) + z * z >= 16 && !(x == 0 && y == 3 && z == 0),
+                            (x - 1) * (x - 1) + y * y + z * z >= 16,
+                            (x + 1) * (x + 1) + y * y + z * z >= 16,
+                            x * x + y * y + (z + 1) * (z + 1) >= 16,
+                            x * x + y * y + (z - 1) * (z - 1) >= 16);
                 }
             }
         }
     }
-    addCube(-3, -1, -10);
+    addCube(-3, -1, -10, false, true, true, true, true, true);
+    pos.set(-3, 0, -10);
     m.build();
 }
 
-void Game::addCube(float x, float y, float z) {
+void Game::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;
-    // bottom
-    m.add( {x - ERROR, y - ERROR, z - ERROR, 0.125f, 0.0f, 0, -1, 0});
-    m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
-    m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
-
-    m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
-    m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, -1, 0});
-    m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
-
-    // top
-    m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 1, 0});
-    m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
-
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
-    m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.3125f, 0.0625f, 0, 1, 0});
-
-    // left
-    m.add( {x - ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, -1, 0, 0});
-    m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
-    m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
-
-    m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
-    m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, -1, 0, 0});
-    m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
-
-    // right
-    m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 1, 0, 0});
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
-    m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
-
-    m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 1, 0, 0});
-
-    // front
-    m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, 0, 1});
-    m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
-    m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
-
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, 0, 0, 1});
-    m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
-    m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
-
-    // back
-    m.add( {x - ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 0, 0, -1});
-    m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
-    m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
-
-    m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, 0, 0, -1});
-    m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
-    m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
+    if(bottom) {
+        m.add( {x - ERROR, y - ERROR, z - ERROR, 0.125f, 0.0f, 0, -1, 0});
+        m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
+        m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
+
+        m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
+        m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, -1, 0});
+        m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
+    }
+    if(top) {
+        m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 1, 0});
+        m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
+
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
+        m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.3125f, 0.0625f, 0, 1, 0});
+    }
+    if(left) {
+        m.add( {x - ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, -1, 0, 0});
+        m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
+        m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
+
+        m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
+        m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, -1, 0, 0});
+        m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
+    }
+    if(right) {
+        m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 1, 0, 0});
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
+        m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
+
+        m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 1, 0, 0});
+    }
+    if(front) {
+        m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, 0, 1});
+        m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
+        m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
+
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, 0, 0, 1});
+        m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
+        m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
+    }
+    if(back) {
+        m.add( {x - ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 0, 0, -1});
+        m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
+        m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
+
+        m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, 0, 0, -1});
+        m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
+        m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
+    }
 }
 
 void Game::tick(const Keys& keys, const MouseButtons& mButtons, Camera& cam) {
     (void) mButtons;
 
-    const float speed = 0.5f;
+    const float speed = 0.25f;
     if(keys.down.isDown()) {
         pos.addMul(cam.getFlatBack(), speed);
     }

+ 1 - 1
client/Game.h

@@ -19,7 +19,7 @@ public:
     void renderTextOverlay(float lag, MatrixStack& stack, Shader& sh, FontRenderer& fr);
 
 private:
-    void addCube(float x, float y, float z);
+    void addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back);
 
     float lengthAngle;
     float widthAngle;

+ 21 - 11
client/GameClient.cpp

@@ -50,6 +50,9 @@ static Keys keys;
 static MouseButtons mButtons;
 static bool useSSAO = false;
 
+static float testRadius = 0.005;
+static float testBias = 0.00025;
+
 struct Shaders {
 
     Shaders() :
@@ -154,7 +157,7 @@ struct Framebuffers {
     world(w, h, Framebuffer::POSITION | Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::RED | Framebuffer::DEPTH24_STENCIL8, 0),
     ssao(w, h, Framebuffer::RED, 0),
     ssaoBlur(w, h, Framebuffer::RED, 0),
-    shadow(w, h, Framebuffer::DEPTH24_STENCIL8, GL_LEQUAL) {
+    shadow(w, h, Framebuffer::DEPTH24_STENCIL8, 0 /*GL_LEQUAL*/) {
     }
 
     void resize(u32 w, u32 h) const {
@@ -274,8 +277,19 @@ static void tick(InternGame& game) {
     keys.tick();
     mButtons.tick();
     game.game.tick(keys, mButtons, game.cam);
-    if(keys.test.getDownTime() == 1) {
-        useSSAO = !useSSAO;
+    if(keys.test.isDown()) {
+        //useSSAO = !useSSAO;
+        //useSSAO = true;
+        testRadius /= 0.95f;
+    }
+    if(keys.test2.isDown()) {
+        testRadius *= 0.95f;
+    }
+    if(keys.test3.isDown()) {
+        testBias /= 0.95f;
+    }
+    if(keys.test4.isDown()) {
+        testBias *= 0.95f;
     }
     mButtons.postTick();
 }
@@ -287,14 +301,8 @@ static void renderShadow(float lag, Shaders& shaders, InternGame& game, Framebuf
     shaders.worldShadowProjView = shaders.worldShadowProj;
     shaders.worldShadowProjView.mul(shaders.worldShadowView);
     shaders.shadow.setMatrix("projView", shaders.worldShadowProjView.getValues());
-
-    //glEnable(GL_CULL_FACE);
-    //glCullFace(GL_FRONT);
-    //glEnable(GL_POLYGON_OFFSET_FILL);
-    //glPolygonOffset(2.0f, 4.0f);
+    
     game.game.renderWorld(lag, game.model, shaders.shadow);
-    //glCullFace(GL_BACK);
-    //glDisable(GL_POLYGON_OFFSET_FILL);
 }
 
 static void renderWorld(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) {
@@ -313,6 +321,8 @@ static void renderWorld(float lag, Shaders& shaders, InternGame& game, Framebuff
     game.model.clear();
     shaders.world.setMatrix("model", game.model.get().getValues());
     fb.shadow.bindDepthTexture(1);
+    shaders.world.setFloat("radius", testRadius);
+    shaders.world.setFloat("bias", testBias);
     game.game.renderWorld(lag, game.model, shaders.world);
 }
 
@@ -376,7 +386,7 @@ static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game) {
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glBlendEquation(GL_FUNC_ADD);
     char buffer[50];
-    snprintf(buffer, 50, "FPS: %f", 128000000000.0f / sum);
+    snprintf(buffer, 50, "FPS: %f %f %f", 128000000000.0f / sum, testRadius, testBias);
     game.fontRenderer.drawString(20, 20, buffer);
     game.game.renderTextOverlay(lag, game.model, shaders.text, game.fontRenderer);
     glDisable(GL_BLEND);

+ 5 - 1
client/input/Keys.cpp

@@ -21,7 +21,8 @@ std::ostream& operator<<(std::ostream& os, const Keys::Key& k) {
 }
 
 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]), test(keys[10]) {
+camLeft(keys[6]), camRight(keys[7]), camUp(keys[8]), camDown(keys[9]), test(keys[10]), test2(keys[11]), test3(keys[12]),
+test4(keys[13]) {
     keys[0].glfwKey = GLFW_KEY_A;
     keys[1].glfwKey = GLFW_KEY_D;
     keys[2].glfwKey = GLFW_KEY_W;
@@ -33,6 +34,9 @@ camLeft(keys[6]), camRight(keys[7]), camUp(keys[8]), camDown(keys[9]), test(keys
     keys[8].glfwKey = GLFW_KEY_UP;
     keys[9].glfwKey = GLFW_KEY_DOWN;
     keys[10].glfwKey = GLFW_KEY_T;
+    keys[11].glfwKey = GLFW_KEY_Y;
+    keys[12].glfwKey = GLFW_KEY_U;
+    keys[13].glfwKey = GLFW_KEY_I;
 }
 
 void Keys::release(int key) {

+ 4 - 1
client/input/Keys.h

@@ -31,7 +31,7 @@ public:
     };
 
 private:
-    Key keys[11];
+    Key keys[14];
 
 public:
     Keys();
@@ -46,6 +46,9 @@ public:
     const Key& camUp;
     const Key& camDown;
     const Key& test;
+    const Key& test2;
+    const Key& test3;
+    const Key& test4;
 
     void release(int key);
     void press(int key);

+ 29 - 37
client/rendering/FontRenderer.cpp

@@ -2,62 +2,54 @@
 
 const size_t FontRenderer::BUFFER_LENGTH = 8 * 1024 * 1024;
 
-FontRenderer::FontRenderer() : tex("resources/font8x8.png"), offset(BUFFER_LENGTH), vba(0), vbo(0)
-{
+FontRenderer::FontRenderer() : tex("resources/font8x8.png"), offset(BUFFER_LENGTH), vba(0), vbo(0) {
     glGenVertexArrays(1, &vba);
     glBindVertexArray(vba);
-    
+
     glGenBuffers(1, &vbo);
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
-    
-    glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(float) * 7, static_cast<float*>(0));
+
+    glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0));
     glEnableVertexAttribArray(0);
 
-    glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(float) * 7, static_cast<float*>(0) + 2);
-    glEnableVertexAttribArray(1);  
-    
-    glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof(float) * 7, static_cast<float*>(0) + 4);
+    glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0) + 2);
+    glEnableVertexAttribArray(1);
+
+    glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0) + 4);
     glEnableVertexAttribArray(2);
 }
 
-FontRenderer::~FontRenderer()
-{
+FontRenderer::~FontRenderer() {
     glDeleteVertexArrays(1, &vba);
     glDeleteBuffers(1, &vbo);
 }
 
-void FontRenderer::drawString(float x, float y, const char* text)
-{
+void FontRenderer::drawString(float x, float y, const char* text) {
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
-    
+
     const size_t maxIndex = 256;
-    const size_t maxLength = maxIndex * 4 * sizeof(float) * 7;
-    
-    if(offset + maxLength >= BUFFER_LENGTH)
-    {
+    const size_t 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)
-    {
+
+    float* buffer = static_cast<float*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, maxLength, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
+    if(buffer == nullptr) {
         return;
     }
-    
+
     size_t index = 0;
     size_t i = 0;
     float r = 1.0f;
     float g = 1.0f;
     float b = 1.0f;
-    
-    while(text[index] != '\0' && index < maxIndex)
-    {
+
+    while(text[index] != '\0' && index < maxIndex) {
         char c = text[index];
-        if(c == '&')
-        {
-            if(text[index + 1] == '\0' || text[index + 2] == '\0' || text[index + 3] == '\0')
-            {
+        if(c == '&') {
+            if(text[index + 1] == '\0' || text[index + 2] == '\0' || text[index + 3] == '\0') {
                 break;
             }
             r = (text[index + 1] - '0') * (1.0f / 9.0f);
@@ -71,7 +63,7 @@ void FontRenderer::drawString(float x, float y, const char* text)
         float minY = (c >> 4) * (1.0f / 16.0f);
         float maxX = minX + (1.0f / 16.0f);
         float maxY = minY + (1.0f / 16.0f);
-        
+
         buffer[i++] = x;
         buffer[i++] = y;
         buffer[i++] = minX;
@@ -79,7 +71,7 @@ void FontRenderer::drawString(float x, float y, const char* text)
         buffer[i++] = r;
         buffer[i++] = g;
         buffer[i++] = b;
-        
+
         buffer[i++] = x;
         buffer[i++] = y + 8;
         buffer[i++] = minX;
@@ -87,7 +79,7 @@ void FontRenderer::drawString(float x, float y, const char* text)
         buffer[i++] = r;
         buffer[i++] = g;
         buffer[i++] = b;
-        
+
         buffer[i++] = x + 8;
         buffer[i++] = y;
         buffer[i++] = maxX;
@@ -95,7 +87,7 @@ void FontRenderer::drawString(float x, float y, const char* text)
         buffer[i++] = r;
         buffer[i++] = g;
         buffer[i++] = b;
-        
+
         buffer[i++] = x + 8;
         buffer[i++] = y + 8;
         buffer[i++] = maxX;
@@ -103,14 +95,14 @@ void FontRenderer::drawString(float x, float y, const char* text)
         buffer[i++] = r;
         buffer[i++] = g;
         buffer[i++] = b;
-        
+
         x += 8;
         index++;
     }
-    
+
     glUnmapBuffer(GL_ARRAY_BUFFER);
     glBindVertexArray(vba);
     tex.bind(0);
-    glDrawArrays(GL_TRIANGLE_STRIP, offset / (sizeof(float) * 7), i / 7);
+    glDrawArrays(GL_TRIANGLE_STRIP, offset / (sizeof (float) * 7), i / 7);
     offset += maxLength;
 }

+ 5 - 6
client/rendering/FontRenderer.h

@@ -6,24 +6,23 @@
 
 #include "client/rendering/Texture.h"
 
-class FontRenderer final
-{
+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:
     Texture tex;
     static const size_t BUFFER_LENGTH;
     size_t offset;
-    
+
     GLuint vba;
     GLuint vbo;
 };

+ 37 - 60
client/rendering/Framebuffer.cpp

@@ -3,16 +3,14 @@
 #include "Framebuffer.h"
 #include "client/Utils.h"
 
-Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc) : mode(mode), buffer(0), valid(false)
-{
+Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc) : mode(mode), buffer(0), valid(false) {
     glGenFramebuffers(1, &buffer);
     glBindFramebuffer(GL_FRAMEBUFFER, buffer);
-    
+
     GLuint attachments[4];
     u32 counter = 0;
-    
-    if(mode & POSITION) // position texture
-    {
+
+    if(mode & POSITION) {
         glGenTextures(1, &(textures[0]));
         glBindTexture(GL_TEXTURE_2D, textures[0]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
@@ -20,13 +18,12 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[0], 0); 
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[0], 0);
         attachments[counter] = GL_COLOR_ATTACHMENT0 + counter;
         counter++;
     }
-      
-    if(mode & NORMAL) // normal texture
-    {
+
+    if(mode & NORMAL) {
         glGenTextures(1, &(textures[1]));
         glBindTexture(GL_TEXTURE_2D, textures[1]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
@@ -34,13 +31,12 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[1], 0);  
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[1], 0);
         attachments[counter] = GL_COLOR_ATTACHMENT0 + counter;
         counter++;
     }
-    
-    if(mode & COLOR) // color texture
-    {
+
+    if(mode & COLOR) {
         glGenTextures(1, &(textures[2]));
         glBindTexture(GL_TEXTURE_2D, textures[2]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
@@ -48,13 +44,12 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[2], 0);  
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[2], 0);
         attachments[counter] = GL_COLOR_ATTACHMENT0 + counter;
         counter++;
     }
-    
-    if(mode & RED) // single color channel texture
-    {
+
+    if(mode & RED) {
         glGenTextures(1, &textures[3]);
         glBindTexture(GL_TEXTURE_2D, textures[3]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
@@ -62,13 +57,12 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[3], 0);  
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[3], 0);
         attachments[counter] = GL_COLOR_ATTACHMENT0 + counter;
         counter++;
     }
-    
-    if(mode & DEPTH24_STENCIL8) // depth texture
-    {
+
+    if(mode & DEPTH24_STENCIL8) {
         glGenTextures(1, &textures[4]);
         glBindTexture(GL_TEXTURE_2D, textures[4]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
@@ -76,99 +70,82 @@ Framebuffer::Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-        if(textureCompareFunc != 0)
-        {
+        if(textureCompareFunc != 0) {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, textureCompareFunc);
         }
         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, textures[4], 0);
     }
-    
-    glDrawBuffers(counter, attachments);   
-    
-    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
-    {
-	std::cout << "frame buffer is not complete\n";
+
+    glDrawBuffers(counter, attachments);
+
+    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
+        std::cout << "frame buffer is not complete\n";
         return;
     }
     valid = true;
 }
 
-Framebuffer::~Framebuffer()
-{
+Framebuffer::~Framebuffer() {
     glDeleteFramebuffers(1, &buffer);
-    for(GLuint& texture : textures)
-    {
+    for(GLuint& texture : textures) {
         glDeleteTextures(1, &texture);
     }
 }
 
-bool Framebuffer::isValid() const
-{
+bool Framebuffer::isValid() const {
     return valid;
 }
 
-void Framebuffer::bind() const
-{
+void Framebuffer::bind() const {
     glBindFramebuffer(GL_FRAMEBUFFER, buffer);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
 }
 
-void Framebuffer::resize(u32 width, u32 height) const
-{
-    if(mode & POSITION) // position texture
-    {
+void Framebuffer::resize(u32 width, u32 height) const {
+    if(mode & POSITION) {
         glBindTexture(GL_TEXTURE_2D, textures[0]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
     }
-    if(mode & NORMAL) // normal texture
-    {
+    if(mode & NORMAL) {
         glBindTexture(GL_TEXTURE_2D, textures[1]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
     }
-    if(mode & COLOR) // color texture
-    {
+    if(mode & COLOR) {
         glBindTexture(GL_TEXTURE_2D, textures[2]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
     }
-    if(mode & RED) // single color channel texture
-    {
+    if(mode & RED) {
         glBindTexture(GL_TEXTURE_2D, textures[3]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
     }
-    if(mode & DEPTH24_STENCIL8) // depth texture
-    {
+    if(mode & DEPTH24_STENCIL8) {
         glBindTexture(GL_TEXTURE_2D, textures[4]);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
     }
 }
 
-void Framebuffer::bindPositionTexture(u32 textureUnit) const
-{
+void Framebuffer::bindPositionTexture(u32 textureUnit) const {
     glActiveTexture(GL_TEXTURE0 + textureUnit);
     glBindTexture(GL_TEXTURE_2D, textures[0]);
 }
 
-void Framebuffer::bindNormalTexture(u32 textureUnit) const
-{
+void Framebuffer::bindNormalTexture(u32 textureUnit) const {
     glActiveTexture(GL_TEXTURE0 + textureUnit);
     glBindTexture(GL_TEXTURE_2D, textures[1]);
 }
 
-void Framebuffer::bindColorTexture(u32 textureUnit) const
-{
+void Framebuffer::bindColorTexture(u32 textureUnit) const {
     glActiveTexture(GL_TEXTURE0 + textureUnit);
     glBindTexture(GL_TEXTURE_2D, textures[2]);
 }
 
-void Framebuffer::bindRedTexture(u32 textureUnit) const
-{
+void Framebuffer::bindRedTexture(u32 textureUnit) const {
     glActiveTexture(GL_TEXTURE0 + textureUnit);
     glBindTexture(GL_TEXTURE_2D, textures[3]);
 }
 
-void Framebuffer::bindDepthTexture(u32 textureUnit) const
-{
+void Framebuffer::bindDepthTexture(u32 textureUnit) const {
     glActiveTexture(GL_TEXTURE0 + textureUnit);
     glBindTexture(GL_TEXTURE_2D, textures[4]);
 }

+ 6 - 7
client/rendering/Framebuffer.h

@@ -6,29 +6,28 @@
 
 #include "common/utils/Types.h"
 
-class Framebuffer final
-{
+class Framebuffer final {
 public:
     static const u32 POSITION = 1;
     static const u32 NORMAL = 2;
     static const u32 COLOR = 4;
     static const u32 RED = 8;
     static const u32 DEPTH24_STENCIL8 = 16;
-    
+
     Framebuffer(u32 width, u32 height, u32 mode, int textureCompareFunc);
     ~Framebuffer();
-    
+
     bool isValid() const;
     void bind() const;
-    
+
     void resize(u32 width, u32 height) const;
-    
+
     void bindPositionTexture(u32 textureUnit) const;
     void bindNormalTexture(u32 textureUnit) const;
     void bindColorTexture(u32 textureUnit) const;
     void bindRedTexture(u32 textureUnit) const;
     void bindDepthTexture(u32 textureUnit) const;
-    
+
 private:
     Framebuffer(const Framebuffer& other) = delete;
     Framebuffer(Framebuffer&& other) = delete;

+ 13 - 19
client/rendering/Mesh.cpp

@@ -1,47 +1,41 @@
 #include "client/rendering/Mesh.h"
 
-Mesh::Mesh() : vba(0), vbo(0)
-{
+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));
+    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);
+    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()
-{
+Mesh::~Mesh() {
     glDeleteVertexArrays(1, &vba);
     glDeleteBuffers(1, &vbo);
 }
 
-void Mesh::add(const VertexData& data)
-{
+void Mesh::add(const VertexData& data) {
     buffer.push_back(data);
 }
 
-void Mesh::clear()
-{
+void Mesh::clear() {
     buffer.clear();
 }
 
-void Mesh::build() const
-{
+void Mesh::build() const {
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
-    glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * buffer.size(), buffer.data(), GL_STATIC_DRAW);
+    glBufferData(GL_ARRAY_BUFFER, sizeof (VertexData) * buffer.size(), buffer.data(), GL_STATIC_DRAW);
 }
 
-void Mesh::draw() const
-{
+void Mesh::draw() const {
     glBindVertexArray(vba);
     glDrawArrays(GL_TRIANGLES, 0, buffer.size());
 }

+ 8 - 9
client/rendering/Mesh.h

@@ -4,11 +4,10 @@
 #include <GL/glew.h>
 #include <vector>
 
-class Mesh final
-{
+class Mesh final {
 public:
-    struct VertexData final
-    {
+
+    struct VertexData final {
         float x;
         float y;
         float z;
@@ -18,22 +17,22 @@ public:
         float ny;
         float nz;
     };
-    
+
     Mesh();
     ~Mesh();
-    
+
     void add(const VertexData& data);
-    
+
     void clear();
     void build() const;
     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;
     std::vector<VertexData> buffer;

+ 8 - 14
client/rendering/NoiseTexture.cpp

@@ -5,21 +5,18 @@
 
 #include "client/rendering/NoiseTexture.h"
 
-NoiseTexture::NoiseTexture(u32 width, u32 height)
-{
+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++)
-        {
+    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());
@@ -28,19 +25,16 @@ NoiseTexture::NoiseTexture(u32 width, u32 height)
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data.data());
 }
 
-NoiseTexture::~NoiseTexture()
-{
+NoiseTexture::~NoiseTexture() {
     glDeleteTextures(1, &texture);
 }
 
-void NoiseTexture::bind(unsigned int index) const
-{
+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);
+float NoiseTexture::getRandom() const {
+    float r = static_cast<float> (rand()) / static_cast<float> (RAND_MAX);
     return r * 2.0f - 1.0f;
 }

+ 6 - 7
client/rendering/NoiseTexture.h

@@ -6,25 +6,24 @@
 
 #include "common/utils/Types.h"
 
-class NoiseTexture final
-{
+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;
 };
 

+ 27 - 40
client/rendering/Shader.cpp

@@ -4,39 +4,34 @@
 #include "client/rendering/Shader.h"
 #include "client/Utils.h"
 
-Shader::Shader(const GLchar* vPath, const GLchar* fPath) : valid(false), 
-        vShader(0), fShader(0), program(0)
-{
+Shader::Shader(const GLchar* vPath, const GLchar* fPath) : valid(false),
+vShader(0), fShader(0), program(0) {
     std::vector<GLchar> vCode;
     std::vector<GLchar> fCode;
     bool vCheck = readFile(vCode, vPath);
     bool fCheck = readFile(fCode, fPath);
-    if(vCheck || fCheck)
-    {
+    if(vCheck || fCheck) {
         return;
     }
-    
+
     vCheck = compileShader(vShader, vCode.data(), GL_VERTEX_SHADER);
     fCheck = compileShader(fShader, fCode.data(), GL_FRAGMENT_SHADER);
-    if(vCheck || fCheck)
-    {
+    if(vCheck || fCheck) {
         return;
     }
-    
+
     program = glCreateProgram();
     glAttachShader(program, vShader);
     glAttachShader(program, fShader);
     glLinkProgram(program);
-    
-    if(checkAndPrintError("shader linking error"))
-    {
+
+    if(checkAndPrintError("shader linking error")) {
         return;
     }
 
     GLint linked;
     glGetProgramiv(program, GL_LINK_STATUS, &linked);
-    if(linked == GL_FALSE)
-    {
+    if(linked == GL_FALSE) {
         GLchar buffer[512];
         glGetProgramInfoLog(program, 512, nullptr, buffer);
         std::cout << "programm linking info log: " << buffer << "\n";
@@ -45,32 +40,26 @@ Shader::Shader(const GLchar* vPath, const GLchar* fPath) : valid(false),
     valid = true;
 }
 
-Shader::~Shader()
-{
+Shader::~Shader() {
     glDeleteProgram(program);
     glDeleteShader(vShader);
     glDeleteShader(fShader);
 }
 
-bool Shader::isValid() const
-{
+bool Shader::isValid() const {
     return valid;
 }
 
-bool Shader::readFile(std::vector<GLchar>& buffer, const GLchar* path) const
-{
+bool Shader::readFile(std::vector<GLchar>& buffer, const GLchar* path) const {
     std::ifstream in;
     in.open(path);
-    if(in.fail())
-    {
-        std::cout << "cannot read shader file: '" << path << "'\n"; 
+    if(in.fail()) {
+        std::cout << "cannot read shader file: '" << path << "'\n";
         return true;
     }
-    while(true)
-    {
+    while(true) {
         GLchar c = in.get();
-        if(in.eof())
-        {
+        if(in.eof()) {
             break;
         }
         buffer.push_back(c);
@@ -79,21 +68,18 @@ bool Shader::readFile(std::vector<GLchar>& buffer, const GLchar* path) const
     return false;
 }
 
-bool Shader::compileShader(GLuint& shader, const GLchar* code, GLenum shaderType) const
-{
+bool Shader::compileShader(GLuint& shader, const GLchar* code, GLenum shaderType) const {
     shader = glCreateShader(shaderType);
     glShaderSource(shader, 1, &code, nullptr);
     glCompileShader(shader);
 
-    if(checkAndPrintError("shader error"))
-    {
+    if(checkAndPrintError("shader error")) {
         return true;
     }
-    
+
     GLint compiled;
     glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
-    if(compiled == GL_FALSE)
-    {
+    if(compiled == GL_FALSE) {
         GLchar buffer[512];
         glGetShaderInfoLog(shader, 512, nullptr, buffer);
         std::cout << "shader info log: " << buffer << "\n";
@@ -102,17 +88,18 @@ bool Shader::compileShader(GLuint& shader, const GLchar* code, GLenum shaderType
     return false;
 }
 
-void Shader::use() const
-{
+void Shader::use() const {
     glUseProgram(program);
 }
 
-void Shader::setMatrix(const GLchar* name, const float* data) const
-{
+void Shader::setMatrix(const GLchar* name, const GLfloat* data) const {
     glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, data);
 }
 
-void Shader::setInt(const GLchar* name, GLint data) const
-{
+void Shader::setInt(const GLchar* name, GLint data) const {
     glUniform1i(glGetUniformLocation(program, name), data);
 }
+
+void Shader::setFloat(const GLchar* name, GLfloat data) const {
+    glUniform1f(glGetUniformLocation(program, name), data);
+}

+ 7 - 8
client/rendering/Shader.h

@@ -6,31 +6,30 @@
 #include <iostream>
 #include <vector>
 
-class Shader final
-{
+class Shader final {
 public:
     Shader(const GLchar* vPath, const GLchar* fPath);
     ~Shader();
 
     bool isValid() const;
     void use() const;
-    void setMatrix(const GLchar* name, const float* data) const;
+    void setMatrix(const GLchar* name, const GLfloat* data) const;
     void setInt(const GLchar* name, GLint data) const;
-    
+    void setFloat(const GLchar* name, GLfloat data) const;
+
 private:
     Shader(const Shader& other) = delete;
     Shader(Shader&& other) = delete;
     Shader& operator=(const Shader& other) = delete;
     Shader& operator=(Shader&& other) = delete;
-    
+
     bool readFile(std::vector<GLchar>& buffer, const GLchar* path) const;
     bool compileShader(GLuint& shader, const GLchar* code, GLenum shaderType) const;
-    
+
     bool valid;
     GLuint vShader;
     GLuint fShader;
     GLuint program;
 };
 
-#endif
-
+#endif

+ 29 - 45
client/rendering/Texture.cpp

@@ -2,74 +2,61 @@
 
 #include "client/rendering/Texture.h"
 
-Texture::Texture(const char* path) : data(nullptr), texture(0)
-{
+Texture::Texture(const char* path) : data(nullptr), texture(0) {
     load(path);
 }
 
-Texture::~Texture()
-{
-    if(data != nullptr)
-    {
+Texture::~Texture() {
+    if(data != nullptr) {
         delete[] data;
     }
     glDeleteTextures(1, &texture);
 }
 
-void Texture::load(const char* path)
-{
+void Texture::load(const char* path) {
     FILE* file = fopen(path, "r");
-    if(file == nullptr)
-    {
+    if(file == nullptr) {
         std::cout << "texture '" << path << "' does not exist\n";
         return;
     }
     bool b = load(path, file);
     fclose(file);
-    if(b)
-    {
+    if(b) {
         initGL();
     }
 }
 
-bool Texture::load(const char* path, FILE* file)
-{
+bool Texture::load(const char* path, FILE* file) {
     // check signature of png
     unsigned char buffer[8];
-    if(fread(buffer, sizeof(char), 8, file) != 8)
-    {
+    if(fread(buffer, sizeof (char), 8, file) != 8) {
         std::cout << "cannot read signature of texture '" << path << "'\n";
         return false;
     }
-    
-    if(png_sig_cmp(buffer, 0, 8))        
-    {
+
+    if(png_sig_cmp(buffer, 0, 8)) {
         std::cout << "file '" << path << "' is not a texture\n";
         return false;
     }
-    
+
     // create structures for data
     png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
-    if(png == nullptr)
-    {
+    if(png == nullptr) {
         std::cout << "cannot create texture data structure\n";
         return false;
     }
-    
+
     png_infop info = png_create_info_struct(png);
-    if(info == nullptr)
-    {
-       std::cout << "cannot create image info structure\n";
-       return false;
+    if(info == nullptr) {
+        std::cout << "cannot create image info structure\n";
+        return false;
     }
 
     u32** rowPointers = nullptr;
-    
+
     // set callback for errors
-    if(setjmp(png_jmpbuf(png)))
-    {
-        if(rowPointers != nullptr)
-        {
+    if(setjmp(png_jmpbuf(png))) {
+        if(rowPointers != nullptr) {
             png_free(png, rowPointers);
         }
         png_destroy_read_struct(&png, &info, nullptr);
@@ -81,35 +68,33 @@ bool Texture::load(const char* path, FILE* file)
     png_init_io(png, file);
     // notify about already used signature bytes
     png_set_sig_bytes(png, 8);
-    
+
     // read info data
     png_read_info(png, info);
     width = png_get_image_width(png, info);
     height = png_get_image_height(png, info);
-    
+
     // read image data
     data = new u32[width * height];
-    
+
     // allocate and set row pointer to correct places in block
-    rowPointers = (u32**) png_malloc(png, height * (sizeof(u32*)));
-    for(unsigned int i = 0; i < height; i++)
-    {
+    rowPointers = (u32**) png_malloc(png, height * (sizeof (u32*)));
+    for(unsigned int i = 0; i < height; i++) {
         rowPointers[i] = (data + i * width);
     }
     png_set_rows(png, info, (png_bytepp) rowPointers);
-    
+
     png_read_image(png, (png_bytepp) rowPointers);
-    
+
     png_free(png, rowPointers);
     png_destroy_read_struct(&png, &info, NULL);
     return true;
 }
 
-void Texture::initGL()
-{
+void Texture::initGL() {
     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);
@@ -120,8 +105,7 @@ void Texture::initGL()
     data = nullptr;
 }
 
-void Texture::bind(unsigned int index) const
-{
+void Texture::bind(unsigned int index) const {
     glActiveTexture(GL_TEXTURE0 + index);
     glBindTexture(GL_TEXTURE_2D, texture);
 }

+ 7 - 10
client/rendering/Texture.h

@@ -7,31 +7,28 @@
 
 #include "common/utils/Types.h"
 
-class Texture final
-{
+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;
-    
+
 private:
     void load(const char* path);
     bool load(const char* path, FILE* file);
     void initGL();
-    
+
     u32 width;
     u32 height;
     u32* data;
-    
+
     GLuint texture;
 };
 
-#endif
-
-
+#endif

+ 3 - 2
common/utils/Types.h

@@ -3,6 +3,8 @@
 
 #include <cstdint>
 
+typedef unsigned int uint;
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
@@ -13,5 +15,4 @@ typedef int16_t s16;
 typedef int32_t s32;
 typedef int64_t s64;
 
-#endif
-
+#endif

+ 54 - 2
resources/shader/worldFragment.fs

@@ -6,21 +6,73 @@ layout (location = 2) out vec4 worldColor;
 layout (location = 3) out float worldShadow;
 
 layout (binding = 0) uniform sampler2D samp;
-layout (binding = 1) uniform sampler2DShadow shadowSamp;
+//layout (binding = 1) uniform sampler2DShadow shadowSamp;
+layout (binding = 1) uniform sampler2D shadowSamp;
 
 uniform mat4 proj;
 uniform mat4 view;
 uniform mat4 model;
 
+uniform mat4 projViewShadow;
+
 in vec3 varPosition;
 in vec2 varTex;
 in vec3 varNormal;
 in vec4 varShadow;
 
+uniform float radius;
+uniform float bias;
+
+const int sampleAmount = 64;
+const vec2 samples[64] = {
+    vec2(-0.000, -0.000), vec2(-0.007, 0.004), vec2(0.016, 0.001), vec2(-0.015, -0.018), 
+    vec2(-0.024, -0.020), vec2(0.036, -0.014), vec2(-0.015, -0.045), vec2(0.037, 0.041), 
+    vec2(-0.036, -0.051), vec2(-0.069, 0.014), vec2(0.068, -0.039), vec2(0.086, -0.001), 
+    vec2(0.062, -0.070), vec2(-0.066, -0.077), vec2(-0.010, 0.109), vec2(-0.013, 0.117), 
+    vec2(-0.086, 0.091), vec2(0.019, 0.131), vec2(-0.051, 0.131), vec2(0.133, 0.067), 
+    vec2(0.154, -0.024), vec2(0.151, -0.063), vec2(0.124, 0.119), vec2(0.133, -0.121), 
+    vec2(0.149, 0.113), vec2(-0.188, -0.054), vec2(0.030, 0.201), vec2(0.002, 0.211), 
+    vec2(-0.063, -0.209), vec2(0.013, 0.226), vec2(-0.052, 0.229), vec2(-0.093, -0.223), 
+    vec2(0.153, 0.198), vec2(0.258, -0.005), vec2(-0.188, -0.188), vec2(0.267, -0.061), 
+    vec2(0.150, -0.238), vec2(0.027, 0.288), vec2(-0.293, 0.049), vec2(-0.044, 0.301), 
+    vec2(-0.018, 0.312), vec2(-0.296, -0.121), vec2(0.026, -0.327), vec2(-0.248, 0.226), 
+    vec2(-0.344, -0.008), vec2(-0.042, 0.349), vec2(-0.357, -0.038), vec2(-0.156, -0.332), 
+    vec2(-0.342, 0.154), vec2(0.031, 0.382), vec2(-0.331, 0.207), vec2(0.209, -0.339), 
+    vec2(-0.068, -0.401), vec2(-0.409, -0.063), vec2(-0.249, 0.340), vec2(-0.041, 0.428), 
+    vec2(0.326, 0.292), vec2(-0.127, -0.427), vec2(0.226, 0.393), vec2(0.424, 0.180), 
+    vec2(-0.063, 0.465), vec2(0.446, -0.167), vec2(-0.477, -0.084), vec2(-0.433, -0.234)
+};
+
+const vec3 light = vec3(-0.280166, -0.573576, -0.769751);
+
 void main(void) {
     worldPosition = varPosition;
     worldNormal = normalize(varNormal);
 
-    worldShadow = textureProj(shadowSamp, varShadow);
+    vec3 pos = varShadow.xyz / varShadow.w;
+
+    float fbias = bias * (1.0 - dot(worldNormal, -light)); 
+
+    float shadow = 0;
+    for(int i = 0; i < sampleAmount; i++) {
+        shadow += float(texture(shadowSamp, pos.xy + vec2(samples[i].x, abs(samples[i].y)) * radius).r + fbias > pos.z);
+    }
+    shadow /= sampleAmount;
+
+    /*if(shadow < 0.5) {
+        shadow = 4 * pow(shadow, 3);
+    } else {
+        //shadow = -2 * shadow * shadow + 4 * shadow - 1;
+        shadow = 1 - exp(-40 * shadow - 0.693147180559945 + 20);
+    }*/
+    //shadow = 1 - exp(-40 * shadow - 0.693147180559945 + 20);
+    //shadow = 1 - exp(-shadow);
+    //shadow = float(shadow > 0.05);
+
+    worldShadow = shadow;
+
+    //worldShadow = textureProj(shadowSamp, varShadow);
+    //float f = 1 - dot(worldNormal, -light);
+    //worldColor = vec4(f, f, f, 1.0);
     worldColor = texture(samp, varTex);
 }

+ 0 - 4
resources/shader/worldPostFragment.fs

@@ -24,8 +24,4 @@ void main() {
     float f = diffuseLight * texture(shadowSamp, varTex).r;
 
     color *= f * 0.5 + 0.5;
-
-    //color *= f * 0.5 + 0.5;
-
-    //color = texture(worldNormalSamp, varTex);
 }  

+ 1 - 3
resources/shader/worldVertex.vs

@@ -8,8 +8,6 @@ uniform mat4 proj;
 uniform mat4 view;
 uniform mat4 model;
 
-uniform mat4 projShadow;
-uniform mat4 viewShadow;
 uniform mat4 projViewShadow;
 
 out vec3 varPosition;
@@ -30,5 +28,5 @@ void main(void) {
     varPosition = worldPos.xyz;
     gl_Position = proj * worldPos;
 
-    varShadow = projViewShadow * vec4(position - light * 0.05, 1.0);
+    varShadow = projViewShadow * vec4(position - light * 0.0, 1.0);
 }