2013-11-12 20:34:35 +00:00
|
|
|
#include "AtmosphereRenderer.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include "SoftwareRenderer.h"
|
2013-11-15 23:27:40 +00:00
|
|
|
#include "AtmosphereDefinition.h"
|
2013-12-08 19:54:34 +00:00
|
|
|
#include "AtmosphereModelBruneton.h"
|
|
|
|
#include "AtmosphereResult.h"
|
2015-09-29 20:31:25 +00:00
|
|
|
#include "GodRaysSampler.h"
|
2013-12-08 19:54:34 +00:00
|
|
|
#include "LightComponent.h"
|
|
|
|
#include "LightStatus.h"
|
2013-11-12 20:34:35 +00:00
|
|
|
#include "Scenery.h"
|
2013-12-26 14:36:15 +00:00
|
|
|
#include "NightSky.h"
|
2015-08-18 18:31:11 +00:00
|
|
|
#include "FloatNode.h"
|
2013-11-12 20:34:35 +00:00
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
/* Factor to convert software units to kilometers */
|
|
|
|
#define WORLD_SCALING 0.05
|
|
|
|
#define SUN_DISTANCE 149597870.0
|
|
|
|
#define SUN_DISTANCE_SCALED (SUN_DISTANCE / WORLD_SCALING)
|
|
|
|
#define SUN_RADIUS 6.955e5
|
|
|
|
#define SUN_RADIUS_SCALED (SUN_RADIUS / WORLD_SCALING)
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
static inline double _getDayFactor(double daytime) {
|
2013-11-12 20:34:35 +00:00
|
|
|
daytime = 1.0 - fabs(0.5 - daytime) / 0.5;
|
|
|
|
return daytime < 0.45 ? 0.0 : sqrt((daytime - 0.45) / 0.55);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
static inline void _applyWeatherEffects(AtmosphereDefinition *definition, AtmosphereResult *result) {
|
|
|
|
if (definition->model == AtmosphereDefinition::ATMOSPHERE_MODEL_DISABLED) {
|
2013-12-15 13:28:46 +00:00
|
|
|
result->updateFinal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-25 21:33:47 +00:00
|
|
|
double humidity = definition->propHumidity()->getValue();
|
2013-11-12 20:34:35 +00:00
|
|
|
double distance = result->distance;
|
2015-08-25 21:33:47 +00:00
|
|
|
double max_distance = 100.0 - 90.0 * humidity;
|
2013-11-12 20:34:35 +00:00
|
|
|
double distancefactor, dayfactor;
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (distance > max_distance) {
|
2013-11-12 20:34:35 +00:00
|
|
|
distance = max_distance;
|
|
|
|
}
|
|
|
|
distancefactor = (distance > max_distance ? max_distance : distance) / max_distance;
|
|
|
|
/* TODO Get day lighting from model */
|
2015-08-18 18:31:11 +00:00
|
|
|
dayfactor = _getDayFactor(definition->propDayTime()->getValue());
|
2013-11-12 20:34:35 +00:00
|
|
|
|
|
|
|
/* Fog masking */
|
2015-11-09 21:30:46 +00:00
|
|
|
if (humidity > 0.3) {
|
2015-08-23 23:19:19 +00:00
|
|
|
result->mask.r = result->mask.g = result->mask.b = (10.0 - 8.0 * humidity) * dayfactor;
|
|
|
|
result->mask.a = distancefactor * (humidity - 0.3) / 0.7;
|
2013-11-12 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scattering tweaking */
|
2015-11-09 21:30:46 +00:00
|
|
|
if (humidity < 0.15) {
|
2013-11-12 20:34:35 +00:00
|
|
|
/* Limit scattering on ultra clear day */
|
2015-08-23 23:19:19 +00:00
|
|
|
double force = (0.15 - humidity) / 0.15;
|
2013-12-11 09:24:35 +00:00
|
|
|
result->inscattering.limitPower(100.0 - 90.0 * pow(force, 0.1));
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2013-11-12 20:34:35 +00:00
|
|
|
/* Scattering boost */
|
2015-08-23 23:19:19 +00:00
|
|
|
double force = 1.2 * (humidity < 0.5 ? sqrt((humidity - 0.15) / 0.35) : 1.0 - (humidity - 0.5) / 0.5);
|
|
|
|
result->inscattering.r *= 1.0 + force * distancefactor * (humidity - 0.15) / 0.85;
|
|
|
|
result->inscattering.g *= 1.0 + force * distancefactor * (humidity - 0.15) / 0.85;
|
|
|
|
result->inscattering.b *= 1.0 + force * distancefactor * (humidity - 0.15) / 0.85;
|
2013-11-12 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attenuation */
|
2015-08-23 23:19:19 +00:00
|
|
|
result->attenuation.r *= 1.0 - 0.4 * distancefactor * humidity;
|
|
|
|
result->attenuation.g *= 1.0 - 0.4 * distancefactor * humidity;
|
|
|
|
result->attenuation.b *= 1.0 - 0.4 * distancefactor * humidity;
|
2013-11-12 20:34:35 +00:00
|
|
|
|
2013-12-08 19:54:34 +00:00
|
|
|
result->updateFinal();
|
2013-11-12 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
BaseAtmosphereRenderer::BaseAtmosphereRenderer(SoftwareRenderer *renderer) : parent(renderer) {
|
2013-11-12 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
AtmosphereResult BaseAtmosphereRenderer::applyAerialPerspective(const Vector3 &, const Color &base) {
|
2013-11-12 20:34:35 +00:00
|
|
|
AtmosphereResult result;
|
|
|
|
result.base = result.final = base;
|
|
|
|
result.inscattering = result.attenuation = COLOR_BLACK;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
AtmosphereResult BaseAtmosphereRenderer::getSkyColor(const Vector3 &) {
|
2013-11-12 20:34:35 +00:00
|
|
|
AtmosphereResult result;
|
|
|
|
result.base = result.final = COLOR_WHITE;
|
|
|
|
result.inscattering = result.attenuation = COLOR_BLACK;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Vector3 BaseAtmosphereRenderer::getSunDirection(bool) const {
|
|
|
|
AtmosphereDefinition *atmosphere = getDefinition();
|
2015-09-24 22:12:31 +00:00
|
|
|
double sun_angle = (atmosphere->propDayTime()->getValue() + 0.75) * M_PI * 2.0;
|
|
|
|
return Vector3(cos(sun_angle), sin(sun_angle), 0.0);
|
2013-12-15 13:28:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
bool BaseAtmosphereRenderer::getLightsAt(std::vector<LightComponent> &, const Vector3 &) const {
|
2015-09-24 22:12:31 +00:00
|
|
|
return false;
|
2013-12-15 13:28:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
AtmosphereDefinition *BaseAtmosphereRenderer::getDefinition() const {
|
2013-12-09 10:59:57 +00:00
|
|
|
return parent->getScenery()->getAtmosphere();
|
2013-11-12 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
SoftwareBrunetonAtmosphereRenderer::SoftwareBrunetonAtmosphereRenderer(SoftwareRenderer *renderer)
|
|
|
|
: BaseAtmosphereRenderer(renderer) {
|
2013-12-09 10:59:57 +00:00
|
|
|
model = new AtmosphereModelBruneton(parent);
|
2013-12-08 19:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
SoftwareBrunetonAtmosphereRenderer::~SoftwareBrunetonAtmosphereRenderer() {
|
2013-12-08 19:54:34 +00:00
|
|
|
delete model;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
AtmosphereResult SoftwareBrunetonAtmosphereRenderer::applyAerialPerspective(const Vector3 &location,
|
|
|
|
const Color &base) {
|
|
|
|
AtmosphereDefinition *definition = getDefinition();
|
2013-11-12 20:34:35 +00:00
|
|
|
AtmosphereResult result;
|
|
|
|
|
2015-09-29 20:31:25 +00:00
|
|
|
// Get base perspective
|
2015-11-09 21:30:46 +00:00
|
|
|
switch (definition->model) {
|
2013-11-15 23:27:40 +00:00
|
|
|
case AtmosphereDefinition::ATMOSPHERE_MODEL_BRUNETON:
|
2013-12-08 19:54:34 +00:00
|
|
|
result = model->applyAerialPerspective(location, base);
|
2013-11-12 20:34:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:31:25 +00:00
|
|
|
// Apply god rays ponderation
|
|
|
|
result.inscattering = parent->getGodRaysSampler()->apply(COLOR_BLACK, result.inscattering, location);
|
|
|
|
|
|
|
|
// Apply weather effects
|
2013-11-12 20:34:35 +00:00
|
|
|
_applyWeatherEffects(definition, &result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
AtmosphereResult SoftwareBrunetonAtmosphereRenderer::getSkyColor(const Vector3 &direction) {
|
|
|
|
AtmosphereDefinition *definition;
|
2013-11-12 20:34:35 +00:00
|
|
|
Vector3 sun_direction, sun_position, camera_location;
|
|
|
|
Color base;
|
|
|
|
|
|
|
|
definition = getDefinition();
|
2013-12-09 10:59:57 +00:00
|
|
|
camera_location = parent->getCameraLocation(VECTOR_ZERO);
|
2013-11-12 20:34:35 +00:00
|
|
|
|
|
|
|
sun_direction = getSunDirection();
|
2015-11-08 22:32:52 +00:00
|
|
|
Vector3 direction_norm = direction.normalize();
|
2013-12-11 10:32:10 +00:00
|
|
|
sun_position = sun_direction.scale(SUN_DISTANCE_SCALED);
|
2013-11-12 20:34:35 +00:00
|
|
|
|
|
|
|
base = COLOR_BLACK;
|
2013-12-26 14:36:15 +00:00
|
|
|
|
2015-09-29 20:31:25 +00:00
|
|
|
// Get night sky
|
2015-11-08 22:32:52 +00:00
|
|
|
base = base.add(parent->getNightSky()->getColor(camera_location.y, direction_norm));
|
2013-12-26 14:36:15 +00:00
|
|
|
|
2015-09-29 20:31:25 +00:00
|
|
|
// Get sun shape
|
2013-11-12 20:34:35 +00:00
|
|
|
/*if (v3Dot(sun_direction, direction) >= 0)
|
|
|
|
{
|
|
|
|
double sun_radius = definition->sun_radius * SUN_RADIUS_SCALED * 5.0; // FIXME Why should we multiply by 5 ?
|
|
|
|
Vector3 hit1, hit2;
|
|
|
|
int hits = euclidRayIntersectSphere(camera_location, direction, sun_position, sun_radius, &hit1, &hit2);
|
|
|
|
if (hits > 1)
|
|
|
|
{
|
2015-11-09 21:30:46 +00:00
|
|
|
double dist = v3Norm(v3Sub(hit2, hit1)) / sun_radius; // distance between intersection points (relative to
|
|
|
|
radius)
|
2013-11-12 20:34:35 +00:00
|
|
|
|
|
|
|
Color sun_color = definition->sun_color;
|
|
|
|
sun_color.r *= 100.0;
|
|
|
|
sun_color.g *= 100.0;
|
|
|
|
sun_color.b *= 100.0;
|
|
|
|
|
|
|
|
if (dist <= 0.05)
|
|
|
|
{
|
|
|
|
sun_color.r *= 1.0 - dist / 0.05;
|
|
|
|
sun_color.g *= 1.0 - dist / 0.05;
|
|
|
|
sun_color.b *= 1.0 - dist / 0.05;
|
|
|
|
}
|
|
|
|
base = sun_color;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2015-09-29 20:31:25 +00:00
|
|
|
// Get scattering
|
2013-11-12 20:34:35 +00:00
|
|
|
AtmosphereResult result;
|
2015-11-08 22:32:52 +00:00
|
|
|
Vector3 location = camera_location.add(direction_norm.scale(6421.0));
|
2015-11-09 21:30:46 +00:00
|
|
|
switch (definition->model) {
|
2013-11-15 23:27:40 +00:00
|
|
|
case AtmosphereDefinition::ATMOSPHERE_MODEL_BRUNETON:
|
2015-11-08 22:32:52 +00:00
|
|
|
result = model->getSkyColor(camera_location, direction_norm, sun_position, base);
|
2013-11-12 20:34:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = BaseAtmosphereRenderer::applyAerialPerspective(location, result.base);
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:31:25 +00:00
|
|
|
// Apply god rays ponderation
|
|
|
|
result.inscattering = parent->getGodRaysSampler()->apply(COLOR_BLACK, result.inscattering, location);
|
|
|
|
|
|
|
|
// Apply weather effects
|
2013-11-12 20:34:35 +00:00
|
|
|
_applyWeatherEffects(definition, &result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-24 22:12:31 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
bool SoftwareBrunetonAtmosphereRenderer::getLightsAt(std::vector<LightComponent> &result,
|
|
|
|
const Vector3 &location) const {
|
2015-09-24 22:12:31 +00:00
|
|
|
bool changed = false;
|
|
|
|
changed |= model->getLightsAt(result, location);
|
|
|
|
changed |= parent->getNightSky()->getLightsAt(result, location);
|
|
|
|
return changed;
|
|
|
|
}
|