Bladeren bron

shadow mapping tests

Kajetan Johannes Hammerle 5 jaren geleden
bovenliggende
commit
3049f7e0fa

+ 4 - 1
Makefile

@@ -11,7 +11,7 @@ run_client: game_client
 game_client: MainClient.cpp\
 	Clock.o DirectRenderer.o KeyManager.o Mesh.o MouseManager.o Shader.o\
 	Texture.o Utils.o Wrapper.o ShaderProgram.o WorldShader.o FramebufferRectangle.o\
-	SSAOShader.o SSAOBlurShader.o WorldPostShader.o OverlayShader.o\
+	SSAOShader.o SSAOBlurShader.o WorldPostShader.o OverlayShader.o WorldShadowShader.o\
 	Matrix3D.o Matrix3DStack.o StackOverflow.o StackUnderflow.o Vector3D.o Plane3D.o Camera3D.o\
 	Client.o\
 	Block.o Blocks.o BlockAir.o\
@@ -71,6 +71,9 @@ WorldPostShader.o: engine/shader/WorldPostShader.cpp engine/shader/WorldPostShad
 OverlayShader.o: engine/shader/OverlayShader.cpp engine/shader/OverlayShader.h
 	g++ $(VERSION) -c engine/shader/OverlayShader.cpp -o $@
 	
+WorldShadowShader.o: engine/shader/WorldShadowShader.cpp engine/shader/WorldShadowShader.h
+	g++ $(VERSION) -c engine/shader/WorldShadowShader.cpp -o $@
+	
 # ------------------------------------------------------------------------------	
 # Client
 # ------------------------------------------------------------------------------

+ 11 - 7
client/Client.cpp

@@ -12,9 +12,9 @@ Client::Client() : world(&chunkProvider)
     BlockRegistry::registerBlocks();
     BlockRenderers::init();
 
-    position.set(0, -9, 3);
-    lengthAngle = 180;
-    widthAngle = 0;
+    position.set(16, 16, 16);
+    lengthAngle = 60;
+    widthAngle = 60;
     
     camera.setPosition(position.getX(), position.getY(), position.getZ(), 0, 0);
     camera.storePosition();
@@ -52,7 +52,7 @@ void Client::tick()
     
     camera.storePosition();
     
-    float factor = 0.05f;
+    float factor = 0.5f;
     if(keyManager.isDown(KEY_LEFT))
     {
         position.addMul(camera.getFlatLeft(), factor);
@@ -110,10 +110,8 @@ void Client::tick()
     keyManager.tick();
 }
 
-void Client::render3DTick(float lag)
+void Client::render3DShadowTick(float lag)
 {
-    fps.update();
-    
     camera.update(lag);
     Engine::setWorldViewMatrix(camera.getViewMatrix());
     
@@ -124,6 +122,12 @@ void Client::render3DTick(float lag)
     entity.renderTick(shader, camera, directRenderer, lag);
 }
 
+void Client::render3DTick(float lag)
+{
+    fps.update();
+    render3DShadowTick(lag);
+}
+
 void Client::render2DTick(float lag)
 {
     shader.setToIdentity();

+ 1 - 0
client/Client.h

@@ -23,6 +23,7 @@ public:
     virtual ~Client();
     
     void tick() override;
+    void render3DShadowTick(float lag) override;
     void render3DTick(float lag) override;
     void render2DTick(float lag) override;
     

+ 14 - 96
client/rendering/entity/EntityRenderer.cpp

@@ -5,7 +5,7 @@
 EntityRenderer::EntityRenderer() : texture("resources/skin.png")
 {
     // head
-    addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 
+    mesh.addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 
             0.125f, 0.0f, 0.25f, 0.125f,
             0.25f, 0.0f, 0.375f, 0.125f,
             0.25f, 0.125f, 0.375f, 0.25f,
@@ -13,7 +13,7 @@ EntityRenderer::EntityRenderer() : texture("resources/skin.png")
             0.125f, 0.125f, 0.25f, 0.25f,
             0.375f, 0.125f, 0.5f, 0.25f);
     // right arm
-    addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
+    mesh.addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
             0.6875f, 0.25f, 0.75f, 0.3125f,
             0.75f, 0.25f, 0.8125f, 0.3125f,
             0.75f, 0.3125f, 0.8125f, 0.5f,
@@ -21,7 +21,7 @@ EntityRenderer::EntityRenderer() : texture("resources/skin.png")
             0.6875f, 0.3125f, 0.75f, 0.5f,
             0.8125f, 0.3125f, 0.875f, 0.5f);
     // left arm
-    addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
+    mesh.addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
             0.5625f, 0.75f, 0.625f, 0.8125f,
             0.625f, 0.75f, 0.6875f, 0.8125f,
             0.625f, 0.8125f, 0.6875f, 1.0f,
@@ -29,7 +29,7 @@ EntityRenderer::EntityRenderer() : texture("resources/skin.png")
             0.5625f, 0.8125f, 0.625f, 1.0f,
             0.6875f, 0.8125f, 0.75f, 1.0f);
     // body
-    addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.75f, 0.25f, 
+    mesh.addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.75f, 0.25f, 
             0.3125f, 0.25f, 0.4375f, 0.3125f,
             0.4375f, 0.25f, 0.5625f, 0.3125f,
             0.4375f, 0.3125f, 0.5f, 0.5f,
@@ -37,7 +37,7 @@ EntityRenderer::EntityRenderer() : texture("resources/skin.png")
             0.3125f, 0.3125f, 0.4375f, 0.5f,
             0.5f, 0.3125f, 0.625f, 0.5f);
     // right leg
-    addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
+    mesh.addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
             0.0625f, 0.25f, 0.125f, 0.3125f,
             0.125f, 0.25f, 0.1875f, 0.3125f,
             0.125f, 0.3125f, 0.1875f, 0.5f,
@@ -45,7 +45,7 @@ EntityRenderer::EntityRenderer() : texture("resources/skin.png")
             0.0625f, 0.3125f, 0.125f, 0.5f,
             0.1875f, 0.3125f, 0.25f, 0.5f);
     // left leg
-    addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
+    mesh.addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f, 
             0.3125f, 0.75f, 0.375f, 0.8125f,
             0.375f, 0.75f, 0.4375f, 0.8125f,
             0.375f, 0.8125f, 0.4375f, 1.0f,
@@ -85,10 +85,15 @@ void EntityRenderer::renderTick(Shader& shader, Camera3D camera, DirectRenderer&
     texture.bind();
     
     shader.setToIdentity();
-    shader.translateTo(0.0f, -10.0f, 0.0f);
+    shader.translateTo(5.0f + 0.5f, 42.0f, 15.0f + 0.25f);
+    //shader.scale(3, 6, 3);
+    shader.rotateY(90);
+    shader.translate(-0.5f, 0.0f, -0.25f);
     
     shader.push();
-    shader.translate(0.25f, 1.5f, 0.0f);
+    shader.translate(0.25f + 0.25f, 1.5f, 0.0f + 0.25f);
+    shader.rotateY(inter * 0.25f - 22.5f);
+    shader.translate(-0.25f, 0.0f, -0.25f);
     Engine::setWorldModelMatrix(shader.getModelMatrix()); 
     mesh.draw(0, 36);
     shader.pop();
@@ -126,91 +131,4 @@ void EntityRenderer::renderTick(Shader& shader, Camera3D camera, DirectRenderer&
     Engine::setWorldModelMatrix(shader.getModelMatrix()); 
     mesh.draw(180, 36);
     shader.pop();
-}
-
-void EntityRenderer::addTriangle(float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY, 
-                                float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY, 
-                                float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY)
-{
-    mesh.addPosition(p1x, p1y, p1z);
-    mesh.addPosition(p2x, p2y, p2z);
-    mesh.addPosition(p3x, p3y, p3z);
-    
-    mesh.addNormal(p1nx, p1ny, p1nz);
-    mesh.addNormal(p2nx, p2ny, p2nz);
-    mesh.addNormal(p3nx, p3ny, p3nz);
-    
-    mesh.addTexture(p1texX, p1texY);
-    mesh.addTexture(p2texX, p2texY);
-    mesh.addTexture(p3texX, p3texY);
-}
-
-void EntityRenderer::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
-               float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY, 
-               float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY, 
-               float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY, 
-               float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY, 
-               float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY, 
-               float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
-{
-    // bottom side
-    addTriangle(
-            sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
-            ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
-            sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
-    addTriangle(
-            ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
-            ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
-            sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
-    
-    // top side
-    addTriangle(
-            sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
-            sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
-            ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY);
-    addTriangle(
-            ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
-            sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
-            ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY);
-    
-    // north side
-    addTriangle(
-            ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
-            ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
-            ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY);
-    addTriangle(
-            ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
-            ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
-            ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY);
-    
-    // south side
-    addTriangle(
-            sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
-            sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
-            sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY);
-    addTriangle(
-            sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
-            sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
-            sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY);
-    
-    // east side
-    addTriangle(
-            sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
-            ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
-            ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY);
-    addTriangle(
-            sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
-            ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
-            sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY);
-    
-    // west side
-    addTriangle(
-            sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
-            ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
-            ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY);
-    addTriangle(
-            sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
-            sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
-            ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY);
-}
-
+}

+ 0 - 12
client/rendering/entity/EntityRenderer.h

@@ -17,18 +17,6 @@ public:
     void renderTick(Shader& shader, Camera3D camera, DirectRenderer& dr, float lag);
     
 private:
-    void addTriangle(float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY, 
-                float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY, 
-                float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY);
-    
-    void addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
-               float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY, 
-               float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY, 
-               float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY, 
-               float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY, 
-               float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY, 
-               float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY);
-    
     Texture texture;
     NormalTextureMesh mesh;
     unsigned int lifetime = 0;

+ 91 - 0
engine/Mesh.cpp

@@ -160,3 +160,94 @@ void Mesh::draw(int start, int count)
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
     glDrawArrays(GL_TRIANGLES, start, count);
 }
+
+NormalTextureMesh::NormalTextureMesh() : Mesh(Mesh::NORMAL | Mesh::TEXTURE)
+{
+}
+
+void NormalTextureMesh::addTriangle(
+        float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY, 
+        float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY, 
+        float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY)
+{
+    addPosition(p1x, p1y, p1z);
+    addPosition(p2x, p2y, p2z);
+    addPosition(p3x, p3y, p3z);
+    
+    addNormal(p1nx, p1ny, p1nz);
+    addNormal(p2nx, p2ny, p2nz);
+    addNormal(p3nx, p3ny, p3nz);
+    
+    addTexture(p1texX, p1texY);
+    addTexture(p2texX, p2texY);
+    addTexture(p3texX, p3texY);
+}
+
+void NormalTextureMesh::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
+        float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY, 
+        float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY, 
+        float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY, 
+        float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY, 
+        float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY, 
+        float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
+{
+    // bottom side
+    addTriangle(
+            sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
+            ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
+            sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
+    addTriangle(
+            ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
+            ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
+            sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
+    
+    // top side
+    addTriangle(
+            sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
+            sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
+            ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY);
+    addTriangle(
+            ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
+            sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
+            ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY);
+    
+    // north side
+    addTriangle(
+            ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
+            ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
+            ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY);
+    addTriangle(
+            ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
+            ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
+            ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY);
+    
+    // south side
+    addTriangle(
+            sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
+            sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
+            sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY);
+    addTriangle(
+            sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
+            sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
+            sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY);
+    
+    // east side
+    addTriangle(
+            sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
+            ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
+            ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY);
+    addTriangle(
+            sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
+            ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
+            sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY);
+    
+    // west side
+    addTriangle(
+            sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
+            ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
+            ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY);
+    addTriangle(
+            sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
+            sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
+            ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY);
+}

+ 12 - 1
engine/Mesh.h

@@ -48,7 +48,18 @@ private:
 class NormalTextureMesh : public Mesh
 {
 public:
-    NormalTextureMesh() : Mesh(Mesh::TEXTURE | Mesh::NORMAL) {};
+    NormalTextureMesh();
+    
+    void addTriangle(float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY, 
+            float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY, 
+            float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY);
+    void addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
+            float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY, 
+            float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY, 
+            float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY, 
+            float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY, 
+            float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY, 
+            float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY);
 };
 
 #endif

+ 0 - 2
engine/Texture.cpp

@@ -145,8 +145,6 @@ void Texture::initGL()
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
     delete[] data;
     data = nullptr;
-
-    glGenerateMipmap(GL_TEXTURE_2D);
 }
 
 void Texture::bind()

+ 64 - 2
engine/Wrapper.cpp

@@ -23,6 +23,8 @@ Matrix3D Engine::projMatrix;
 // rectangle for framebuffer drawing
 FramebufferRectangle Engine::rectangle;
 
+// shader stage 0 - world shadow mapping
+WorldShadowShader Engine::worldShadowShader;
 // shader stage 1 - world
 WorldShader Engine::worldShader;
 // shader stage 2 - world ssao
@@ -75,7 +77,9 @@ bool Engine::init(int width, int height, const char* name)
     }
     cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
 
-    if(!worldShader.init() || !ssaoShader.init() || !ssaoBlurShader.init() || !worldPostShader.init() || !overlayShader.init() || !rectangle.init())
+    if(!worldShader.init() || !ssaoShader.init() || !ssaoBlurShader.init() || 
+            !worldPostShader.init() || !overlayShader.init() || !rectangle.init() || 
+            !worldShadowShader.init())
     {
         glfwDestroyWindow(window);
         glfwTerminate();
@@ -147,6 +151,7 @@ void Engine::start(IClient* client)
                     ssaoShader.resize();
                     ssaoBlurShader.resize();
                     worldPostShader.resize();
+                    worldShadowShader.resize();
                 }
 
                 if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
@@ -263,7 +268,7 @@ void Engine::printError()
     switch(error)
     {
         case GL_NO_ERROR: 
-            cout << "> No error has been recorded." << endl;
+            //cout << "> No error has been recorded." << endl;
             break;
         case GL_INVALID_ENUM: 
             cout << "> An unacceptable value is specified for an enumerated argument." << endl;
@@ -305,6 +310,61 @@ void Engine::onRenderTick(float lag)
     projMatrix.set(2, 3, (2.0f * nearClip * farClip) / (nearClip - farClip));
     projMatrix.set(3, 3, 0); 
     
+    // -------------------------------------------------------------------------
+    // shader stage 0 - world shadow mapping
+    // -------------------------------------------------------------------------
+    /*worldShadowShader.preRender(projMatrix.getValues());
+    
+    Matrix3D sunView;
+    Vector3D front;
+    front.setAngles(60, 60);
+    
+    // back
+    Vector3D back;
+    back.setInverse(front);
+
+    // right
+    Vector3D right;
+    right.set(front);
+    right.cross(0.0f, 1.0f, 0.0f);
+    right.normalize();
+    
+    // left
+    Vector3D left;
+    left.setInverse(right);
+    
+    // up
+    Vector3D up;
+    up.set(front);
+    up.cross(left);
+    up.normalize();
+    
+    Vector3D interCamera(16, 16, 16);
+    
+    sunView.set(0, 0, right.getX());
+    sunView.set(0, 1, right.getY());
+    sunView.set(0, 2, right.getZ());
+    sunView.set(0, 3, right.dotInverse(interCamera));
+    
+    sunView.set(1, 0, up.getX());
+    sunView.set(1, 1, up.getY());
+    sunView.set(1, 2, up.getZ());
+    sunView.set(1, 3, up.dotInverse(interCamera));
+    
+    sunView.set(2, 0, back.getX());
+    sunView.set(2, 1, back.getY());
+    sunView.set(2, 2, back.getZ());
+    sunView.set(2, 3, back.dotInverse(interCamera));
+    
+    sunView.set(3, 0, 0.0f);
+    sunView.set(3, 1, 0.0f);
+    sunView.set(3, 0, 0.0f);
+    sunView.set(3, 3, 1.0f);
+        
+    worldShadowShader.setViewMatrix(sunView.getValues());
+    
+    client->render3DShadowTick(lag);*/
+    
     // -------------------------------------------------------------------------
     // shader stage 1 - world
     // -------------------------------------------------------------------------
@@ -323,6 +383,7 @@ void Engine::onRenderTick(float lag)
     worldShader.bindColorTexture(3);
     worldShader.bindDepthTexture(4);
     ssaoShader.bindNoiseTexture(5);
+    worldShadowShader.bindDepthTexture(6);
     
     rectangle.draw();
     
@@ -367,6 +428,7 @@ void Engine::setWorldViewMatrix(const float* data)
 void Engine::setWorldModelMatrix(const float* data)
 {
     worldShader.setModelMatrix(data);
+    worldShadowShader.setModelMatrix(data);
 }
 
 void Engine::setOverlayModelMatrix(const float* data)

+ 5 - 0
engine/Wrapper.h

@@ -7,6 +7,7 @@
 #include "../math/Vector3D.h"
 #include "../math/Matrix3D.h"
 #include "shader/WorldShader.h"
+#include "shader/WorldShadowShader.h"
 #include "shader/SSAOShader.h"
 #include "shader/SSAOBlurShader.h"
 #include "shader/FramebufferRectangle.h"
@@ -21,6 +22,7 @@ class IClient
 {
 public:
     virtual void tick() = 0;
+    virtual void render3DShadowTick(float lag) = 0;
     virtual void render3DTick(float lag) = 0;
     virtual void render2DTick(float lag) = 0;
     virtual void onKeyEvent(int key, int scancode, int action, int mods) = 0;
@@ -34,6 +36,7 @@ public:
     static DummyClient dummy;
     
     void tick() override { cout << "Dummy tick" << endl; };
+    void render3DShadowTick(float lag) override { cout << "Dummy render3DShadowTick" << endl; };
     void render3DTick(float lag) override { cout << "Dummy render3DTick" << endl; };
     void render2DTick(float lag) override { cout << "Dummy render3DTick" << endl; };
     void onKeyEvent(int key, int scancode, int action, int mods) override { cout << "Dummy onKeyEvent" << endl; };
@@ -101,6 +104,8 @@ private:
     // rectangle for framebuffer drawing
     static FramebufferRectangle rectangle;
     
+    // shader stage 0 - world shadow mapping
+    static WorldShadowShader worldShadowShader;
     // shader stage 1 - world
     static WorldShader worldShader;
     // shader stage 2 - world ssao

+ 1 - 1
engine/shader/SSAOShader.h

@@ -32,7 +32,7 @@ private:
     GLuint noiseTexture = 0;
     // uniform data
     int numberOfSamples = 32;
-    float radius = 1.5f;
+    float radius = 2.5f;
     // uniforms locations
     GLint unifProjMatrix = 0;
     GLint unifNumberOfSamples = 0;

+ 95 - 0
engine/shader/WorldShadowShader.cpp

@@ -0,0 +1,95 @@
+#include "WorldShadowShader.h"
+#include "../Wrapper.h"
+
+WorldShadowShader::WorldShadowShader()
+{
+}
+
+WorldShadowShader::~WorldShadowShader()
+{
+    glDeleteFramebuffers(1, &framebuffer);
+    glDeleteTextures(1, &depthTexture);
+}
+
+bool WorldShadowShader::init()
+{
+    program.compile("shader/worldShadowVertex.vs", "shader/worldShadowFragment.fs");
+    if(!program.isValid())
+    {
+        return false;
+    }
+    
+    // generate framebuffer
+    glGenFramebuffers(1, &framebuffer);
+    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
+    glDrawBuffer(GL_NONE);
+    glReadBuffer(GL_NONE);
+        
+    // depth texture
+    glGenTextures(1, &depthTexture);
+    glBindTexture(GL_TEXTURE_2D, depthTexture);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, Engine::getWidth(), Engine::getHeight(), 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
+    // attache depth texture to framebuffer
+    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); 
+    
+    // check if framebuffer is okay
+    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+    {
+	cout << "world shadow frame buffer is not complete!" << endl;
+        return false;
+    }
+    // unbind framebuffer
+    glBindFramebuffer(GL_FRAMEBUFFER, 0);
+    
+    // get uniform locations
+    unifProjMatrix = glGetUniformLocation(program.getProgram(), "projMatrix");
+    unifViewMatrix = glGetUniformLocation(program.getProgram(), "viewMatrix");
+    unifModelMatrix = glGetUniformLocation(program.getProgram(), "modelMatrix");
+    
+    return true;
+}
+
+void WorldShadowShader::resize()
+{
+    glBindTexture(GL_TEXTURE_2D, depthTexture);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, Engine::getWidth(), Engine::getHeight(), 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
+}
+
+void WorldShadowShader::preRender(const float* projMatrix)
+{
+    // bind world shadow shader program
+    glUseProgram(program.getProgram());
+    
+    // set projection matrix uniform
+    glUniformMatrix4fv(unifProjMatrix, 1, 0, projMatrix);
+
+    // bind world framebuffer
+    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
+    
+    // clear color, depth and stencil buffer
+    glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+    
+    // no color, depth testing is needed
+    glEnable(GL_DEPTH_TEST);
+}
+
+void WorldShadowShader::bindDepthTexture(unsigned int textureUnit)
+{
+    glActiveTexture(GL_TEXTURE0 + textureUnit);
+    glBindTexture(GL_TEXTURE_2D, depthTexture);
+}
+
+void WorldShadowShader::setViewMatrix(const float* data)
+{
+    glUniformMatrix4fv(unifViewMatrix, 1, 0, data);
+}
+
+void WorldShadowShader::setModelMatrix(const float* data)
+{
+    glUniformMatrix4fv(unifModelMatrix, 1, 0, data);
+}
+

+ 38 - 0
engine/shader/WorldShadowShader.h

@@ -0,0 +1,38 @@
+#ifndef WORLDSHADOWMAPPINGSHADER_H
+#define WORLDSHADOWMAPPINGSHADER_H
+
+#include "ShaderProgram.h"
+
+class WorldShadowShader
+{
+public:
+    WorldShadowShader();
+    virtual ~WorldShadowShader();
+    
+    bool init();
+    void resize();
+
+    void preRender(const float* projMatrix);
+    
+    void bindDepthTexture(unsigned int textureUnit);
+    
+    void setViewMatrix(const float* data);
+    void setModelMatrix(const float* data);
+    
+private:
+    // shader
+    ShaderProgram program;
+    // framebuffer
+    GLuint framebuffer = 0;
+    // textures
+    GLuint depthTexture = 0;
+    // uniforms locations
+    GLint unifProjMatrix = 0;
+    GLint unifViewMatrix = 0;
+    GLint unifModelMatrix = 0;
+};
+
+#endif
+
+
+

+ 8 - 0
shader/ssaoFragment.fs

@@ -5,6 +5,7 @@ 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;
@@ -71,4 +72,11 @@ void main()
     occlusion /= numberOfSamples;
     occlusion = 1 - occlusion;
     color = occlusion;
+
+    //color = texture(worldDepthSamp, varTextureCoord).x;
+
+    /*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;*/
 } 

+ 8 - 0
shader/worldShadowFragment.fs

@@ -0,0 +1,8 @@
+#version 430
+
+out vec4 color;
+
+void main(void)
+{
+    color = vec4(1.0, 0.0, 1.0, 1.0);
+}

+ 12 - 0
shader/worldShadowVertex.vs

@@ -0,0 +1,12 @@
+#version 430
+
+layout (location = 0) in vec3 position;
+
+uniform mat4 projMatrix;
+uniform mat4 viewMatrix;
+uniform mat4 modelMatrix;
+
+void main(void)
+{ 
+    gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
+}

+ 6 - 2
shader/worldVertex.vs

@@ -22,16 +22,20 @@ void main(void)
     varTextureCoord = textureCoord; 
     varColor = vec4(color.xyz, 1);
 
-    if(abs(normal.x) == 1)
+    /*if(abs(normal.x) == 1)
     {
         varColor = varColor * 0.8;
     }
     if(abs(normal.z) == 1)
     {
         varColor = varColor * 0.9;
-    }
+    }*/
     
     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;*/
 }