From: Perttu Ahola Date: Fri, 16 Mar 2012 14:34:30 +0000 (+0200) Subject: Dynamic sky, fog and cloud colors; sun and moon X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=2e90ed07acd295387c0da6c0689d14665b6c125d;p=zefram%2Fminetest%2Fminetest_engine.git Dynamic sky, fog and cloud colors; sun and moon --- diff --git a/share/client/textures/sunrisebg.png b/share/client/textures/sunrisebg.png new file mode 100644 index 00000000..d8b010eb Binary files /dev/null and b/share/client/textures/sunrisebg.png differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox1.png b/share/server/games/mesetint/mods/default/textures/skybox1.png deleted file mode 100644 index 9801d5f4..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox1.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox1_dawn.png b/share/server/games/mesetint/mods/default/textures/skybox1_dawn.png deleted file mode 100644 index 9711c478..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox1_dawn.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox1_night.png b/share/server/games/mesetint/mods/default/textures/skybox1_night.png deleted file mode 100644 index 32e43a63..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox1_night.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox2.png b/share/server/games/mesetint/mods/default/textures/skybox2.png deleted file mode 100644 index a8c94b4d..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox2.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox2_dawn.png b/share/server/games/mesetint/mods/default/textures/skybox2_dawn.png deleted file mode 100644 index a761dff2..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox2_dawn.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox2_night.png b/share/server/games/mesetint/mods/default/textures/skybox2_night.png deleted file mode 100644 index beb07a9c..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox2_night.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox3.png b/share/server/games/mesetint/mods/default/textures/skybox3.png deleted file mode 100644 index 2776ec71..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox3.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox3_dawn.png b/share/server/games/mesetint/mods/default/textures/skybox3_dawn.png deleted file mode 100644 index 22c8cbef..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox3_dawn.png and /dev/null differ diff --git a/share/server/games/mesetint/mods/default/textures/skybox3_night.png b/share/server/games/mesetint/mods/default/textures/skybox3_night.png deleted file mode 100644 index bb509789..00000000 Binary files a/share/server/games/mesetint/mods/default/textures/skybox3_night.png and /dev/null differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e43ff411..d3d9d64d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -159,6 +159,7 @@ endif() # Client sources set(minetest_SRCS ${common_SRCS} + sky.cpp clientmap.cpp content_cso.cpp content_mapblock.cpp diff --git a/src/camera.cpp b/src/camera.cpp index 53166167..97571c77 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -375,8 +375,12 @@ void Camera::updateViewingRange(f32 frametime_in) m_draw_control.wanted_range = viewing_range_max; // Just so big a value that everything rendered is visible - // Some more allowance than viewing_range_max * BS because of active objects etc. - m_cameranode->setFarValue(viewing_range_max * BS * 10); + // Some more allowance than viewing_range_max * BS because of clouds, + // active objects, etc. + if(viewing_range_max < 200*BS) + m_cameranode->setFarValue(200 * BS * 10); + else + m_cameranode->setFarValue(viewing_range_max * BS * 10); f32 wanted_fps = g_settings->getFloat("wanted_fps"); wanted_fps = MYMAX(wanted_fps, 1.0); diff --git a/src/client.cpp b/src/client.cpp index 6d992e71..2148bfe5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -247,14 +247,16 @@ Client::Client( m_animation_time(0), m_crack_level(-1), m_crack_pos(0,0,0), - m_time_of_day(0), m_map_seed(0), m_password(password), m_access_denied(false), m_texture_receive_progress(0), m_textures_received(false), m_itemdef_received(false), - m_nodedef_received(false) + m_nodedef_received(false), + m_time_of_day_set(false), + m_last_time_of_day_f(-1), + m_time_of_day_update_timer(0) { m_packetcounter_timer = 0.0; //m_delete_unused_sectors_timer = 0.0; @@ -333,6 +335,8 @@ void Client::step(float dtime) if(m_animation_time > 60.0) m_animation_time -= 60.0; + m_time_of_day_update_timer += dtime; + //infostream<<"Client steps "< +#include #include "log.h" #include "mapsector.h" #include "main.h" // dout_client, g_settings @@ -29,6 +30,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "profiler.h" #include "settings.h" +#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" + ClientMap::ClientMap( Client *client, IGameDef *gamedef, @@ -189,7 +192,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) */ float animation_time = m_client->getAnimationTime(); int crack = m_client->getCrackLevel(); - u32 daynight_ratio = m_client->getDayNightRatio(); + u32 daynight_ratio = m_client->getEnv().getDayNightRatio(); m_camera_mutex.Lock(); v3f camera_position = m_camera_position; @@ -509,6 +512,182 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) <<", rendered "<getNodeNoEx(p); + if(ndef->get(n).param_type == CPT_LIGHT && + !ndef->get(n).sunlight_propagates) + allow_allowing_non_sunlight_propagates = true; + } + // If would start at CONTENT_IGNORE, start closer + { + v3s16 p = floatToInt(pf, BS); + MapNode n = map->getNodeNoEx(p); + if(n.getContent() == CONTENT_IGNORE){ + float newd = 2*BS; + pf = p0 + dir * 2*newd; + distance = newd; + } + } + for(int i=0; distance < end_distance; i++){ + pf += dir * step; + distance += step; + step *= step_multiplier; + + v3s16 p = floatToInt(pf, BS); + MapNode n = map->getNodeNoEx(p); + if(allow_allowing_non_sunlight_propagates && i == 0 && + ndef->get(n).param_type == CPT_LIGHT && + !ndef->get(n).sunlight_propagates){ + allow_non_sunlight_propagates = true; + } + if(ndef->get(n).param_type != CPT_LIGHT || + (!ndef->get(n).sunlight_propagates && + !allow_non_sunlight_propagates)){ + nonlight_seen = true; + noncount++; + if(noncount >= 4) + break; + continue; + } + if(distance >= sunlight_min_d && *sunlight_seen == false + && nonlight_seen == false) + if(n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN) + *sunlight_seen = true; + noncount = 0; + brightness_sum += decode_light(n.getLightBlend(daylight_factor, ndef)); + brightness_count++; + } + *result = 0; + if(brightness_count == 0) + return false; + *result = brightness_sum / brightness_count; + /*std::cerr<<"Sampled "< 35*BS) + sunlight_min_d = 35*BS; + core::array values; + for(u32 i=0; i a; + a.buildRotateFromTo(v3f(0,1,0), z_dir); + v3f dir = m_camera_direction; + a.rotateVect(dir); + int br = 0; + float step = BS*1.5; + if(max_d > 35*BS) + step = max_d / 35 * 1.5; + float off = step * z_offsets[i]; + bool sunlight_seen_now = false; + bool ok = getVisibleBrightness(this, m_camera_position, dir, + step, 1.0, max_d*0.6+off, max_d, ndef, daylight_factor, + sunlight_min_d, + &br, &sunlight_seen_now); + if(sunlight_seen_now) + sunlight_seen_count++; + if(!ok) + continue; + values.push_back(br); + // Don't try too much if being in the sun is clear + if(sunlight_seen_count >= 20) + break; + } + int brightness_sum = 0; + int brightness_count = 0; + values.sort(); + u32 num_values_to_use = values.size(); + if(num_values_to_use >= 10) + num_values_to_use -= num_values_to_use/2; + else if(num_values_to_use >= 7) + num_values_to_use -= num_values_to_use/3; + u32 first_value_i = (values.size() - num_values_to_use) / 2; + if(debugprint){ + for(u32 i=0; i < first_value_i; i++) + std::cerr<get(n).param_type == CPT_LIGHT){ + ret = decode_light(n.getLightBlend(daylight_factor, ndef)); + } else { + ret = oldvalue; + //ret = blend_light(255, 0, daylight_factor); + } + } else { + /*float pre = (float)brightness_sum / (float)brightness_count; + float tmp = pre; + const float d = 0.2; + pre *= 1.0 + d*2; + pre -= tmp * d; + int preint = pre; + ret = MYMAX(0, MYMIN(255, preint));*/ + ret = brightness_sum / brightness_count; + } + if(debugprint) + std::cerr<<"Result: "<getBool("enable_3d_clouds")) - num_faces_to_draw = 6; + + bool enable_3d = g_settings->getBool("enable_3d_clouds"); + int num_faces_to_draw = enable_3d ? 6 : 1; + + m_material.setFlag(video::EMF_BACK_FACE_CULLING, enable_3d); driver->setTransform(video::ETS_WORLD, AbsoluteTransformation); driver->setMaterial(m_material); @@ -90,8 +93,10 @@ void Clouds::render() */ const s16 cloud_radius_i = 12; - const float cloud_size = BS*48; - const v2f cloud_speed(-BS*2, 0); + const float cloud_size = BS*64; + const v2f cloud_speed(0, -BS*2); + + const float cloud_full_radius = cloud_size * cloud_radius_i; // Position of cloud noise origin in world coordinates v2f world_cloud_origin_pos_f = m_time*cloud_speed; @@ -110,33 +115,103 @@ void Clouds::render() center_of_drawing_in_noise_i.Y * cloud_size ) + world_cloud_origin_pos_f; + /*video::SColor c_top(128,b*240,b*240,b*255); + video::SColor c_side_1(128,b*230,b*230,b*255); + video::SColor c_side_2(128,b*220,b*220,b*245); + video::SColor c_bottom(128,b*205,b*205,b*230);*/ + video::SColorf c_top_f(m_color); + video::SColorf c_side_1_f(m_color); + video::SColorf c_side_2_f(m_color); + video::SColorf c_bottom_f(m_color); + c_side_1_f.r *= 0.95; + c_side_1_f.g *= 0.95; + c_side_1_f.b *= 0.95; + c_side_2_f.r *= 0.90; + c_side_2_f.g *= 0.90; + c_side_2_f.b *= 0.90; + c_bottom_f.r *= 0.80; + c_bottom_f.g *= 0.80; + c_bottom_f.b *= 0.80; + c_top_f.a = 0.9; + c_side_1_f.a = 0.9; + c_side_2_f.a = 0.9; + c_bottom_f.a = 0.9; + video::SColor c_top = c_top_f.toSColor(); + video::SColor c_side_1 = c_side_1_f.toSColor(); + video::SColor c_side_2 = c_side_2_f.toSColor(); + video::SColor c_bottom = c_bottom_f.toSColor(); + + // Get fog parameters for setting them back later + video::SColor fog_color(0,0,0,0); + video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR; + f32 fog_start = 0; + f32 fog_end = 0; + f32 fog_density = 0; + bool fog_pixelfog = false; + bool fog_rangefog = false; + driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density, + fog_pixelfog, fog_rangefog); + + // Set our own fog + driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5, + cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog); + + // Read noise + + bool *grid = new bool[cloud_radius_i*2*cloud_radius_i*2]; + for(s16 zi=-cloud_radius_i; zi= 0.80); +#endif +#if 1 + double noise = noise2d_perlin( + (float)p_in_noise_i.X*cloud_size/BS/200, + (float)p_in_noise_i.Y*cloud_size/BS/200, + m_seed, 3, 0.5); + grid[i] = (noise >= 0.4); +#endif + } + +#define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius)) +#define CONTAINS(x, z, radius) \ + ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius)) + + for(s16 zi0=-cloud_radius_i; zi0= 0) + zi = cloud_radius_i - zi - 1; + if(xi >= 0) + xi = cloud_radius_i - xi - 1; + + u32 i = GETINDEX(xi, zi, cloud_radius_i); + + if(grid[i] == false) continue; - float b = m_brightness; - video::SColor c_top(128,b*240,b*240,b*255); - video::SColor c_side_1(128,b*230,b*230,b*255); - video::SColor c_side_2(128,b*220,b*220,b*245); - video::SColor c_bottom(128,b*205,b*205,b*230); + v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f; video::S3DVertex v[4] = { @@ -146,55 +221,95 @@ void Clouds::render() video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0) }; - f32 rx = cloud_size; + /*if(zi <= 0 && xi <= 0){ + v[0].Color.setBlue(255); + v[1].Color.setBlue(255); + v[2].Color.setBlue(255); + v[3].Color.setBlue(255); + }*/ + + f32 rx = cloud_size/2; f32 ry = 8*BS; - f32 rz = cloud_size; + f32 rz = cloud_size/2; for(int i=0; idrawVertexPrimitiveList(v, 4, indices, 2, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT); } - } + + delete[] grid; + + // Restore fog settings + driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density, + fog_pixelfog, fog_rangefog); } void Clouds::step(float dtime) @@ -219,9 +339,11 @@ void Clouds::step(float dtime) m_time += dtime; } -void Clouds::update(v2f camera_p, float brightness) +void Clouds::update(v2f camera_p, video::SColorf color) { m_camera_pos = camera_p; - m_brightness = brightness; + m_color = color; + //m_brightness = brightness; + //dstream<<"m_brightness="< m_box; float m_cloud_y; float m_brightness; + video::SColorf m_color; u32 m_seed; v2f m_camera_pos; float m_time; diff --git a/src/daynightratio.h b/src/daynightratio.h new file mode 100644 index 00000000..1b075c4f --- /dev/null +++ b/src/daynightratio.h @@ -0,0 +1,43 @@ +/* +Minetest-c55 +Copyright (C) 2010-2012 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef DAYNIGHTRATIO_HEADER +#define DAYNIGHTRATIO_HEADER + +inline u32 time_to_daynight_ratio(u32 time_of_day) +{ + s32 t = time_of_day%24000; + if(t < 4500 || t >= 19500) + return 150; + else if(t < 4750 || t >= 19250) + return 250; + else if(t < 5000 || t >= 19000) + return 350; + else if(t < 5250 || t >= 18750) + return 500; + else if(t < 5500 || t >= 18500) + return 675; + else if(t < 5750 || t >= 18250) + return 875; + else + return 1000; +} + +#endif + diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 97b00d42..a777ed73 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -71,7 +71,7 @@ void set_default_settings(Settings *settings) settings->setDefault("fps_max", "60"); // A bit more than the server will send around the player, to make fog blend well settings->setDefault("viewing_range_nodes_min", "35"); - settings->setDefault("viewing_range_nodes_max", "128"); + settings->setDefault("viewing_range_nodes_max", "160"); settings->setDefault("screenW", "800"); settings->setDefault("screenH", "600"); settings->setDefault("address", ""); @@ -121,11 +121,11 @@ void set_default_settings(Settings *settings) settings->setDefault("active_block_range", "2"); //settings->setDefault("max_simultaneous_block_sends_per_client", "1"); // This causes frametime jitter on client side, or does it? - settings->setDefault("max_simultaneous_block_sends_per_client", "2"); - settings->setDefault("max_simultaneous_block_sends_server_total", "8"); - settings->setDefault("max_block_send_distance", "7"); - settings->setDefault("max_block_generate_distance", "5"); - settings->setDefault("time_send_interval", "20"); + settings->setDefault("max_simultaneous_block_sends_per_client", "4"); + settings->setDefault("max_simultaneous_block_sends_server_total", "20"); + settings->setDefault("max_block_send_distance", "10"); + settings->setDefault("max_block_generate_distance", "6"); + settings->setDefault("time_send_interval", "5"); settings->setDefault("time_speed", "96"); settings->setDefault("server_unload_unused_data_timeout", "29"); settings->setDefault("server_map_save_interval", "5.3"); diff --git a/src/environment.cpp b/src/environment.cpp index 93649eed..e713ab61 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -41,11 +41,15 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef SERVER #include "clientmap.h" #endif +#include "daynightratio.h" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" Environment::Environment(): - m_time_of_day(9000) + m_time_of_day(9000), + m_time_of_day_f(9000./24000), + m_time_of_day_speed(0), + m_time_counter(0) { } @@ -195,17 +199,35 @@ void Environment::printPlayers(std::ostream &o) } } -/*void Environment::setDayNightRatio(u32 r) -{ - getDayNightRatio() = r; -}*/ - u32 Environment::getDayNightRatio() { - //return getDayNightRatio(); return time_to_daynight_ratio(m_time_of_day); } +void Environment::stepTimeOfDay(float dtime) +{ + m_time_counter += dtime; + f32 speed = m_time_of_day_speed * 24000./(24.*3600); + u32 units = (u32)(m_time_counter*speed); + m_time_counter -= (f32)units / speed; + bool sync_f = false; + if(units > 0){ + // Sync at overflow + if(m_time_of_day + units >= 24000) + sync_f = true; + m_time_of_day = (m_time_of_day + units) % 24000; + if(sync_f) + m_time_of_day_f = (float)m_time_of_day / 24000.0; + } + if(!sync_f){ + m_time_of_day_f += m_time_of_day_speed/24/3600*dtime; + if(m_time_of_day_f > 1.0) + m_time_of_day_f -= 1.0; + if(m_time_of_day_f < 0.0) + m_time_of_day_f += 1.0; + } +} + /* ABMWithState */ @@ -893,6 +915,9 @@ void ServerEnvironment::step(float dtime) //TimeTaker timer("ServerEnv step"); + /* Step time of day */ + stepTimeOfDay(dtime); + /* Increment game time */ @@ -1861,6 +1886,9 @@ void ClientEnvironment::step(float dtime) { DSTACK(__FUNCTION_NAME); + /* Step time of day */ + stepTimeOfDay(dtime); + // Get some settings bool free_move = g_settings->getBool("free_move"); diff --git a/src/environment.h b/src/environment.h index a3a659c5..04df9be4 100644 --- a/src/environment.h +++ b/src/environment.h @@ -74,27 +74,39 @@ public: core::list getPlayers(bool ignore_disconnected); void printPlayers(std::ostream &o); - //void setDayNightRatio(u32 r); u32 getDayNightRatio(); // 0-23999 virtual void setTimeOfDay(u32 time) { m_time_of_day = time; + m_time_of_day_f = (float)time / 24000.0; } u32 getTimeOfDay() - { - return m_time_of_day; - } + { return m_time_of_day; } + + float getTimeOfDayF() + { return m_time_of_day_f; } + + void stepTimeOfDay(float dtime); + + void setTimeOfDaySpeed(float speed) + { m_time_of_day_speed = speed; } + + float getTimeOfDaySpeed() + { return m_time_of_day_speed; } protected: // peer_ids in here should be unique, except that there may be many 0s core::list m_players; - // Brightness - //u32 m_daynight_ratio; // Time of day in milli-hours (0-23999); determines day and night u32 m_time_of_day; + // Time of day in 0...1 + float m_time_of_day_f; + float m_time_of_day_speed; + // Used to buffer dtime for adding to m_time_of_day + float m_time_counter; }; /* diff --git a/src/game.cpp b/src/game.cpp index 778615cf..04b9f105 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -54,6 +54,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "subgame.h" #include "quicktune_shortcutter.h" #include "clientmap.h" +#include "sky.h" /* Setting this to 1 enables a special camera mode that forces @@ -543,51 +544,6 @@ PointedThing getPointedThing(Client *client, v3f player_position, return result; } -void update_skybox(video::IVideoDriver* driver, ITextureSource *tsrc, - scene::ISceneManager* smgr, scene::ISceneNode* &skybox, - float brightness) -{ - if(skybox) - { - skybox->remove(); - } - - /*// Disable skybox if FarMesh is enabled - if(g_settings->getBool("enable_farmesh")) - return;*/ - - if(brightness >= 0.7) - { - skybox = smgr->addSkyBoxSceneNode( - tsrc->getTextureRaw("skybox2.png"), - tsrc->getTextureRaw("skybox3.png"), - tsrc->getTextureRaw("skybox1.png"), - tsrc->getTextureRaw("skybox1.png"), - tsrc->getTextureRaw("skybox1.png"), - tsrc->getTextureRaw("skybox1.png")); - } - else if(brightness >= 0.2) - { - skybox = smgr->addSkyBoxSceneNode( - tsrc->getTextureRaw("skybox2_dawn.png"), - tsrc->getTextureRaw("skybox3_dawn.png"), - tsrc->getTextureRaw("skybox1_dawn.png"), - tsrc->getTextureRaw("skybox1_dawn.png"), - tsrc->getTextureRaw("skybox1_dawn.png"), - tsrc->getTextureRaw("skybox1_dawn.png")); - } - else - { - skybox = smgr->addSkyBoxSceneNode( - tsrc->getTextureRaw("skybox2_night.png"), - tsrc->getTextureRaw("skybox3_night.png"), - tsrc->getTextureRaw("skybox1_night.png"), - tsrc->getTextureRaw("skybox1_night.png"), - tsrc->getTextureRaw("skybox1_night.png"), - tsrc->getTextureRaw("skybox1_night.png")); - } -} - /* Draws a screen with a single text on it. Text will be removed when the screen is drawn the next time. @@ -679,10 +635,6 @@ void the_game( //const s32 hotbar_imagesize = 64; s32 hotbar_imagesize = 48; - // The color of the sky - //video::SColor skycolor = video::SColor(255,140,186,250); - video::SColor bgcolor_bright = video::SColor(255,170,200,230); - /* Draw "Loading" screen */ @@ -891,13 +843,6 @@ void the_game( */ client.afterContentReceived(); - /* - Create skybox - */ - float old_brightness = 1.0; - scene::ISceneNode* skybox = NULL; - update_skybox(driver, tsrc, smgr, skybox, 1.0); - /* Create the camera node */ @@ -912,13 +857,18 @@ void the_game( Clouds */ - float cloud_height = BS*100; Clouds *clouds = NULL; if(g_settings->getBool("enable_clouds")) { - clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1, - cloud_height, time(0)); + clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1, time(0)); } + + /* + Skybox thingy + */ + + Sky *sky = NULL; + sky = new Sky(smgr->getRootSceneNode(), smgr, -1); /* FarMesh @@ -1022,7 +972,7 @@ void the_game( // A test //throw con::PeerNotFoundException("lol"); - float brightness = 1.0; + float recent_turn_speed = 0.0; core::list frametime_log; @@ -1054,6 +1004,9 @@ void the_game( u32 show_profiler = 0; u32 show_profiler_max = 3; // Number of pages + float time_of_day = 0; + float time_of_day_smooth = 0; + /* Main loop */ @@ -1639,6 +1592,7 @@ void the_game( NOTE: Do this before client.setPlayerControl() to not cause a camera lag of one frame */ + float turn_amount = 0; if((device->isWindowActive() && noMenuActive()) || random_input) { if(!random_input) @@ -1668,11 +1622,14 @@ void the_game( dx -= dtime * keyspeed; if(input->isKeyDown(irr::KEY_RIGHT)) dx += dtime * keyspeed;*/ - - camera_yaw -= dx*0.2; - camera_pitch += dy*0.2; + + float d = 0.2; + camera_yaw -= dx*d; + camera_pitch += dy*d; if(camera_pitch < -89.5) camera_pitch = -89.5; if(camera_pitch > 89.5) camera_pitch = 89.5; + + turn_amount = v2f(dx, dy).getLength() * d; } input->setMousePos(displaycenter.X, displaycenter.Y); } @@ -1684,6 +1641,8 @@ void the_game( //infostream<<"window inactive"<getBrightness(); + direct_brightness = (float)client.getEnv().getClientMap() + .getBackgroundBrightness(MYMIN(fog_range*1.2, 60*BS), + daynight_ratio, (int)(old_brightness*255.5), &sunlight_seen) + / 255.0; + } + + time_of_day = client.getEnv().getTimeOfDayF(); + float maxsm = 0.05; + if(fabs(time_of_day - time_of_day_smooth) > maxsm && + fabs(time_of_day - time_of_day_smooth + 1.0) > maxsm && + fabs(time_of_day - time_of_day_smooth - 1.0) > maxsm) + time_of_day_smooth = time_of_day; + float todsm = 0.05; + if(time_of_day_smooth > 0.8 && time_of_day < 0.2) + time_of_day_smooth = time_of_day_smooth * (1.0-todsm) + + (time_of_day+1.0) * todsm; + else + time_of_day_smooth = time_of_day_smooth * (1.0-todsm) + + time_of_day * todsm; + + sky->update(time_of_day_smooth, time_brightness, direct_brightness, + sunlight_seen); + + float brightness = sky->getBrightness(); + video::SColor bgcolor = sky->getBgColor(); + video::SColor skycolor = sky->getSkyColor(); /* Update clouds */ - if(clouds) - { - clouds->step(dtime); - clouds->update(v2f(player_position.X, player_position.Z), - brightness); + if(clouds){ + if(sky->getCloudsVisible()){ + clouds->setVisible(true); + clouds->step(dtime); + clouds->update(v2f(player_position.X, player_position.Z), + sky->getCloudColor()); + } else{ + clouds->setVisible(false); + } } /* @@ -2208,35 +2201,17 @@ void the_game( brightness, farmesh_range); } - // Store brightness value - old_brightness = brightness; - /* Fog */ if(g_settings->getBool("enable_fog") == true && !force_fog_off) { - f32 range; - if(farmesh) - { - range = BS*farmesh_range; - } - else - { - range = draw_control.wanted_range*BS + 0.0*MAP_BLOCKSIZE*BS; - range *= 0.9; - if(draw_control.range_all) - range = 100000*BS; - /*if(range < 50*BS) - range = range * 0.5 + 25*BS;*/ - } - driver->setFog( bgcolor, video::EFT_FOG_LINEAR, - range*0.4, - range*1.0, + fog_range*0.4, + fog_range*1.0, 0.01, false, // pixel fog false // range fog @@ -2443,15 +2418,15 @@ void the_game( { TimeTaker timer("beginScene"); - driver->beginScene(false, true, bgcolor); //driver->beginScene(false, true, bgcolor); + //driver->beginScene(true, true, bgcolor); + driver->beginScene(true, true, skycolor); beginscenetime = timer.stop(true); } //timer3.stop(); - + //infostream<<"smgr->drawAll()"<drawAll(); @@ -2570,6 +2545,11 @@ void the_game( NULL); } + // Clear Z buffer + driver->clearZBuffer(); + // Draw some sky things + //draw_horizon(driver, camera.getCameraNode()); + /* End scene */ diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index e8ebecb5..27ab2950 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -46,6 +46,7 @@ extern "C" { #include "content_nodemeta.h" #include "utility.h" #include "tool.h" +#include "daynightratio.h" static void stackDump(lua_State *L, std::ostream &o) { diff --git a/src/server.cpp b/src/server.cpp index 2139df95..f3bf1e23 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -854,7 +854,6 @@ Server::Server( m_craftdef(createCraftDefManager()), m_thread(this), m_emergethread(this), - m_time_counter(0), m_time_of_day_send_timer(0), m_uptime(0), m_shutdown_requested(false), @@ -1197,19 +1196,12 @@ void Server::AsyncRunStep() } /* - Update m_time_of_day and overall game time + Update time of day and overall game time */ { JMutexAutoLock envlock(m_env_mutex); - m_time_counter += dtime; - f32 speed = g_settings->getFloat("time_speed") * 24000./(24.*3600); - u32 units = (u32)(m_time_counter*speed); - m_time_counter -= (f32)units / speed; - - m_env->setTimeOfDay((m_env->getTimeOfDay() + units) % 24000); - - //infostream<<"Server: m_time_of_day = "<setTimeOfDaySpeed(g_settings->getFloat("time_speed")); /* Send to clients at constant intervals @@ -1231,7 +1223,7 @@ void Server::AsyncRunStep() //Player *player = m_env->getPlayer(client->peer_id); SharedBuffer data = makePacket_TOCLIENT_TIME_OF_DAY( - m_env->getTimeOfDay()); + m_env->getTimeOfDay(), g_settings->getFloat("time_speed")); // Send as reliable m_con.Send(client->peer_id, 0, data, true); } @@ -2216,7 +2208,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // Send time of day { SharedBuffer data = makePacket_TOCLIENT_TIME_OF_DAY( - m_env->getTimeOfDay()); + m_env->getTimeOfDay(), g_settings->getFloat("time_speed")); m_con.Send(peer_id, 0, data, true); } diff --git a/src/server.h b/src/server.h index f149ac68..32ce8eb5 100644 --- a/src/server.h +++ b/src/server.h @@ -426,11 +426,6 @@ public: core::list getPlayerInfo(); - /*u32 getDayNightRatio() - { - return time_to_daynight_ratio(m_time_of_day.get()); - }*/ - // Environment must be locked when called void setTimeOfDay(u32 time) { @@ -714,10 +709,6 @@ private: Time related stuff */ - // 0-23999 - //MutexedVariable m_time_of_day; - // Used to buffer dtime for adding to m_time_of_day - float m_time_counter; // Timer for sending time of day over network float m_time_of_day_send_timer; // Uptime of server in seconds diff --git a/src/sky.cpp b/src/sky.cpp new file mode 100644 index 00000000..ba9b1749 --- /dev/null +++ b/src/sky.cpp @@ -0,0 +1,446 @@ +#include "sky.h" +#include "IVideoDriver.h" +#include "ISceneManager.h" +#include "ICameraSceneNode.h" +#include "S3DVertex.h" +#include "utility.h" // MYMIN +#include "tile.h" // getTexturePath +#include "noise.h" // easeCurve +#include "main.h" // g_profiler +#include "profiler.h" + +//! constructor +Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id): + 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) +{ + setAutomaticCulling(scene::EAC_OFF); + Box.MaxEdge.set(0,0,0); + Box.MinEdge.set(0,0,0); + + // create material + + video::SMaterial mat; + mat.Lighting = false; + mat.ZBuffer = video::ECFN_NEVER; + mat.ZWriteEnable = false; + mat.AntiAliasing=0; + mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; + mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; + mat.BackfaceCulling = false; + + m_materials[0] = mat; + + m_materials[1] = mat; + //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA; + m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + + m_materials[2] = mat; + m_materials[2].setTexture(0, mgr->getVideoDriver()->getTexture( + getTexturePath("sunrisebg.png").c_str())); + m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR; + + for(u32 i=0; iregisterNodeForRendering(this, scene::ESNRP_SKY_BOX); + + scene::ISceneNode::OnRegisterSceneNode(); +} + +const core::aabbox3d& Sky::getBoundingBox() const +{ + return Box; +} + +//! renders the node. +void Sky::render() +{ + video::IVideoDriver* driver = SceneManager->getVideoDriver(); + scene::ICameraSceneNode* camera = SceneManager->getActiveCamera(); + + if (!camera || !driver) + return; + + ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG); + + // draw perspective skybox + + core::matrix4 translate(AbsoluteTransformation); + translate.setTranslation(camera->getAbsolutePosition()); + + // Draw the sky box between the near and far clip plane + const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f; + core::matrix4 scale; + scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance)); + + driver->setTransform(video::ETS_WORLD, translate * scale); + + if(m_sunlight_seen) + { + float sunsize = 0.07; + video::SColorf suncolor_f(1, 1, 0, 1); + suncolor_f.r = 1; + suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7+m_time_brightness*(0.5))); + suncolor_f.b = MYMAX(0.0, m_brightness*0.95); + video::SColorf suncolor2_f(1, 1, 1, 1); + suncolor_f.r = 1; + suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85+m_time_brightness*(0.5))); + suncolor_f.b = MYMAX(0.0, m_brightness); + + float moonsize = 0.04; + video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1); + video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1); + + float nightlength = 0.41; + float wn = nightlength / 2; + float wicked_time_of_day = 0; + if(m_time_of_day > wn && m_time_of_day < 1.0 - wn) + wicked_time_of_day = (m_time_of_day - wn)/(1.0-wn*2)*0.5 + 0.25; + else if(m_time_of_day < 0.5) + wicked_time_of_day = m_time_of_day / wn * 0.25; + else + wicked_time_of_day = 1.0 - ((1.0-m_time_of_day) / wn * 0.25); + /*std::cerr<<"time_of_day="< " + <<"wicked_time_of_day="<setMaterial(m_materials[2]); + { + float mid1 = 0.25; + float mid = (wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1)); + float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0; + float a = easeCurve(MYMAX(0, MYMIN(1, a_))); + //std::cerr<<"a_="< + +#ifndef SKY_HEADER +#define SKY_HEADER + +#define SKY_MATERIAL_COUNT 3 +#define SKY_STAR_COUNT 200 + +// Skybox, rendered with zbuffer turned off, before all other nodes. +class Sky : public scene::ISceneNode +{ +public: + //! constructor + Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id); + + virtual void OnRegisterSceneNode(); + + //! renders the node. + virtual void render(); + + virtual const core::aabbox3d& getBoundingBox() const; + + // Used by Irrlicht for optimizing rendering + virtual video::SMaterial& getMaterial(u32 i) + { return m_materials[i]; } + + // Used by Irrlicht for optimizing rendering + virtual u32 getMaterialCount() const + { return SKY_MATERIAL_COUNT; } + + void update(float m_time_of_day, float time_brightness, + float direct_brightness, bool sunlight_seen); + + float getBrightness(){ return m_brightness; } + video::SColor getBgColor(){ return m_bgcolor; } + video::SColor getSkyColor(){ return m_skycolor; } + + bool getCloudsVisible(){ return m_clouds_visible; } + video::SColorf getCloudColor(){ return m_cloudcolor_f; } + +private: + core::aabbox3d Box; + video::SMaterial m_materials[SKY_MATERIAL_COUNT]; + + bool m_first_update; + float m_time_of_day; + float m_time_brightness; + bool m_sunlight_seen; + float m_brightness; + float m_cloud_brightness; + bool m_clouds_visible; + video::SColorf m_bgcolor_bright_f; + video::SColorf m_skycolor_bright_f; + video::SColorf m_cloudcolor_bright_f; + video::SColor m_bgcolor; + video::SColor m_skycolor; + video::SColorf m_cloudcolor_f; + v3f m_stars[SKY_STAR_COUNT]; + u16 m_star_indices[SKY_STAR_COUNT*4]; + video::S3DVertex m_star_vertices[SKY_STAR_COUNT*4]; +}; + +#endif + diff --git a/src/utility.h b/src/utility.h index 15c01373..fbd76f23 100644 --- a/src/utility.h +++ b/src/utility.h @@ -1751,21 +1751,6 @@ std::string deSerializeJsonString(std::istream &is); // -inline u32 time_to_daynight_ratio(u32 time_of_day) -{ - s32 t = time_of_day%24000; - if(t < 4500 || t >= 19500) - return 150; - else if(t < 5000 || t >= 19000) - return 350; - else if(t < 5500 || t >= 18500) - return 500; - else if(t < 6000 || t >= 18000) - return 750; - else - return 1000; -} - // Random helper. Usually d=BS inline core::aabbox3d getNodeBox(v3s16 p, float d) {