From 35ecfb174905b7c75d0d3b21080d8cf9a929704f Mon Sep 17 00:00:00 2001 From: leonidomgz Date: Fri, 6 Mar 2020 00:19:22 +0100 Subject: [PATCH] rebased, fixed and fine tuned values and fully implemented d/n cycle also increased total day + night duration to 16min --- client/source/states/GameState.cpp | 19 ++++++++++++++++--- resources/shaders/fog.f.glsl | 14 +++++++++++++- resources/shaders/game.f.glsl | 11 +++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/client/source/states/GameState.cpp b/client/source/states/GameState.cpp index afa7ea6b5..ac3433387 100644 --- a/client/source/states/GameState.cpp +++ b/client/source/states/GameState.cpp @@ -24,6 +24,8 @@ * * ===================================================================================== */ +#include +#include #include #include @@ -174,9 +176,20 @@ void GameState::initShaders() { void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { // FIXME: This uniform is not used anymore since water/leaves effects are disabled - // gk::Shader::bind(&m_shader); - // m_shader.setUniform("u_time", gk::GameClock::getTicks()); - // gk::Shader::bind(nullptr); + // Now used by day/night cycle + gk::Shader::bind(&m_shader); + m_shader.setUniform("u_time", gk::GameClock::getTicks()); + gk::Shader::bind(nullptr); + + float pi = 3.1415927; + float frequency = 480000; + float time = gk::GameClock::getTicks() % 960000; + float sunlight = std::min(std::max((double)(1 + std::cos(2 * pi / frequency * time) * 2.0), 0.0), 1.0); + float red = std::min(std::max((double)sunlight - 0.55, 0.0), 0.45); + float green = std::min(std::max((double)sunlight - 0.35, 0.0), 0.65); + float blue = std::min(std::max((double)sunlight - 0.09, 0.0), 0.91); + + glClearColor(red, green, blue, 1.0); states.shader = &m_shader; diff --git a/resources/shaders/fog.f.glsl b/resources/shaders/fog.f.glsl index f19509fae..c2ac2f155 100644 --- a/resources/shaders/fog.f.glsl +++ b/resources/shaders/fog.f.glsl @@ -1,8 +1,20 @@ #version 120 +varying vec2 v_lightValue; + +uniform int u_time; + vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd) { float fog = clamp((fogEnd - fogCoord) / (fogEnd - fogStart), 0.0, 1.0); - return mix(vec4(0.196078, 0.6, 0.8, 1.0), color, fog); + const float pi = 3.1415927; + const float frequency = 480000; + // 960000 + float time = mod(u_time, 960000); + float sunlight = clamp((1 + cos(2 * pi / frequency * time) * 4.0), 0.0, 1.0); + float red = clamp(sunlight - 0.55, 0.0, 0.45); + float green = clamp(sunlight - 0.35, 0.0, 0.65); + float blue = clamp(sunlight - 0.09, 0.0, 0.91); + return mix(vec4(red, green, blue, 1.0), color, fog); } diff --git a/resources/shaders/game.f.glsl b/resources/shaders/game.f.glsl index 3a722a54a..99b137b72 100644 --- a/resources/shaders/game.f.glsl +++ b/resources/shaders/game.f.glsl @@ -9,6 +9,7 @@ varying float v_blockFace; varying float v_dist; uniform int u_renderDistance; +uniform int u_time; // Get current pixel color vec4 getColor(); @@ -45,6 +46,12 @@ void main() { float minBrightness = 2.0 / 16.0; if (lightCheck != -1.) { + const float pi = 3.1415927; + const float frequency = 480000; + + float time = mod(u_time, 960000); + float sunlight = clamp(v_lightValue.x * 0.5 * (1 + cos(2 * pi / frequency * time) * 4.0), 3, 15); + float ambientIntensity = max(max(v_lightValue.x, v_lightValue.y) / 16.0, minBrightness); float diffuseIntensity = max(v_lightValue.x, v_lightValue.y) / 32.0; @@ -58,8 +65,8 @@ void main() { // South or North if (blockFace == 2. || blockFace == 3.) ambientIntensity = max(ambientIntensity * 0.9, minBrightness); - - color = light(color, vec3(1.0, 1.0, 1.0), v_coord3d, ambientIntensity, diffuseIntensity); + float lightval = clamp(sunlight / 16.0, v_lightValue.y / 16.0, 1.0); + color = light(color, vec3(lightval, lightval, lightval), v_coord3d, ambientIntensity, diffuseIntensity); // color = vec4(0, 0, v_lightValue.x / 16.0, 1); }