+#if ENABLE_GLES+ u16 *indices = new u16[m_star_count * 3];
+ video::S3DVertex *vertices =
+ new video::S3DVertex[m_star_count * 3];
+ for (u32 i = 0; i < m_star_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.data(), m_star_count * 3,
+ indices.data(), m_star_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
+}
void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
{
/*
- * Create an array of vertices with the dimensions specified.
- * pos_1, pos_2: position of the body's vertices
- * c: color of the body
- */
+ * Create an array of vertices with the dimensions specified.
+ * pos_1, pos_2: position of the body's vertices
+ * c: color of the body
+ */
const f32 t = 1.0f;
const f32 o = 0.0f;
@@ -738,11 +815,11 @@ void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1,
void Sky::place_sky_body(
std::array<video::S3DVertex, 4> &vertices, float horizon_position, float day_position)
/*
- * Place body in the sky.
- * vertices: The body as a rectangle of 4 vertices
- * horizon_position: turn the body around the Y axis
- * day_position: turn the body around the Z axis, to place it depending of the time of the day
- */
+ * Place body in the sky.
+ * vertices: The body as a rectangle of 4 vertices
+ * horizon_position: turn the body around the Y axis
+ * day_position: turn the body around the Z axis, to place it depending of the time of the day
+ */
{
for (video::S3DVertex &vertex : vertices) {
// Body is directed to -Z (south) by default
@@ -750,3 +827,168 @@ void Sky::place_sky_body(
vertex.Pos.rotateXYBy(day_position);
}
}
+
+void Sky::setSunTexture(std::string sun_texture,
+ 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_materials[3].Lighting = !!m_sun_tonemap;
+
+ if (m_sun_params.texture == sun_texture)
+ 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;
+ }
+}
+
+void Sky::setSunriseTexture(std::string sunglow_texture,
+ ITextureSource* tsrc)
+{
+ // Ignore matching textures (with modifiers) entirely.
+ if (m_sun_params.sunrise == sunglow_texture)
+ return;
+ m_sun_params.sunrise = sunglow_texture;
+ m_materials[2].setTexture(0, tsrc->getTextureForMesh(
+ sunglow_texture.empty() ? "sunrisebg.png" : sunglow_texture)
+ );
+}
+
+void Sky::setMoonTexture(std::string moon_texture,
+ 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_materials[4].Lighting = !!m_moon_tonemap;
+
+ if (m_moon_params.texture == moon_texture)
+ 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;
+ }
+}
+
+void Sky::setStarCount(u16 star_count, bool force_update)
+{
+ // Force updating star count at game init.
+ if (force_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);
+ }
+ // Ignore changing star count if the new value is identical
+ } else if (m_star_params.count == star_count)
+ return;
+ else {
+ 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);
+ }
+ }
+}
+
+void Sky::setSkyColors(const SkyboxParams sky)
+{
+ m_sky_params.sky_color = sky.sky_color;
+}
+
+void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
+ std::string use_sun_tint)
+{
+ // Change sun and moon tinting:
+ m_sky_params.sun_tint = sun_tint;
+ m_sky_params.moon_tint = moon_tint;
+ // Faster than comparing strings every rendering frame
+ if (use_sun_tint == "default")
+ m_default_tint = true;
+ else if (use_sun_tint == "custom")
+ m_default_tint = false;
+ else
+ m_default_tint = true;
+}
+
+void Sky::addTextureToSkybox(std::string texture, int material_id,
+ ITextureSource *tsrc)
+{
+ // Sanity check for more than six textures.
+ if (material_id + 5 >= SKY_MATERIAL_COUNT)
+ return;
+ // 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].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()
+{
+ 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();
+}
@@ -21,11 +21,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <array>
#include "camera.h"
#include "irrlichttypes_extrabloated.h"
+#include "skyparams.h"
#pragma once
-#define SKY_MATERIAL_COUNT 5
-#define SKY_STAR_COUNT 1000
+#define SKY_MATERIAL_COUNT 12
class ITextureSource;
@@ -45,8 +45,6 @@ public:
// 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,
@@ -64,6 +62,23 @@ public:
return m_visible ? m_skycolor : m_fallback_bg_color;
}
+ void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
+ void setSunTexture(std::string sun_texture,
+ std::string sun_tonemap, ITextureSource *tsrc);
+ void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
+ void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
+ void setSunriseTexture(std::string sunglow_texture, ITextureSource* tsrc);
+
+ void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
+ void setMoonTexture(std::string moon_texture,
+ std::string moon_tonemap, ITextureSource *tsrc);
+ void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
+
+ void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
+ void setStarCount(u16 star_count, bool force_update);
+ void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
+ void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; }
+
bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
@@ -79,12 +94,16 @@ public:
m_bgcolor = bgcolor;
m_skycolor = skycolor;
}
- void setBodiesVisible(bool visible) { m_bodies_visible = visible; }
-
+ void setSkyColors(const SkyboxParams sky);
+ void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
+ std::string use_sun_tint);
+ void setInClouds(bool clouds) { m_in_clouds = clouds; }
+ void clearSkyboxTextures() { m_sky_params.textures.clear(); }
+ void addTextureToSkybox(std::string texture, int material_id,
+ ITextureSource *tsrc);
private:
aabb3f m_box;
video::SMaterial m_materials[SKY_MATERIAL_COUNT];
-
// How much sun & moon transition should affect horizon color
float m_horizon_blend()
{
@@ -134,25 +153,46 @@ private:
bool m_clouds_visible; // Whether clouds are disabled due to player underground
bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
bool m_directional_colored_fog;
- bool m_bodies_visible = true; // sun, moon, stars
+ bool m_in_clouds = true; // Prevent duplicating bools to remember old values
+
video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
video::SColor m_bgcolor;
video::SColor m_skycolor;
video::SColorf m_cloudcolor_f;
- v3f m_stars[SKY_STAR_COUNT];
+
+ // pure white: becomes "diffuse light component" for clouds
+ video::SColorf m_cloudcolor_day_f = video::SColorf(1, 1, 1, 1);
+ // dawn-factoring version of pure white (note: R is above 1.0)
+ video::SColorf m_cloudcolor_dawn_f = video::SColorf(
+ 255.0f/240.0f,
+ 223.0f/240.0f,
+ 191.0f/255.0f
+ );
+
+ SkyboxParams m_sky_params;
+ SunParams m_sun_params;
+ MoonParams m_moon_params;
+ StarParams m_star_params;
+
+ bool m_default_tint = true;
+
+ std::vector<v3f> m_stars;
+
video::ITexture *m_sun_texture;
video::ITexture *m_moon_texture;
video::ITexture *m_sun_tonemap;
video::ITexture *m_moon_tonemap;
+
void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
- const video::SColor &suncolor2, float wicked_time_of_day);
+ const video::SColor &suncolor2, float wicked_time_of_day);
void draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
- const video::SColor &mooncolor2, float wicked_time_of_day);
+ const video::SColor &mooncolor2, float wicked_time_of_day);
void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
- float pos_1, float pos_2, const video::SColor &c);
- void place_sky_body(
- std::array<video::S3DVertex, 4> &vertices, float horizon_position,
- float day_position);
+ float pos_1, float pos_2, const video::SColor &c);
+ void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
+ void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
+ float horizon_position, float day_position);
+ void setSkyDefaults();
};
@@ -114,9 +114,9 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_MODCHANNEL_MSG", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_ModChannelMsg }, // 0x57
{ "TOCLIENT_MODCHANNEL_SIGNAL", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_ModChannelSignal }, // 0x58
{ "TOCLIENT_NODEMETA_CHANGED", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_NodemetaChanged }, // 0x59
- null_command_handler,
- null_command_handler,
- null_command_handler,
+ { "TOCLIENT_SET_SUN", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_HudSetSun }, // 0x5a
+ { "TOCLIENT_SET_MOON", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_HudSetMoon }, // 0x5b
+ { "TOCLIENT_SET_STARS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_HudSetStars }, // 0x5c
null_command_handler,
null_command_handler,