-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GameTime] Added to centralize time, sunlight intensity and sky color.
- Loading branch information
Showing
7 changed files
with
117 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
#version 120 | ||
|
||
uniform vec4 u_fogColor; | ||
|
||
uniform float u_time; | ||
uniform vec4 u_skyColor; | ||
|
||
vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd) { | ||
float fog = clamp((fogEnd - fogCoord) / (fogEnd - fogStart), 0.0, 1.0); | ||
|
||
const float pi = 3.1415927; | ||
|
||
float sunlight = clamp(0.5 + sin(2 * pi * u_time) * 2.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); | ||
return mix(vec4(u_skyColor.r, u_skyColor.g, u_skyColor.b, u_fogColor.a), color, fog); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* OpenMiner | ||
* | ||
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]> | ||
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) | ||
* | ||
* This file is part of OpenMiner. | ||
* | ||
* OpenMiner is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* OpenMiner is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with OpenMiner; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* ===================================================================================== | ||
*/ | ||
#include <cmath> | ||
|
||
#include <gk/core/GameClock.hpp> | ||
|
||
#include "GameTime.hpp" | ||
#include "Sky.hpp" | ||
|
||
float GameTime::getCurrentTime(float offset) { | ||
return std::fmod(gk::GameClock::getInstance().getTicks() * daySpeed / 1000.f + offset, 360.f) / 360.f; | ||
} | ||
|
||
float GameTime::getSunlightIntensityFromTime(float time) { | ||
static const float pi = 3.1415927f; | ||
return std::clamp(0.5f + std::sin(2 * pi * time) * 2.0f, 0.0f, 1.0f); | ||
} | ||
|
||
gk::Color GameTime::getSkyColorFromTime(const Sky &sky, float time) { | ||
float sunlight = getSunlightIntensityFromTime(time); | ||
|
||
gk::Color skyColor = sky.color(); | ||
skyColor.r = std::clamp(sunlight - (1.f - skyColor.r), 0.0f, skyColor.r); | ||
skyColor.g = std::clamp(sunlight - (1.f - skyColor.g), 0.0f, skyColor.g); | ||
skyColor.b = std::clamp(sunlight - (1.f - skyColor.b), 0.0f, skyColor.b); | ||
skyColor.a = 1.f; | ||
|
||
return skyColor; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* OpenMiner | ||
* | ||
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]> | ||
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) | ||
* | ||
* This file is part of OpenMiner. | ||
* | ||
* OpenMiner is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* OpenMiner is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with OpenMiner; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* ===================================================================================== | ||
*/ | ||
#ifndef GAMETIME_HPP_ | ||
#define GAMETIME_HPP_ | ||
|
||
#include <gk/graphics/Color.hpp> | ||
|
||
class Sky; | ||
|
||
class GameTime { | ||
public: | ||
static constexpr float daySpeed = 8.f; | ||
|
||
static float getCurrentTime(float offset = 0.f); | ||
|
||
// Note: These functions are only needed in the client | ||
static float getSunlightIntensityFromTime(float time); | ||
static gk::Color getSkyColorFromTime(const Sky &sky, float time); | ||
}; | ||
|
||
#endif // GAMETIME_HPP_ |