Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Day/night cycle #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion resources/shaders/fog.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

uniform vec4 u_fogColor;

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(u_fogColor, color, fog);
const float pi = 3.1415927;
const float frequency = 480000;

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);
zodiuxus marked this conversation as resolved.
Show resolved Hide resolved
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);
zodiuxus marked this conversation as resolved.
Show resolved Hide resolved
/* return mix(u_fogColor, color, fog); */
}

13 changes: 12 additions & 1 deletion resources/shaders/game.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ varying float v_blockFace;
varying float v_dist;

uniform int u_renderDistance;

uniform sampler2D u_tex;

uniform int u_time;

// Get light color
vec4 light(vec4 color, vec3 lightColor, vec4 lightPosition, float ambientIntensity, float diffuseIntensity);

Expand Down Expand Up @@ -50,6 +53,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 @@ -64,7 +73,9 @@ void main() {
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);
zodiuxus marked this conversation as resolved.
Show resolved Hide resolved

color = light(color, vec3(lightval, lightval, lightval), v_coord3d, ambientIntensity, diffuseIntensity);
}

color.rgb *= v_ambientOcclusion;
Expand Down
19 changes: 16 additions & 3 deletions source/client/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 @@ -208,9 +210,20 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {

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::getInstance().getTicks());
// gk::Shader::bind(nullptr);
// Now used by day/night cycle
gk::Shader::bind(&m_shader);
m_shader.setUniform("u_time", (int)gk::GameClock::getInstance().getTicks());
gk::Shader::bind(nullptr);

float pi = 3.1415927;
float frequency = 480000;
float time = gk::GameClock::getInstance().getTicks() % 960000;
float sunlight = std::min(std::max((1 + std::cos(2 * pi / frequency * time) * 2.0), 0.0), 1.0);
float red = std::min(std::max(sunlight - 0.55, 0.0), 0.45);
float green = std::min(std::max(sunlight - 0.35, 0.0), 0.65);
float blue = std::min(std::max(sunlight - 0.09, 0.0), 0.91);

glClearColor(red, green, blue, 1.0);

m_fbo.begin();

Expand Down