Sky.cpp: Improve code style. Define sky colours as SColor

This commit is contained in:
paramat 2016-06-09 03:15:41 +01:00
parent 559dd99469
commit a39d536282
1 changed files with 267 additions and 258 deletions

View File

@ -11,7 +11,7 @@
#include "settings.h"
#include "camera.h" // CameraModes
//! constructor
Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
ITextureSource *tsrc):
scene::ISceneNode(parent, mgr, id),
@ -28,7 +28,7 @@ Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
m_box.MaxEdge.set(0, 0, 0);
m_box.MinEdge.set(0, 0, 0);
// create material
// Create material
video::SMaterial mat;
mat.Lighting = false;
@ -66,6 +66,7 @@ Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
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);
@ -86,6 +87,7 @@ Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
}
void Sky::OnRegisterSceneNode()
{
if (IsVisible)
@ -94,7 +96,7 @@ void Sky::OnRegisterSceneNode()
scene::ISceneNode::OnRegisterSceneNode();
}
//! renders the node.
void Sky::render()
{
if (!m_visible)
@ -108,7 +110,7 @@ void Sky::render()
ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
// draw perspective skybox
// Draw perspective skybox
core::matrix4 translate(AbsoluteTransformation);
translate.setTranslation(camera->getAbsolutePosition());
@ -120,16 +122,15 @@ void Sky::render()
driver->setTransform(video::ETS_WORLD, translate * scale);
if(m_sunlight_seen)
{
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.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.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;
@ -159,14 +160,17 @@ void Sky::render()
if (m_sun_tonemap) {
u8 * texels = (u8 *)m_sun_tonemap->lock();
video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
video::SColor texel_color (255,texel->getRed(),texel->getGreen(), texel->getBlue());
video::SColor texel_color (255, texel->getRed(),
texel->getGreen(), texel->getBlue());
m_sun_tonemap->unlock();
m_materials[3].EmissiveColor = texel_color;
}
if (m_moon_tonemap) {
u8 * texels = (u8 *)m_moon_tonemap->lock();
video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
video::SColor texel_color (255,texel->getRed(),texel->getGreen(), texel->getBlue());
video::SColor texel_color (255, texel->getRed(),
texel->getGreen(), texel->getBlue());
m_moon_tonemap->unlock();
m_materials[4].EmissiveColor = texel_color;
}
@ -178,13 +182,10 @@ void Sky::render()
driver->setMaterial(m_materials[1]);
//video::SColor cloudyfogcolor(255,255,255,255);
video::SColor cloudyfogcolor = m_bgcolor;
//video::SColor cloudyfogcolor = m_bgcolor.getInterpolated(m_skycolor, 0.5);
// Draw far cloudy fog thing
for(u32 j=0; j<4; j++)
{
// Draw far cloudy fog thing blended with skycolor
for (u32 j = 0; j < 4; j++) {
video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
vertices[0] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, t);
vertices[1] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, t);
@ -201,13 +202,14 @@ void Sky::render()
// Switch from -Z (south) to -X (west)
vertices[i].Pos.rotateXZBy(-90);
else
// Switch from -Z (south) to -Z (north)
// Switch from -Z (south) to +Z (north)
vertices[i].Pos.rotateXZBy(-180);
}
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
}
for(u32 j=0; j<4; j++)
{
// Draw far cloudy fog thing
for (u32 j = 0; j < 4; j++) {
video::SColor c = cloudyfogcolor;
vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
@ -224,7 +226,7 @@ void Sky::render()
// Switch from -Z (south) to -X (west)
vertices[i].Pos.rotateXZBy(-90);
else
// Switch from -Z (south) to -Z (north)
// Switch from -Z (south) to +Z (north)
vertices[i].Pos.rotateXZBy(-180);
}
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
@ -232,9 +234,10 @@ void Sky::render()
driver->setMaterial(m_materials[2]);
// Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
{
float mid1 = 0.25;
float mid = (wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1));
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_="<<a_<<" a="<<a<<std::endl;
@ -332,8 +335,7 @@ void Sky::render()
}
// Draw moon
if(wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7)
{
if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
if (!m_moon_texture) {
driver->setMaterial(m_materials[1]);
float d = moonsize * 1.9;
@ -408,7 +410,7 @@ void Sky::render()
}
}
// Stars
// Draw stars
driver->setMaterial(m_materials[1]);
do {
float starbrightness = MYMAX(0, MYMIN(1,
@ -455,9 +457,8 @@ void Sky::render()
scene::EPT_QUADS, video::EIT_16BIT);
} while(0);
for(u32 j=0; j<2; j++)
{
//video::SColor c = m_skycolor;
// Draw far cloudy fog thing below east and west horizons
for (u32 j = 0; j < 2; j++) {
video::SColor c = cloudyfogcolor;
vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
@ -477,6 +478,7 @@ void Sky::render()
}
}
void Sky::update(float time_of_day, float time_brightness,
float direct_brightness, bool sunlight_seen,
CameraMode cam_mode, float yaw, float pitch)
@ -501,31 +503,31 @@ void Sky::update(float time_of_day, float time_brightness,
bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
//video::SColorf bgcolor_bright_normal_f(170./255,200./255,230./255, 1.0);
video::SColorf bgcolor_bright_normal_f(155./255,193./255,240./255, 1.0);
video::SColorf bgcolor_bright_indoor_f(100./255,100./255,100./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 bgcolor_bright_dawn_f
(155./255*1.2,193./255,240./255, 1.0);
video::SColorf bgcolor_bright_night_f
(64./255, 144./255, 255./255, 1.0);
/*
Development colours
video::SColorf skycolor_bright_normal_f =
video::SColor(255, 140, 186, 250);
video::SColorf skycolor_bright_dawn_f =
video::SColor(255, 180, 186, 250);
video::SColorf skycolor_bright_night_f =
video::SColor(255, 0, 107, 255);
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_normal_f =
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.875, 0.75);
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 = video::SColor(255, 155, 193, 240);
video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 240, 240, 255);
video::SColorf cloudcolor_bright_dawn_f = video::SColor(255, 255, 223, 191);
float cloud_color_change_fraction = 0.95;
if (sunlight_seen) {
@ -535,8 +537,7 @@ void Sky::update(float time_of_day, float time_brightness,
m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
cloud_color_change_fraction = 0.0;
}
}
else{
} else {
if (direct_brightness < m_brightness)
m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
else
@ -546,7 +547,7 @@ void Sky::update(float time_of_day, float time_brightness,
m_clouds_visible = true;
float color_change_fraction = 0.98;
if (sunlight_seen) {
if(is_dawn){
if (is_dawn) { // Dawn
m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
bgcolor_bright_dawn_f, color_change_fraction);
m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
@ -554,12 +555,12 @@ void Sky::update(float time_of_day, float time_brightness,
m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
cloudcolor_bright_dawn_f, color_change_fraction);
} else {
if (time_brightness < 0.07) { // Night sky
if (time_brightness < 0.07) { // Night
m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
bgcolor_bright_night_f, color_change_fraction);
m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
skycolor_bright_night_f, color_change_fraction);
} else { // Daytime sky
} else { // Day
m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
bgcolor_bright_normal_f, color_change_fraction);
m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
@ -584,21 +585,22 @@ void Sky::update(float time_of_day, float time_brightness,
255,
bgcolor_bright.getRed() * m_brightness,
bgcolor_bright.getGreen() * m_brightness,
bgcolor_bright.getBlue() * m_brightness);
bgcolor_bright.getBlue() * m_brightness
);
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);
skycolor_bright.getBlue() * m_brightness
);
// 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_directional_colored_fog) {
if (m_horizon_blend() != 0)
{
// calculate hemisphere value from yaw, (inverted in third person front view)
if (m_horizon_blend() != 0) {
// Calculate hemisphere value from yaw, (inverted in third person front view)
s8 dir_factor = 1;
if (cam_mode > CAMERA_MODE_THIRD)
dir_factor = -1;
@ -606,41 +608,48 @@ void Sky::update(float time_of_day, float time_brightness,
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(pitch)) / 90 * 1.5, 1));
// invert direction to match where the sun and moon are rising
// 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::fabs(pitch)) / 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
// 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);
if (m_sun_tonemap)
{
pointcolor_sun_f.r = pointcolor_light * (float)m_materials[3].EmissiveColor.getRed() / 255;
pointcolor_sun_f.b = pointcolor_light * (float)m_materials[3].EmissiveColor.getBlue() / 255;
pointcolor_sun_f.g = pointcolor_light * (float)m_materials[3].EmissiveColor.getGreen() / 255;
}
else
{
if (m_sun_tonemap) {
pointcolor_sun_f.r = pointcolor_light *
(float)m_materials[3].EmissiveColor.getRed() / 255;
pointcolor_sun_f.b = pointcolor_light *
(float)m_materials[3].EmissiveColor.getBlue() / 255;
pointcolor_sun_f.g = pointcolor_light *
(float)m_materials[3].EmissiveColor.getGreen() / 255;
} else {
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);
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);
if (m_moon_tonemap)
{
pointcolor_moon_f.r = pointcolor_light * (float)m_materials[4].EmissiveColor.getRed() / 255;
pointcolor_moon_f.b = pointcolor_light * (float)m_materials[4].EmissiveColor.getBlue() / 255;
pointcolor_moon_f.g = pointcolor_light * (float)m_materials[4].EmissiveColor.getGreen() / 255;
video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
if (m_moon_tonemap) {
pointcolor_moon_f.r = pointcolor_light *
(float)m_materials[4].EmissiveColor.getRed() / 255;
pointcolor_moon_f.b = pointcolor_light *
(float)m_materials[4].EmissiveColor.getBlue() / 255;
pointcolor_moon_f.g = pointcolor_light *
(float)m_materials[4].EmissiveColor.getGreen() / 255;
}
video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
// calculate the blend color
// Calculate the blend color
pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
}
m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
@ -653,24 +662,24 @@ void Sky::update(float time_of_day, float time_brightness,
cloud_direct_brightness = time_brightness;
if (time_brightness >= 0.2 && time_brightness < 0.7)
cloud_direct_brightness *= 1.3;
}
else {
cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 + m_time_brightness, 1);
} else {
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.r * m_cloud_brightness,
m_cloudcolor_bright_f.g * m_cloud_brightness,
m_cloudcolor_bright_f.b * m_cloud_brightness,
1.0);
1.0
);
if (m_directional_colored_fog) {
m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f, video::SColorf(pointcolor), m_horizon_blend() * 0.25);
m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
video::SColorf(pointcolor), m_horizon_blend() * 0.25);
}
}