Skip to content

Commit

Permalink
rebased, fixed and fine tuned values and fully implemented d/n cycle
Browse files Browse the repository at this point in the history
also increased total day + night duration to 16min
  • Loading branch information
leonidomgz committed Mar 5, 2020
1 parent 0c71255 commit 35ecfb1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
19 changes: 16 additions & 3 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*
* =====================================================================================
*/
#include <algorithm>
#include <cmath>
#include <iostream>

#include <glm/gtc/matrix_transform.hpp>
Expand Down Expand Up @@ -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;

Expand Down
14 changes: 13 additions & 1 deletion resources/shaders/fog.f.glsl
Original file line number Diff line number Diff line change
@@ -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);
}

11 changes: 9 additions & 2 deletions resources/shaders/game.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down

0 comments on commit 35ecfb1

Please sign in to comment.