Skip to content

Commit

Permalink
Basic day/night cycle added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 17, 2020
1 parent fe43dcd commit e73f436
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
12 changes: 11 additions & 1 deletion resources/shaders/fog.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

uniform vec4 u_fogColor;

uniform float 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;

float sunlight = clamp((1 + sin(2 * pi * u_time) * 4.0), 0.0, 1.0);

float red = clamp(sunlight - (1 - u_fogColor.r), 0.0, u_fogColor.r);
float green = clamp(sunlight - (1 - u_fogColor.g), 0.0, u_fogColor.g);
float blue = clamp(sunlight - (1 - u_fogColor.b), 0.0, u_fogColor.b);

return mix(vec4(red, green, blue, u_fogColor.a), color, fog);
}

11 changes: 10 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 float u_time;

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

Expand Down Expand Up @@ -50,6 +53,10 @@ void main() {

float minBrightness = 2.0 / 16.0;
if (lightCheck != -1.) {
const float pi = 3.1415927;

float sunlight = clamp(v_lightValue.x * 0.5 * (1 + sin(2 * pi * u_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 +71,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);

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

color.rgb *= v_ambientOcclusion;
Expand Down
2 changes: 1 addition & 1 deletion source/client/graphics/CelestialObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void CelestialObject::updateVertexBuffer() const {
}

void CelestialObject::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform.rotate(fmod(gk::GameClock::getInstance().getTicks(true) / 1000.f, 360), {0, 1, 0});
states.transform.rotate(-fmod(gk::GameClock::getInstance().getTicks() * 4 / 1000.f, 360), {0, 1, 0});
states.transform *= getTransform();

states.vertexAttributes = VertexAttribute::All;
Expand Down
4 changes: 2 additions & 2 deletions source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

Skybox::Skybox(gk::Camera &camera) : m_camera(camera) {
m_sun.setColor(gk::Color::Yellow);
m_sun.setPosition(150, -10, 0);
m_sun.setPosition(150, -10, -10);

m_moon.setColor(gk::Color::White);
m_moon.setPosition(-150, -10, 0);
m_moon.setPosition(-150, -10, -10);
}

void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
Expand Down
23 changes: 19 additions & 4 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 @@ -207,10 +209,23 @@ 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);
float time = std::fmod(gk::GameClock::getInstance().getTicks() * 4.f / 1000.f, 360.f) / 360.f;

gk::Shader::bind(&m_shader);
m_shader.setUniform("u_time", time);
gk::Shader::bind(nullptr);

if (m_world.sky()) {
const float pi = 3.1415927f;
float sunlight = std::clamp(0.5f + std::sin(2 * pi * time) * 2.0f, 0.0f, 1.0f);

gk::Color skyColor = m_world.sky()->color();
float red = std::clamp(sunlight - (1 - skyColor.r), 0.0f, skyColor.r);
float green = std::clamp(sunlight - (1 - skyColor.g), 0.0f, skyColor.g);
float blue = std::clamp(sunlight - (1 - skyColor.b), 0.0f, skyColor.b);

glClearColor(red, green, blue, 1.0f);
}

m_fbo.begin();

Expand Down
3 changes: 1 addition & 2 deletions source/client/world/ClientWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ void ClientWorld::changeDimension(u16 dimensionID) {
const Sky &sky = Registry::getInstance().getSkyFromStringID(dimension.sky());
m_sky = &sky;

glCheck(glClearColor(sky.color().r, sky.color().g, sky.color().b, sky.color().a));

// glCheck(glClearColor(sky.color().r, sky.color().g, sky.color().b, sky.color().a));
}

void ClientWorld::receiveChunkData(Network::Packet &packet) {
Expand Down
2 changes: 2 additions & 0 deletions source/client/world/ClientWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class ClientWorld : public World, public gk::Drawable {

std::size_t loadedChunkCount() const { return m_chunks.size(); }

const Sky *sky() const { return m_sky; }

private:
void createChunkNeighbours(ClientChunk *chunk);

Expand Down

0 comments on commit e73f436

Please sign in to comment.