From 0742dec5d017dfd465871f887a7a239a589b7764 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Mon, 30 Mar 2020 04:36:00 +0200 Subject: [PATCH] Reduced AO effect with smooth lighting. The strength of this effect is now a config option. --- config.example.lua | 1 + source/client/core/Config.cpp | 2 ++ source/client/core/Config.hpp | 1 + source/client/states/SettingsMenuState.cpp | 11 +++++++++ source/client/world/ChunkBuilder.cpp | 26 ++++++++++++++-------- source/client/world/ChunkBuilder.hpp | 11 ++++++--- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/config.example.lua b/config.example.lua index bdd5e6ab..65932e51 100644 --- a/config.example.lua +++ b/config.example.lua @@ -25,6 +25,7 @@ screenWidth = 1600 screenHeight = 1050 guiScale = 3 mipmapLevels = 0 +aoStrength = 0.5 -- Input mouseSensitivity = 8 diff --git a/source/client/core/Config.cpp b/source/client/core/Config.cpp index 8c2298ea..55be3032 100644 --- a/source/client/core/Config.cpp +++ b/source/client/core/Config.cpp @@ -49,6 +49,7 @@ u16 Config::screenWidth = 1600; u16 Config::screenHeight = 1050; u8 Config::guiScale = 3; u8 Config::mipmapLevels = 0; +float Config::aoStrength = 0.5f; // Input u8 Config::mouseSensitivity = 8; @@ -86,6 +87,7 @@ void Config::loadConfigFromFile(const char *file) { screenHeight = lua["screenHeight"].get_or(screenHeight); guiScale = lua["guiScale"].get_or(guiScale); mipmapLevels = lua["mipmapLevels"].get_or(mipmapLevels); + aoStrength = lua["aoStrength"].get_or(aoStrength); mouseSensitivity = lua["mouseSensitivity"].get_or(mouseSensitivity); diff --git a/source/client/core/Config.hpp b/source/client/core/Config.hpp index 911dcb1d..4ee70949 100644 --- a/source/client/core/Config.hpp +++ b/source/client/core/Config.hpp @@ -53,6 +53,7 @@ namespace Config { extern u16 screenHeight; extern u8 guiScale; extern u8 mipmapLevels; + extern float aoStrength; // Input extern u8 mouseSensitivity; diff --git a/source/client/states/SettingsMenuState.cpp b/source/client/states/SettingsMenuState.cpp index 5703e917..2aa0229a 100644 --- a/source/client/states/SettingsMenuState.cpp +++ b/source/client/states/SettingsMenuState.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "Config.hpp" #include "Events.hpp" @@ -196,6 +197,16 @@ void SettingsMenuState::addGraphicsButtons() { Config::mipmapLevels = (Config::mipmapLevels + 1) % 5; button.setText("Mipmap Levels: " + std::to_string(Config::mipmapLevels)); }); + + m_menuWidget.addButton("AO Strength: " + gk::to_string(Config::aoStrength, 2), [] (TextButton &button) { + Config::aoStrength += 0.25f; + if (Config::aoStrength > 1.5f) + Config::aoStrength = 0.f; + + button.setText("AO Strength: " + gk::to_string(Config::aoStrength, 2)); + + World::isReloadRequested = true; + }); } void SettingsMenuState::addInputButtons() { diff --git a/source/client/world/ChunkBuilder.cpp b/source/client/world/ChunkBuilder.cpp index 59d9ad26..9767a75c 100644 --- a/source/client/world/ChunkBuilder.cpp +++ b/source/client/world/ChunkBuilder.cpp @@ -380,15 +380,22 @@ inline u8 ChunkBuilder::getLightForVertex(Light light, s8f x, s8f y, s8f z, cons (normal.z != 0) ? offset.z : 0 }; - // Get light values for surrounding nodes - s8 lightValues[4] = { - getLight(&chunk, x + minOffset.x, y + minOffset.y, z + offset.z), - getLight(&chunk, x + offset.x, y + minOffset.y, z + minOffset.z), - getLight(&chunk, x + minOffset.x, y + offset.y, z + minOffset.z), - getLight(&chunk, x + offset.x, y + offset.y, z + offset.z), + gk::Vector3i surroundingBlocks[4]{ + {x + minOffset.x, y + minOffset.y, z + offset.z}, + {x + offset.x, y + minOffset.y, z + minOffset.z}, + {x + minOffset.x, y + offset.y, z + minOffset.z}, + {x + offset.x, y + offset.y, z + offset.z} }; - u8 count = 0, total = 0; + // Get light values for surrounding nodes + s8 lightValues[4] = { + getLight(&chunk, surroundingBlocks[0].x, surroundingBlocks[0].y, surroundingBlocks[0].z), + getLight(&chunk, surroundingBlocks[1].x, surroundingBlocks[1].y, surroundingBlocks[1].z), + getLight(&chunk, surroundingBlocks[2].x, surroundingBlocks[2].y, surroundingBlocks[2].z), + getLight(&chunk, surroundingBlocks[3].x, surroundingBlocks[3].y, surroundingBlocks[3].z), + }; + + float count = 0, total = 0; for (u8 i = 0 ; i < 4 ; ++i) { // Fix light approximation // if (i == 3 && lightValues[i] > lightValues[0] && !lightValues[1] && !lightValues[2]) @@ -396,8 +403,9 @@ inline u8 ChunkBuilder::getLightForVertex(Light light, s8f x, s8f y, s8f z, cons // If the chunk is initialized, add the light value to the total if (lightValues[i] != -1) { - total += lightValues[i]; - ++count; + float strength = ((surroundingBlocks[i] - normal == gk::Vector3i{x, y, z}) ? 1 : Config::aoStrength); + total += lightValues[i] * strength; + count += strength; } } diff --git a/source/client/world/ChunkBuilder.hpp b/source/client/world/ChunkBuilder.hpp index 51ca7cc9..7291b734 100644 --- a/source/client/world/ChunkBuilder.hpp +++ b/source/client/world/ChunkBuilder.hpp @@ -60,15 +60,20 @@ class ChunkBuilder { void addFace(s8f x, s8f y, s8f z, s8f f, const ClientChunk &chunk, const Block &block, const gk::Vector3i &normal, const glm::vec3 *const vertexPos[4], const gk::Vector3i *const neighbourOfs[4]); - void addCross(s8f x, s8f y, s8f z, const ClientChunk &chunk, const Block &block, const glm::vec3 *const vertexPos[2][4]); + + void addCross(s8f x, s8f y, s8f z, const ClientChunk &chunk, const Block &block, + const glm::vec3 *const vertexPos[2][4]); enum class Light { Sun, Torch }; - u8 getAmbientOcclusion(s8f x, s8f y, s8f z, const gk::Vector3i &offset, const gk::Vector3i &normal, const ClientChunk &chunk); - u8 getLightForVertex(Light light, s8f x, s8f y, s8f z, const gk::Vector3i &offset, const gk::Vector3i &normal, const ClientChunk &chunk); + u8 getAmbientOcclusion(s8f x, s8f y, s8f z, const gk::Vector3i &offset, + const gk::Vector3i &normal, const ClientChunk &chunk); + + u8 getLightForVertex(Light light, s8f x, s8f y, s8f z, const gk::Vector3i &offset, + const gk::Vector3i &normal, const ClientChunk &chunk); std::array, layers> m_vertices;