Browse Source

toggleable shadows

Kajetan Johannes Hammerle 3 years ago
parent
commit
c5cc5136f6

+ 6 - 4
client/Game.cpp

@@ -127,9 +127,9 @@ void Game::tick() {
     ray.set(pos, lengthAngle, widthAngle);
 
     if(control.keys.test5.getDownTime() == 1) {
-        renderSettings.ortho = !renderSettings.ortho;
+        renderSettings.shadows = !renderSettings.shadows;
     }
-    if(control.keys.test.isDown()) {
+    /*if(control.keys.test.isDown()) {
         renderSettings.testRadius /= 0.95f;
     }
     if(control.keys.test2.isDown()) {
@@ -140,7 +140,7 @@ void Game::tick() {
     }
     if(control.keys.test4.isDown()) {
         renderSettings.testBias *= 0.95f;
-    }
+    }*/
 
     testAngle += 5.0;
 }
@@ -158,7 +158,7 @@ void Game::renderWorld(float lag, Renderer& renderer) const {
 
 void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
     (void) lag;
-    renderer.scale(2.0f).update();
+    renderer.scale(1.0f).update();
 
     char buffer[50];
     snprintf(buffer, 50, "FPS: %.2f", fps.getUpdatesPerSecond());
@@ -169,6 +169,8 @@ void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) co
     fr.drawString(10, 30, buffer);
     snprintf(buffer, 50, "Radius: %.6f", renderSettings.testRadius);
     fr.drawString(10, 40, buffer);
+    snprintf(buffer, 50, "Shadows: %d", renderSettings.shadows);
+    fr.drawString(10, 50, buffer);
 }
 
 bool Game::isRunning() const {

+ 21 - 4
client/rendering/Engine.cpp

@@ -1,6 +1,11 @@
+#include <cmath>
+#include <cfloat>
+
 #include "client/rendering/Engine.h"
 #include "client/rendering/wrapper/GLFWWrapper.h"
 #include "client/rendering/wrapper/GLWrapper.h"
+#include "client/rendering/Renderer.h"
+#include "client/math/Plane.h"
 
 Engine::Engine(Shaders& shaders, Framebuffers& fb, Camera& camera, const WindowSize& size,
         RenderSettings& renderSettings) :
@@ -19,10 +24,12 @@ void Engine::renderTick(float lag, const Game& game) {
     camera.update(lag);
     updateWorldProjection();
     updateWorldView();
-    
-    renderShadow(lag, game);
+
+    if(renderSettings.shadows) {
+        renderShadow(lag, game);
+    }
     renderWorld(lag, game);
-    if(renderSettings.useSSAO) {
+    if(renderSettings.ssao) {
         renderSSAO();
     }
     renderPostWorld();
@@ -53,6 +60,7 @@ void Engine::renderWorld(float lag, const Game& game) {
     model.clear();
     shaders.world.setMatrix("model", model.get().getValues());
     fb.shadow.bindDepthTexture(1);
+    shaders.world.setInt("shadows", renderSettings.shadows);
     shaders.world.setFloat("radius", renderSettings.testRadius);
     shaders.world.setFloat("bias", renderSettings.testBias);
     Renderer renderer(shaders.world, model);
@@ -89,7 +97,8 @@ void Engine::renderPostWorld() {
     fb.ssaoBlur.bindRedTexture(1);
     fb.world.bindRedTexture(2);
     fb.world.bindNormalTexture(3);
-    shaders.postWorld.setInt("useSSAO", renderSettings.useSSAO);
+    shaders.postWorld.setInt("ssao", renderSettings.ssao);
+    shaders.postWorld.setInt("shadows", renderSettings.shadows);
     rectangle.draw();
 }
 
@@ -113,6 +122,10 @@ void Engine::renderTextOverlay(float lag, const Game& game) {
 
 void Engine::updateWorldProjection() {
     frustum.setProjection(worldProj, size.width, size.height);
+    
+    if(!renderSettings.shadows) {
+        return;
+    }
 
     // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
     const float fovY = 60.0f;
@@ -251,6 +264,10 @@ void Engine::updateWorldView() {
     worldView.set(12, right.dotInverse(pos));
     worldView.set(13, up.dotInverse(pos));
     worldView.set(14, back.dotInverse(pos));
+    
+    if(!renderSettings.shadows) {
+        return;
+    }
 
     right.set(0.939693f, 0.0f, -0.34202f);
     back.set(0.280166f, 0.573576f, 0.769751f);

+ 5 - 13
client/rendering/Engine.h

@@ -1,23 +1,15 @@
 #ifndef ENGINE_H
 #define ENGINE_H
 
-#include <cmath>
-#include <cfloat>
-
-#include "common/utils/Types.h"
-#include "client/math/Ray.h"
-#include "client/rendering/FontRenderer.h"
-#include "client/rendering/Mesh.h"
-#include "client/rendering/Framebuffers.h"
 #include "client/rendering/Shaders.h"
-#include "client/rendering/Renderer.h"
+#include "client/rendering/Framebuffers.h"
 #include "client/math/Camera.h"
-#include "client/math/Plane.h"
-#include "client/math/Frustum.h"
+#include "client/rendering/RenderSettings.h"
 #include "client/Game.h"
+#include "client/math/Frustum.h"
+#include "client/rendering/FontRenderer.h"
 #include "client/rendering/NoiseTexture.h"
-#include "client/utils/Clock.h"
-#include "client/rendering/RenderSettings.h"
+#include "client/rendering/Mesh.h"
 
 class Engine final {
 public:

+ 1 - 1
client/rendering/RenderSettings.cpp

@@ -1,4 +1,4 @@
 #include "client/rendering/RenderSettings.h"
 
-RenderSettings::RenderSettings() : useSSAO(true), testRadius(0.0005f), testBias(0.0005), ortho(false) {
+RenderSettings::RenderSettings() : ssao(true), shadows(false), testRadius(0.0005f), testBias(0.0005), ortho(false) {
 }

+ 2 - 1
client/rendering/RenderSettings.h

@@ -6,7 +6,8 @@
 struct RenderSettings {
     RenderSettings();
     
-    bool useSSAO;
+    bool ssao;
+    bool shadows;
     float testRadius;
     float testBias;
     bool ortho;

+ 12 - 23
resources/shader/worldFragment.fs

@@ -6,7 +6,6 @@ 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 sampler2D shadowSamp;
 
 uniform mat4 proj;
@@ -20,6 +19,7 @@ in vec2 varTex;
 in vec3 varNormal;
 in vec4 varShadow;
 
+uniform bool shadows;
 uniform float radius;
 uniform float bias;
 
@@ -49,29 +49,18 @@ void main(void) {
     worldPosition = varPosition;
     worldNormal = normalize(varNormal);
 
-    vec3 pos = varShadow.xyz / varShadow.w;
-
-    float fbias = bias * (1.0 - max(dot(worldNormal, -light), 0.0)); 
-
-    float shadow = 0;
-    for(int i = 0; i < sampleAmount; i++) {
-        shadow += float(texture(shadowSamp, pos.xy + vec2(samples[i].x, samples[i].y) * radius).r + fbias > pos.z);
-    }
-    shadow /= sampleAmount;
-
-    /*if(shadow < 0.5) {
-        shadow = 4 * pow(shadow, 3);
+    if(shadows) {
+        vec3 pos = varShadow.xyz / varShadow.w;
+        float fbias = bias * (1.0 - max(dot(worldNormal, -light), 0.0)); 
+        float shadow = 0;
+        for(int i = 0; i < sampleAmount; i++) {
+            shadow += float(texture(shadowSamp, pos.xy + vec2(samples[i].x, samples[i].y) * radius).r + fbias > pos.z);
+        }
+        shadow /= sampleAmount;
+        worldShadow = shadow;
     } 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 = float(shadow > 0.05);
-
-    worldShadow = shadow;
+        worldShadow = 1.0;
+    }
 
-    //worldShadow = textureProj(shadowSamp, varShadow);
-    //float f = 1.0 - max(dot(worldNormal, -light), 0.0);
-    //worldColor = vec4(f, f, f, 1.0);
     worldColor = texture(samp, varTex);
 }

+ 8 - 7
resources/shader/worldPostFragment.fs

@@ -5,7 +5,8 @@ layout (binding = 1) uniform sampler2D ssaoSamp;
 layout (binding = 2) uniform sampler2D shadowSamp;
 layout (binding = 3) uniform sampler2D worldNormalSamp;
 
-uniform bool useSSAO;
+uniform bool ssao;
+uniform bool shadows;
 
 in vec2 varTex;
 
@@ -14,14 +15,14 @@ out vec4 color;
 const vec3 light = vec3(-0.280166, -0.573576, -0.769751);
 
 void main() {
-    if(useSSAO) {
+    if(ssao) {
         color = vec4(texture(colorSamp, varTex).xyz * (texture(ssaoSamp, varTex).r + 0.5), 1.0);
     } else {
         color = vec4(texture(colorSamp, varTex).xyz, 1.0);
     }
-
-    float diffuseLight = float(dot(texture(worldNormalSamp, varTex).xyz, -light) > 0);
-    float f = diffuseLight * texture(shadowSamp, varTex).r;
-
-    color *= f * 0.5 + 0.5;
+    if(shadows) {
+        float diffuseLight = float(dot(texture(worldNormalSamp, varTex).xyz, -light) > 0);
+        float f = diffuseLight * texture(shadowSamp, varTex).r;
+        color *= f * 0.5 + 0.5;
+    }
 }