aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game.cpp2
-rw-r--r--src/sky.cpp77
-rw-r--r--src/sky.h41
3 files changed, 96 insertions, 24 deletions
diff --git a/src/game.cpp b/src/game.cpp
index e1424c9dd..cc5d3d81d 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1318,7 +1318,7 @@ void the_game(
*/
Sky *sky = NULL;
- sky = new Sky(smgr->getRootSceneNode(), smgr, -1);
+ sky = new Sky(smgr->getRootSceneNode(), smgr, -1, client.getEnv().getLocalPlayer());
/*
A copy of the local inventory
diff --git a/src/sky.cpp b/src/sky.cpp
index e55dd1eb2..a9fb90122 100644
--- a/src/sky.cpp
+++ b/src/sky.cpp
@@ -8,16 +8,18 @@
#include "main.h" // g_profiler
#include "profiler.h"
#include "util/numeric.h" // MYMIN
+#include <cmath>
//! constructor
-Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id):
+Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player):
scene::ISceneNode(parent, mgr, id),
m_first_update(true),
m_brightness(0.5),
m_cloud_brightness(0.5),
m_bgcolor_bright_f(1,1,1,1),
m_skycolor_bright_f(1,1,1,1),
- m_cloudcolor_bright_f(1,1,1,1)
+ m_cloudcolor_bright_f(1,1,1,1),
+ m_player(player)
{
setAutomaticCulling(scene::EAC_OFF);
Box.MaxEdge.set(0,0,0);
@@ -431,7 +433,8 @@ void Sky::update(float time_of_day, float time_brightness,
video::SColor(255, 240,240,255);
//video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
//video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
- video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
+ //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
+ video::SColorf cloudcolor_bright_dawn_f(1.0, 0.875, 0.75);
float cloud_color_change_fraction = 0.95;
if(sunlight_seen){
@@ -470,41 +473,73 @@ void Sky::update(float time_of_day, float time_brightness,
} else {
m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
bgcolor_bright_indoor_f, color_change_fraction);
- m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
- cloudcolor_bright_normal_f, color_change_fraction);
m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
bgcolor_bright_indoor_f, color_change_fraction);
+ m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
+ cloudcolor_bright_normal_f, color_change_fraction);
m_clouds_visible = false;
}
+
+ // Horizon coloring based on sun and moon direction during sunset and sunrise
+ video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
+ if (m_horizon_blend() != 0)
+ {
+ // calculate hemisphere value from yaw
+ f32 pointcolor_blend = wrapDegrees_0_360(m_player->getYaw() + 90);
+ if (pointcolor_blend > 180)
+ pointcolor_blend = 360 - pointcolor_blend;
+ pointcolor_blend /= 180;
+ // bound view angle to determine where transition starts and ends
+ pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) * 1.375;
+ // combine the colors when looking up or down, otherwise turning looks weird
+ pointcolor_blend += (0.5 - pointcolor_blend) * (1 - MYMIN((90 - std::abs(m_player->getPitch())) / 90 * 1.5, 1));
+ // invert direction to match where the sun and moon are rising
+ if (m_time_of_day > 0.5)
+ pointcolor_blend = 1 - pointcolor_blend;
+
+ // horizon colors of sun and moon
+ f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
+ video::SColorf pointcolor_sun_f(1, 1, 1, 1);
+ pointcolor_sun_f.r = pointcolor_light * 1;
+ pointcolor_sun_f.b = pointcolor_light * (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
+ pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 + (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
+ video::SColorf pointcolor_moon_f(0.5 * pointcolor_light, 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
+ video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
+ video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
+ // calculate the blend color
+ pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
+ }
+
video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
m_bgcolor = video::SColor(
- 255,
- bgcolor_bright.getRed() * m_brightness,
- bgcolor_bright.getGreen() * m_brightness,
- bgcolor_bright.getBlue() * m_brightness);
-
+ 255,
+ bgcolor_bright.getRed() * m_brightness,
+ bgcolor_bright.getGreen() * m_brightness,
+ bgcolor_bright.getBlue() * m_brightness);
+ m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
+
video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
m_skycolor = video::SColor(
- 255,
- skycolor_bright.getRed() * m_brightness,
- skycolor_bright.getGreen() * m_brightness,
- skycolor_bright.getBlue() * m_brightness);
-
+ 255,
+ skycolor_bright.getRed() * m_brightness,
+ skycolor_bright.getGreen() * m_brightness,
+ skycolor_bright.getBlue() * m_brightness);
+ m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
+
float cloud_direct_brightness = 0;
if(sunlight_seen){
- cloud_direct_brightness = time_brightness;
- if(time_brightness >= 0.2 && time_brightness < 0.7)
- cloud_direct_brightness *= 1.3;
+ cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 + m_time_brightness, 1);
} else {
cloud_direct_brightness = direct_brightness;
}
m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
m_cloudcolor_f = video::SColorf(
- m_cloudcolor_bright_f.getRed() * m_cloud_brightness,
- m_cloudcolor_bright_f.getGreen() * m_cloud_brightness,
- m_cloudcolor_bright_f.getBlue() * m_cloud_brightness,
+ m_cloudcolor_bright_f.r * m_cloud_brightness,
+ m_cloudcolor_bright_f.g * m_cloud_brightness,
+ m_cloudcolor_bright_f.b * m_cloud_brightness,
1.0);
+ m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f, video::SColorf(pointcolor), m_horizon_blend() * 0.75);
}
diff --git a/src/sky.h b/src/sky.h
index 7b7295e4e..35c86db93 100644
--- a/src/sky.h
+++ b/src/sky.h
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include <ISceneNode.h>
+#include "localplayer.h"
#ifndef SKY_HEADER
#define SKY_HEADER
@@ -31,7 +32,7 @@ class Sky : public scene::ISceneNode
{
public:
//! constructor
- Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id);
+ Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player);
virtual void OnRegisterSceneNode();
@@ -61,7 +62,42 @@ public:
private:
core::aabbox3d<f32> Box;
video::SMaterial m_materials[SKY_MATERIAL_COUNT];
-
+
+ // How much sun & moon transition should affect horizon color
+ float m_horizon_blend()
+ {
+ if (!m_sunlight_seen)
+ return 0;
+ float x; m_time_of_day >= 0.5 ? x = (1 - m_time_of_day) * 2 : x = m_time_of_day * 2;
+ if (x <= 0.3)
+ return 0;
+ if (x <= 0.4) // when the sun and moon are aligned
+ return (x - 0.3) * 10;
+ if (x <= 0.5)
+ return (0.5 - x) * 10;
+ return 0;
+ }
+
+ // Mix two colors by a given amount
+ video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
+ {
+ video::SColor result = video::SColor(
+ col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
+ col1.getRed() * (1 - factor) + col2.getRed() * factor,
+ col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
+ col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
+ return result;
+ }
+ video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
+ {
+ video::SColorf result = video::SColorf(
+ col1.r * (1 - factor) + col2.r * factor,
+ col1.g * (1 - factor) + col2.g * factor,
+ col1.b * (1 - factor) + col2.b * factor,
+ col1.a * (1 - factor) + col2.a * factor);
+ return result;
+ }
+
bool m_first_update;
float m_time_of_day;
float m_time_brightness;
@@ -78,6 +114,7 @@ private:
v3f m_stars[SKY_STAR_COUNT];
u16 m_star_indices[SKY_STAR_COUNT*4];
video::S3DVertex m_star_vertices[SKY_STAR_COUNT*4];
+ LocalPlayer* m_player;
};
#endif
ss="hl opt">.end(); it++) { (*it)->wait(); } // Force kill all threads for (std::vector<AsyncWorkerThread *>::iterator it = workerThreads.begin(); it != workerThreads.end(); it++) { delete *it; } jobQueueMutex.lock(); jobQueue.clear(); jobQueueMutex.unlock(); workerThreads.clear(); } /******************************************************************************/ bool AsyncEngine::registerFunction(const char* name, lua_CFunction func) { if (initDone) { return false; } functionList[name] = func; return true; } /******************************************************************************/ void AsyncEngine::initialize(unsigned int numEngines) { initDone = true; for (unsigned int i = 0; i < numEngines; i++) { AsyncWorkerThread *toAdd = new AsyncWorkerThread(this, std::string("AsyncWorker-") + itos(i)); workerThreads.push_back(toAdd); toAdd->start(); } } /******************************************************************************/ unsigned int AsyncEngine::queueAsyncJob(std::string func, std::string params) { jobQueueMutex.lock(); LuaJobInfo toAdd; toAdd.id = jobIdCounter++; toAdd.serializedFunction = func; toAdd.serializedParams = params; jobQueue.push_back(toAdd); jobQueueCounter.post(); jobQueueMutex.unlock(); return toAdd.id; } /******************************************************************************/ LuaJobInfo AsyncEngine::getJob() { jobQueueCounter.wait(); jobQueueMutex.lock(); LuaJobInfo retval; retval.valid = false; if (!jobQueue.empty()) { retval = jobQueue.front(); jobQueue.pop_front(); retval.valid = true; } jobQueueMutex.unlock(); return retval; } /******************************************************************************/ void AsyncEngine::putJobResult(LuaJobInfo result) { resultQueueMutex.lock(); resultQueue.push_back(result); resultQueueMutex.unlock(); } /******************************************************************************/ void AsyncEngine::step(lua_State *L) { int error_handler = PUSH_ERROR_HANDLER(L); lua_getglobal(L, "core"); resultQueueMutex.lock(); while (!resultQueue.empty()) { LuaJobInfo jobDone = resultQueue.front(); resultQueue.pop_front(); lua_getfield(L, -1, "async_event_handler"); if (lua_isnil(L, -1)) { FATAL_ERROR("Async event handler does not exist!"); } luaL_checktype(L, -1, LUA_TFUNCTION); lua_pushinteger(L, jobDone.id); lua_pushlstring(L, jobDone.serializedResult.data(), jobDone.serializedResult.size()); PCALL_RESL(L, lua_pcall(L, 2, 0, error_handler)); } resultQueueMutex.unlock(); lua_pop(L, 2); // Pop core and error handler } /******************************************************************************/ void AsyncEngine::pushFinishedJobs(lua_State* L) { // Result Table MutexAutoLock l(resultQueueMutex); unsigned int index = 1; lua_createtable(L, resultQueue.size(), 0); int top = lua_gettop(L); while (!resultQueue.empty()) { LuaJobInfo jobDone = resultQueue.front(); resultQueue.pop_front(); lua_createtable(L, 0, 2); // Pre-allocate space for two map fields int top_lvl2 = lua_gettop(L); lua_pushstring(L, "jobid"); lua_pushnumber(L, jobDone.id); lua_settable(L, top_lvl2); lua_pushstring(L, "retval"); lua_pushlstring(L, jobDone.serializedResult.data(), jobDone.serializedResult.size()); lua_settable(L, top_lvl2); lua_rawseti(L, top, index++); } } /******************************************************************************/ void AsyncEngine::prepareEnvironment(lua_State* L, int top) { for (std::map<std::string, lua_CFunction>::iterator it = functionList.begin(); it != functionList.end(); it++) { lua_pushstring(L, it->first.c_str()); lua_pushcfunction(L, it->second); lua_settable(L, top); } } /******************************************************************************/ AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name) : Thread(name), ScriptApiBase(), jobDispatcher(jobDispatcher) { lua_State *L = getStack(); // Prepare job lua environment lua_getglobal(L, "core"); int top = lua_gettop(L); // Push builtin initialization type lua_pushstring(L, "async"); lua_setglobal(L, "INIT"); jobDispatcher->prepareEnvironment(L, top); } /******************************************************************************/ AsyncWorkerThread::~AsyncWorkerThread() { sanity_check(!isRunning()); } /******************************************************************************/ void* AsyncWorkerThread::run() { lua_State *L = getStack(); std::string script = getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua"; try { loadScript(script); } catch (const ModError &e) { errorstream << "Execution of async base environment failed: " << e.what() << std::endl; FATAL_ERROR("Execution of async base environment failed"); } int error_handler = PUSH_ERROR_HANDLER(L); lua_getglobal(L, "core"); if (lua_isnil(L, -1)) { FATAL_ERROR("Unable to find core within async environment!"); } // Main loop while (!stopRequested()) { // Wait for job