/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2020 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <cmath>
#include "sky.h"
-#include "ITexture.h"
-#include "IVideoDriver.h"
-#include "ISceneManager.h"
-#include "ICameraSceneNode.h"
-#include "S3DVertex.h"
+#include <ITexture.h>
+#include <IVideoDriver.h>
+#include <ISceneManager.h>
+#include <ICameraSceneNode.h>
+#include <S3DVertex.h>
#include "client/tile.h"
#include "noise.h" // easeCurve
#include "profiler.h"
#include "util/numeric.h"
-#include <cmath>
#include "client/renderingengine.h"
#include "settings.h"
#include "camera.h" // CameraModes
-#include "config.h"
+
using namespace irr::core;
-Sky::Sky(s32 id, ITextureSource *tsrc) :
- scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
- RenderingEngine::get_scene_manager(), id)
+static video::SMaterial baseMaterial()
{
- setAutomaticCulling(scene::EAC_OFF);
- m_box.MaxEdge.set(0, 0, 0);
- m_box.MinEdge.set(0, 0, 0);
-
- // Create material
-
video::SMaterial mat;
mat.Lighting = false;
-#if ENABLE_GLES
+#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
mat.ZBuffer = video::ECFN_DISABLED;
+ mat.ZWriteEnable = video::EZW_OFF;
#else
+ mat.ZWriteEnable = false;
mat.ZBuffer = video::ECFN_NEVER;
#endif
- 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;
+ return mat;
+}
+
+static inline void disableTextureFiltering(video::SMaterial &mat)
+{
+ mat.setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
+ mat.setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
+ mat.setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
+}
+
+Sky::Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc) :
+ scene::ISceneNode(rendering_engine->get_scene_manager()->getRootSceneNode(),
+ rendering_engine->get_scene_manager(), id)
+{
+ m_seed = (u64)myrand() << 32 | myrand();
+
+ setAutomaticCulling(scene::EAC_OFF);
+ m_box.MaxEdge.set(0, 0, 0);
+ m_box.MinEdge.set(0, 0, 0);
- m_materials[0] = mat;
+ m_enable_shaders = g_settings->getBool("enable_shaders");
- m_materials[1] = mat;
- //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
+ m_sky_params = SkyboxDefaults::getSkyDefaults();
+ m_sun_params = SkyboxDefaults::getSunDefaults();
+ m_moon_params = SkyboxDefaults::getMoonDefaults();
+ m_star_params = SkyboxDefaults::getStarDefaults();
+
+ // Create materials
+
+ m_materials[0] = baseMaterial();
+ m_materials[0].MaterialType = ssrc->getShaderInfo(ssrc->getShader("stars_shader", TILE_MATERIAL_ALPHA)).material;
+ m_materials[0].Lighting = true;
+ m_materials[0].ColorMaterial = video::ECM_NONE;
+
+ m_materials[1] = baseMaterial();
m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- m_materials[2] = mat;
+ m_materials[2] = baseMaterial();
m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
-
- // Ensures that sun and moon textures and tonemaps are correct.
- setSkyDefaults();
- m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
- tsrc->getTextureForMesh(m_sun_params.texture) : NULL;
- m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
- tsrc->getTextureForMesh(m_moon_params.texture) : NULL;
- m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
- tsrc->getTexture(m_sun_params.tonemap) : NULL;
- m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
- tsrc->getTexture(m_moon_params.tonemap) : NULL;
- if (m_sun_texture) {
- m_materials[3] = mat;
- m_materials[3].setTexture(0, m_sun_texture);
- m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- // Use tonemaps if available
- if (m_sun_tonemap)
- m_materials[3].Lighting = true;
- }
- if (m_moon_texture) {
- m_materials[4] = mat;
- m_materials[4].setTexture(0, m_moon_texture);
- m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- // Use tonemaps if available
- if (m_moon_tonemap)
- m_materials[4].Lighting = true;
- }
+ setSunTexture(m_sun_params.texture, m_sun_params.tonemap, tsrc);
+
+ setMoonTexture(m_moon_params.texture, m_moon_params.tonemap, tsrc);
for (int i = 5; i < 11; i++) {
- m_materials[i] = mat;
+ m_materials[i] = baseMaterial();
m_materials[i].Lighting = true;
m_materials[i].MaterialType = video::EMT_SOLID;
}
+
m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
- setStarCount(1000, true);
+
+ if (g_settings->getBool("enable_dynamic_shadows")) {
+ float val = g_settings->getFloat("shadow_sky_body_orbit_tilt");
+ m_sky_body_orbit_tilt = rangelim(val, 0.0f, 60.0f);
+ }
+
+ setStarCount(1000);
}
void Sky::OnRegisterSceneNode()
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.415;
- 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="<<m_time_of_day<<" -> "
- <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
+ float wicked_time_of_day = getWickedTimeOfDay(m_time_of_day);
video::SColor suncolor = suncolor_f.toSColor();
video::SColor suncolor2 = suncolor2_f.toSColor();
const f32 t = 1.0f;
const f32 o = 0.0f;
- static const u16 indices[4] = {0, 1, 2, 3};
+ static const u16 indices[6] = {0, 1, 2, 0, 2, 3};
video::S3DVertex vertices[4];
driver->setMaterial(m_materials[1]);
vertex.Pos.rotateXZBy(180);
}
}
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
}
// Switch from -Z (south) to +Z (north)
vertex.Pos.rotateXZBy(-180);
}
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
}
// Switch from -Z (south) to -X (west)
vertex.Pos.rotateXZBy(-90);
}
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
// Draw sun
// Switch from -Z (south) to +Z (north)
vertex.Pos.rotateXZBy(-180);
}
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
// Draw bottom far cloudy fog thing in front of sun, moon and stars
vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
}
}
bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
- /*
- Development colours
-
- video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
-
- 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 bgcolor_bright_normal_f = m_sky_params.sky_color.day_horizon;
video::SColorf bgcolor_bright_indoor_f = m_sky_params.sky_color.indoors;
video::SColorf bgcolor_bright_dawn_f = m_sky_params.sky_color.dawn_horizon;
* wicked_time_of_day: current time of day, to know where should be the sun in the sky
*/
{
- static const u16 indices[4] = {0, 1, 2, 3};
+ static const u16 indices[] = {0, 1, 2, 0, 2, 3};
std::array<video::S3DVertex, 4> vertices;
if (!m_sun_texture) {
driver->setMaterial(m_materials[1]);
for (int i = 0; i < 4; i++) {
draw_sky_body(vertices, -sunsizes[i], sunsizes[i], colors[i]);
place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
} else {
driver->setMaterial(m_materials[3]);
c = video::SColor(255, 255, 255, 255);
draw_sky_body(vertices, -d, d, c);
place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
}
* the sky
*/
{
- static const u16 indices[4] = {0, 1, 2, 3};
+ static const u16 indices[] = {0, 1, 2, 0, 2, 3};
std::array<video::S3DVertex, 4> vertices;
if (!m_moon_texture) {
driver->setMaterial(m_materials[1]);
for (int i = 0; i < 4; i++) {
draw_sky_body(vertices, moonsizes_1[i], moonsizes_2[i], colors[i]);
place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
} else {
driver->setMaterial(m_materials[4]);
c = video::SColor(255, 255, 255, 255);
draw_sky_body(vertices, -d, d, c);
place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
}
void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
{
- driver->setMaterial(m_materials[1]);
// Tune values so that stars first appear just after the sun
// disappears over the horizon, and disappear just before the sun
// appears over the horizon.
// to time 4000.
float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
- float starbrightness = clamp((0.25f - fabsf(tod)) * 20.0f, 0.0f, 1.0f);
-
- float f = starbrightness;
- float d = (0.006 / 2) * m_star_params.scale;
-
- video::SColor starcolor = m_star_params.starcolor;
- starcolor.setAlpha(f * m_star_params.starcolor.getAlpha());
-
- // Stars are only drawn when not fully transparent
- if (m_star_params.starcolor.getAlpha() < 1)
+ float starbrightness = (0.25f - fabsf(tod)) * 20.0f;
+ m_star_color = m_star_params.starcolor;
+ m_star_color.a *= clamp(starbrightness, 0.0f, 1.0f);
+ if (m_star_color.a <= 0.0f) // Stars are only drawn when not fully transparent
return;
-#if ENABLE_GLES
- u16 *indices = new u16[m_star_params.count * 3];
- video::S3DVertex *vertices =
- new video::S3DVertex[m_star_params.count * 3];
- for (u32 i = 0; i < m_star_params.count; i++) {
- indices[i * 3 + 0] = i * 3 + 0;
- indices[i * 3 + 1] = i * 3 + 1;
- indices[i * 3 + 2] = i * 3 + 2;
- v3f r = m_stars[i];
- core::CMatrix4<f32> a;
- a.buildRotateFromTo(v3f(0, 1, 0), r);
- v3f p = v3f(-d, 1, -d);
- v3f p1 = v3f(d, 1, 0);
- v3f p2 = v3f(-d, 1, d);
- a.rotateVect(p);
- a.rotateVect(p1);
- a.rotateVect(p2);
- p.rotateXYBy(wicked_time_of_day * 360 - 90);
- p1.rotateXYBy(wicked_time_of_day * 360 - 90);
- p2.rotateXYBy(wicked_time_of_day * 360 - 90);
- vertices[i * 3 + 0].Pos = p;
- vertices[i * 3 + 0].Color = starcolor;
- vertices[i * 3 + 1].Pos = p1;
- vertices[i * 3 + 1].Color = starcolor;
- vertices[i * 3 + 2].Pos = p2;
- vertices[i * 3 + 2].Color = starcolor;
- }
- driver->drawIndexedTriangleList(vertices, m_star_params.count * 3,
- indices, m_star_params.count);
- delete[] indices;
- delete[] vertices;
-#else
- u16 *indices = new u16[m_star_params.count * 4];
- video::S3DVertex *vertices =
- new video::S3DVertex[m_star_params.count * 4];
- for (u32 i = 0; i < m_star_params.count; i++) {
- indices[i * 4 + 0] = i * 4 + 0;
- indices[i * 4 + 1] = i * 4 + 1;
- indices[i * 4 + 2] = i * 4 + 2;
- indices[i * 4 + 3] = i * 4 + 3;
- v3f r = m_stars[i];
- core::CMatrix4<f32> a;
- a.buildRotateFromTo(v3f(0, 1, 0), r);
- v3f p = v3f(-d, 1, -d);
- v3f p1 = v3f(d, 1, -d);
- v3f p2 = v3f(d, 1, d);
- v3f p3 = v3f(-d, 1, d);
- a.rotateVect(p);
- a.rotateVect(p1);
- a.rotateVect(p2);
- a.rotateVect(p3);
- p.rotateXYBy(wicked_time_of_day * 360 - 90);
- p1.rotateXYBy(wicked_time_of_day * 360 - 90);
- p2.rotateXYBy(wicked_time_of_day * 360 - 90);
- p3.rotateXYBy(wicked_time_of_day * 360 - 90);
- vertices[i * 4 + 0].Pos = p;
- vertices[i * 4 + 0].Color = starcolor;
- vertices[i * 4 + 1].Pos = p1;
- vertices[i * 4 + 1].Color = starcolor;
- vertices[i * 4 + 2].Pos = p2;
- vertices[i * 4 + 2].Color = starcolor;
- vertices[i * 4 + 3].Pos = p3;
- vertices[i * 4 + 3].Color = starcolor;
- }
- driver->drawVertexPrimitiveList(vertices, m_star_params.count * 4,
- indices, m_star_params.count, video::EVT_STANDARD,
- scene::EPT_QUADS, video::EIT_16BIT);
- delete[] indices;
- delete[] vertices;
-#endif
+ m_materials[0].DiffuseColor = m_materials[0].EmissiveColor = m_star_color.toSColor();
+ auto sky_rotation = core::matrix4().setRotationAxisRadians(2.0f * M_PI * (wicked_time_of_day - 0.25f), v3f(0.0f, 0.0f, 1.0f));
+ auto world_matrix = driver->getTransform(video::ETS_WORLD);
+ driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation);
+ driver->setMaterial(m_materials[0]);
+ driver->drawMeshBuffer(m_stars.get());
+ driver->setTransform(video::ETS_WORLD, world_matrix);
}
void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
* day_position: turn the body around the Z axis, to place it depending of the time of the day
*/
{
+ v3f centrum(0, 0, -1);
+ centrum.rotateXZBy(horizon_position);
+ centrum.rotateXYBy(day_position);
+ centrum.rotateYZBy(m_sky_body_orbit_tilt);
for (video::S3DVertex &vertex : vertices) {
// Body is directed to -Z (south) by default
vertex.Pos.rotateXZBy(horizon_position);
vertex.Pos.rotateXYBy(day_position);
+ vertex.Pos.Z += centrum.Z;
}
}
-void Sky::setSunTexture(std::string sun_texture,
- std::string sun_tonemap, ITextureSource *tsrc)
+void Sky::setSunTexture(const std::string &sun_texture,
+ const std::string &sun_tonemap, ITextureSource *tsrc)
{
// Ignore matching textures (with modifiers) entirely,
// but lets at least update the tonemap before hand.
m_sun_params.tonemap = sun_tonemap;
- m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
- tsrc->getTexture(m_sun_params.tonemap) : NULL;
+ m_sun_tonemap = tsrc->isKnownSourceImage(sun_tonemap) ?
+ tsrc->getTexture(sun_tonemap) : nullptr;
m_materials[3].Lighting = !!m_sun_tonemap;
- if (m_sun_params.texture == sun_texture)
+ if (m_sun_params.texture == sun_texture && !m_first_update)
return;
m_sun_params.texture = sun_texture;
- if (sun_texture != "") {
- // We want to ensure the texture exists first.
- m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
-
- if (m_sun_texture) {
- m_materials[3] = m_materials[0];
- m_materials[3].setTexture(0, m_sun_texture);
- m_materials[3].MaterialType = video::
- EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[3].setFlag(
- video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[3].setFlag(
- video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[3].setFlag(
- video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- }
- } else {
- m_sun_texture = nullptr;
+ m_sun_texture = nullptr;
+ if (sun_texture == "sun.png") {
+ // Dumb compatibility fix: sun.png transparently falls back to no texture
+ m_sun_texture = tsrc->isKnownSourceImage(sun_texture) ?
+ tsrc->getTexture(sun_texture) : nullptr;
+ } else if (!sun_texture.empty()) {
+ m_sun_texture = tsrc->getTextureForMesh(sun_texture);
+ }
+
+ if (m_sun_texture) {
+ m_materials[3] = baseMaterial();
+ m_materials[3].setTexture(0, m_sun_texture);
+ m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ disableTextureFiltering(m_materials[3]);
+ m_materials[3].Lighting = !!m_sun_tonemap;
}
}
-void Sky::setSunriseTexture(std::string sunglow_texture,
+void Sky::setSunriseTexture(const std::string &sunglow_texture,
ITextureSource* tsrc)
{
// Ignore matching textures (with modifiers) entirely.
);
}
-void Sky::setMoonTexture(std::string moon_texture,
- std::string moon_tonemap, ITextureSource *tsrc)
+void Sky::setMoonTexture(const std::string &moon_texture,
+ const std::string &moon_tonemap, ITextureSource *tsrc)
{
// Ignore matching textures (with modifiers) entirely,
// but lets at least update the tonemap before hand.
m_moon_params.tonemap = moon_tonemap;
- m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
- tsrc->getTexture(m_moon_params.tonemap) : NULL;
+ m_moon_tonemap = tsrc->isKnownSourceImage(moon_tonemap) ?
+ tsrc->getTexture(moon_tonemap) : nullptr;
m_materials[4].Lighting = !!m_moon_tonemap;
- if (m_moon_params.texture == moon_texture)
+ if (m_moon_params.texture == moon_texture && !m_first_update)
return;
m_moon_params.texture = moon_texture;
- if (moon_texture != "") {
- // We want to ensure the texture exists first.
- m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
-
- if (m_moon_texture) {
- m_materials[4] = m_materials[0];
- m_materials[4].setTexture(0, m_moon_texture);
- m_materials[4].MaterialType = video::
- EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[4].setFlag(
- video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[4].setFlag(
- video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[4].setFlag(
- video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- }
- } else {
- m_moon_texture = nullptr;
+ m_moon_texture = nullptr;
+ if (moon_texture == "moon.png") {
+ // Dumb compatibility fix: moon.png transparently falls back to no texture
+ m_moon_texture = tsrc->isKnownSourceImage(moon_texture) ?
+ tsrc->getTexture(moon_texture) : nullptr;
+ } else if (!moon_texture.empty()) {
+ m_moon_texture = tsrc->getTextureForMesh(moon_texture);
+ }
+
+ if (m_moon_texture) {
+ m_materials[4] = baseMaterial();
+ m_materials[4].setTexture(0, m_moon_texture);
+ m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ disableTextureFiltering(m_materials[4]);
+ m_materials[4].Lighting = !!m_moon_tonemap;
}
}
-void Sky::setStarCount(u16 star_count, bool force_update)
+void Sky::setStarCount(u16 star_count)
{
// Allow force updating star count at game init.
- if (m_star_params.count != star_count || force_update) {
+ if (m_star_params.count != star_count || m_first_update) {
m_star_params.count = star_count;
- m_stars.clear();
- // Rebuild the stars surrounding the camera
- for (u16 i = 0; i < star_count; i++) {
- v3f star = v3f(
- myrand_range(-10000, 10000),
- myrand_range(-10000, 10000),
- myrand_range(-10000, 10000)
- );
-
- star.normalize();
- m_stars.emplace_back(star);
- }
+ updateStars();
}
}
+void Sky::updateStars()
+{
+ m_stars.reset(new scene::SMeshBuffer());
+ // Stupid IrrLicht doesn’t allow non-indexed rendering, and indexed quad
+ // rendering is slow due to lack of hardware support. So as indices are
+ // 16-bit and there are 4 vertices per star... the limit is 2^16/4 = 0x4000.
+ // That should be well enough actually.
+ if (m_star_params.count > 0x4000) {
+ warningstream << "Requested " << m_star_params.count << " stars but " << 0x4000 << " is the max\n";
+ m_star_params.count = 0x4000;
+ }
+ m_stars->Vertices.reallocate(4 * m_star_params.count);
+ m_stars->Indices.reallocate(6 * m_star_params.count);
+
+ video::SColor fallback_color = m_star_params.starcolor; // used on GLES 2 “without shaders”
+ PcgRandom rgen(m_seed);
+ float d = (0.006 / 2) * m_star_params.scale;
+ for (u16 i = 0; i < m_star_params.count; i++) {
+ v3f r = v3f(
+ rgen.range(-10000, 10000),
+ rgen.range(-10000, 10000),
+ rgen.range(-10000, 10000)
+ );
+ core::CMatrix4<f32> a;
+ a.buildRotateFromTo(v3f(0, 1, 0), r);
+ v3f p = v3f(-d, 1, -d);
+ v3f p1 = v3f(d, 1, -d);
+ v3f p2 = v3f(d, 1, d);
+ v3f p3 = v3f(-d, 1, d);
+ a.rotateVect(p);
+ a.rotateVect(p1);
+ a.rotateVect(p2);
+ a.rotateVect(p3);
+ m_stars->Vertices.push_back(video::S3DVertex(p, {}, fallback_color, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p1, {}, fallback_color, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p2, {}, fallback_color, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
+ }
+ for (u16 i = 0; i < m_star_params.count; i++) {
+ m_stars->Indices.push_back(i * 4 + 0);
+ m_stars->Indices.push_back(i * 4 + 1);
+ m_stars->Indices.push_back(i * 4 + 2);
+ m_stars->Indices.push_back(i * 4 + 2);
+ m_stars->Indices.push_back(i * 4 + 3);
+ m_stars->Indices.push_back(i * 4 + 0);
+ }
+ m_stars->setHardwareMappingHint(scene::EHM_STATIC);
+}
+
void Sky::setSkyColors(const SkyColor &sky_color)
{
m_sky_params.sky_color = sky_color;
}
void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
- std::string use_sun_tint)
+ const std::string &use_sun_tint)
{
// Change sun and moon tinting:
m_sky_params.fog_sun_tint = sun_tint;
m_default_tint = true;
}
-void Sky::addTextureToSkybox(std::string texture, int material_id,
+void Sky::addTextureToSkybox(const std::string &texture, int material_id,
ITextureSource *tsrc)
{
// Sanity check for more than six textures.
// Keep a list of texture names handy.
m_sky_params.textures.emplace_back(texture);
video::ITexture *result = tsrc->getTextureForMesh(texture);
- m_materials[material_id+5] = m_materials[0];
+ m_materials[material_id+5] = baseMaterial();
m_materials[material_id+5].setTexture(0, result);
m_materials[material_id+5].MaterialType = video::EMT_SOLID;
}
-// To be called once at game init to setup default values.
-void Sky::setSkyDefaults()
+float getWickedTimeOfDay(float time_of_day)
{
- SkyboxDefaults sky_defaults;
- m_sky_params.sky_color = sky_defaults.getSkyColorDefaults();
- m_sun_params = sky_defaults.getSunDefaults();
- m_moon_params = sky_defaults.getMoonDefaults();
- m_star_params = sky_defaults.getStarDefaults();
+ float nightlength = 0.415f;
+ float wn = nightlength / 2;
+ float wicked_time_of_day = 0;
+ if (time_of_day > wn && time_of_day < 1.0f - wn)
+ wicked_time_of_day = (time_of_day - wn) / (1.0f - wn * 2) * 0.5f + 0.25f;
+ else if (time_of_day < 0.5f)
+ wicked_time_of_day = time_of_day / wn * 0.25f;
+ else
+ wicked_time_of_day = 1.0f - ((1.0f - time_of_day) / wn * 0.25f);
+ return wicked_time_of_day;
}