2012-12-02 11:08:56 +00:00
|
|
|
#include "private.h"
|
2012-11-25 21:53:01 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../tools.h"
|
|
|
|
#include "../renderer.h"
|
|
|
|
#include "../system.h"
|
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
/******************** Fake ********************/
|
2013-03-14 17:29:12 +00:00
|
|
|
static AtmosphereResult _fakeApplyAerialPerspective(Renderer* renderer, Vector3 location, Color base)
|
2013-03-11 16:57:26 +00:00
|
|
|
{
|
2013-03-14 17:29:12 +00:00
|
|
|
AtmosphereResult result;
|
2013-03-11 16:57:26 +00:00
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(location);
|
2013-03-14 17:29:12 +00:00
|
|
|
result.base = result.final = base;
|
|
|
|
result.inscattering = result.attenuation = COLOR_BLACK;
|
|
|
|
return result;
|
2013-03-11 16:57:26 +00:00
|
|
|
}
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
static AtmosphereResult _fakeGetSkyColor(Renderer* renderer, Vector3 direction)
|
2013-03-11 16:57:26 +00:00
|
|
|
{
|
2013-03-14 17:29:12 +00:00
|
|
|
AtmosphereResult result;
|
2013-03-11 16:57:26 +00:00
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(direction);
|
2013-03-14 17:29:12 +00:00
|
|
|
result.base = result.final = COLOR_WHITE;
|
|
|
|
result.inscattering = result.attenuation = COLOR_BLACK;
|
|
|
|
return result;
|
2013-03-11 16:57:26 +00:00
|
|
|
}
|
2012-12-15 10:14:57 +00:00
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
static void _fakeGetLightingStatus(Renderer* renderer, LightStatus* status, Vector3 normal, int opaque)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
2013-03-11 16:57:26 +00:00
|
|
|
LightDefinition light;
|
|
|
|
|
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(normal);
|
|
|
|
UNUSED(opaque);
|
2012-12-24 15:15:40 +00:00
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
light.color.r = 0.8;
|
|
|
|
light.color.g = 0.8;
|
|
|
|
light.color.b = 0.8;
|
|
|
|
light.direction.x = -0.7;
|
|
|
|
light.direction.y = -0.7;
|
|
|
|
light.direction.z = 0.7;
|
|
|
|
light.altered = 0;
|
|
|
|
light.reflection = 0.0;
|
|
|
|
lightingPushLight(status, &light);
|
|
|
|
light.color.r = 0.3;
|
|
|
|
light.color.g = 0.31;
|
|
|
|
light.color.b = 0.34;
|
|
|
|
light.direction.x = 0.7;
|
|
|
|
light.direction.y = -0.7;
|
|
|
|
light.direction.z = -0.7;
|
|
|
|
light.altered = 0;
|
|
|
|
light.reflection = 0.0;
|
|
|
|
lightingPushLight(status, &light);
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
/******************** Real ********************/
|
2013-03-18 21:07:57 +00:00
|
|
|
static inline double _getDayFactor(double daytime)
|
|
|
|
{
|
|
|
|
daytime = 1.0 - fabs(0.5 - daytime) / 0.5;
|
|
|
|
return daytime < 0.45 ? 0.0 : sqrt((daytime - 0.45) / 0.55);
|
|
|
|
}
|
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
static inline void _applyWeatherEffects(AtmosphereDefinition* definition, AtmosphereResult* result)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
2013-03-14 17:29:12 +00:00
|
|
|
double distance = result->distance;
|
2013-03-18 21:07:57 +00:00
|
|
|
double max_distance = 100.0 - 90.0 * definition->humidity;
|
|
|
|
double distancefactor, dayfactor;
|
2013-03-11 16:57:26 +00:00
|
|
|
|
2013-03-18 21:07:57 +00:00
|
|
|
if (distance > max_distance)
|
2012-12-15 10:14:57 +00:00
|
|
|
{
|
2013-03-18 21:07:57 +00:00
|
|
|
distance = max_distance;
|
2012-12-15 10:14:57 +00:00
|
|
|
}
|
2013-03-18 21:07:57 +00:00
|
|
|
distancefactor = (distance > max_distance ? max_distance : distance) / max_distance;
|
|
|
|
/* TODO Get day lighting from model */
|
|
|
|
dayfactor = _getDayFactor(definition->_daytime);
|
2012-12-12 13:21:46 +00:00
|
|
|
|
2013-03-18 21:07:57 +00:00
|
|
|
/* Fog masking */
|
|
|
|
if (definition->humidity > 0.3)
|
|
|
|
{
|
|
|
|
result->mask.r = result->mask.g = result->mask.b = (10.0 - 8.0 * definition->humidity) * dayfactor;
|
|
|
|
result->mask.a = distancefactor * (definition->humidity - 0.3) / 0.7;
|
|
|
|
}
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-03-18 21:07:57 +00:00
|
|
|
/* Scattering tweaking */
|
|
|
|
if (definition->humidity < 0.15)
|
|
|
|
{
|
|
|
|
/* Limit scattering on ultra clear day */
|
|
|
|
double force = (0.15 - definition->humidity) / 0.15;
|
|
|
|
colorLimitPower(&result->inscattering, 100.0 - 90.0 * pow(force, 0.1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Scattering boost */
|
|
|
|
double force = 1.2 * (definition->humidity < 0.5 ? sqrt((definition->humidity - 0.15) / 0.35) : 1.0 - (definition->humidity - 0.5) / 0.5);
|
|
|
|
result->inscattering.r *= 1.0 + force * distancefactor * (definition->humidity - 0.15) / 0.85;
|
|
|
|
result->inscattering.g *= 1.0 + force * distancefactor * (definition->humidity - 0.15) / 0.85;
|
|
|
|
result->inscattering.b *= 1.0 + force * distancefactor * (definition->humidity - 0.15) / 0.85;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attenuation */
|
|
|
|
result->attenuation.r *= 1.0 - 0.4 * distancefactor * definition->humidity;
|
|
|
|
result->attenuation.g *= 1.0 - 0.4 * distancefactor * definition->humidity;
|
|
|
|
result->attenuation.b *= 1.0 - 0.4 * distancefactor * definition->humidity;
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
atmosphereUpdateResult(result);
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
static AtmosphereResult _realApplyAerialPerspective(Renderer* renderer, Vector3 location, Color base)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
2013-03-11 16:57:26 +00:00
|
|
|
AtmosphereDefinition* definition = renderer->atmosphere->definition;
|
2013-03-14 17:29:12 +00:00
|
|
|
AtmosphereResult result;
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
/* Get base perspective */
|
|
|
|
switch (definition->model)
|
|
|
|
{
|
|
|
|
case ATMOSPHERE_MODEL_BRUNETON:
|
2013-03-14 17:29:12 +00:00
|
|
|
result = brunetonApplyAerialPerspective(renderer, location, base);
|
2013-03-11 16:57:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
/* Apply weather effects */
|
2013-03-18 21:07:57 +00:00
|
|
|
_applyWeatherEffects(definition, &result);
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
return result;
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
static AtmosphereResult _realGetSkyColor(Renderer* renderer, Vector3 direction)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
|
|
|
AtmosphereDefinition* definition;
|
2013-01-31 15:10:11 +00:00
|
|
|
Vector3 sun_direction, sun_position, camera_location;
|
2013-03-14 17:29:12 +00:00
|
|
|
Color base;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
definition = renderer->atmosphere->definition;
|
2013-01-31 15:10:11 +00:00
|
|
|
camera_location = renderer->getCameraLocation(renderer, VECTOR_ZERO);
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-12-23 22:05:12 +00:00
|
|
|
sun_direction = renderer->atmosphere->getSunDirection(renderer);
|
2012-11-25 21:53:01 +00:00
|
|
|
direction = v3Normalize(direction);
|
2013-01-30 17:00:26 +00:00
|
|
|
sun_position = v3Scale(sun_direction, SUN_DISTANCE_SCALED);
|
2012-11-25 21:53:01 +00:00
|
|
|
|
2013-01-20 21:17:03 +00:00
|
|
|
/* Get sun shape */
|
2013-03-14 17:29:12 +00:00
|
|
|
base = COLOR_BLACK;
|
2013-03-18 21:07:57 +00:00
|
|
|
/*if (v3Dot(sun_direction, direction) >= 0)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
2013-03-18 21:07:57 +00:00
|
|
|
double sun_radius = definition->sun_radius * SUN_RADIUS_SCALED * 5.0; // FIXME Why should we multiply by 5 ?
|
2013-02-03 21:18:32 +00:00
|
|
|
Vector3 hit1, hit2;
|
|
|
|
int hits = euclidRayIntersectSphere(camera_location, direction, sun_position, sun_radius, &hit1, &hit2);
|
|
|
|
if (hits > 1)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
2013-03-18 21:07:57 +00:00
|
|
|
double dist = v3Norm(v3Sub(hit2, hit1)) / sun_radius; // distance between intersection points (relative to radius)
|
2013-02-03 21:18:32 +00:00
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
Color sun_color = definition->sun_color;
|
2013-02-03 21:18:32 +00:00
|
|
|
sun_color.r *= 100.0;
|
|
|
|
sun_color.g *= 100.0;
|
|
|
|
sun_color.b *= 100.0;
|
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
if (dist <= 0.05)
|
2013-02-03 21:18:32 +00:00
|
|
|
{
|
2013-03-14 17:29:12 +00:00
|
|
|
sun_color.r *= 1.0 - dist / 0.05;
|
|
|
|
sun_color.g *= 1.0 - dist / 0.05;
|
|
|
|
sun_color.b *= 1.0 - dist / 0.05;
|
2013-02-03 21:18:32 +00:00
|
|
|
}
|
2013-03-14 17:29:12 +00:00
|
|
|
base = sun_color;
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
2013-03-18 21:07:57 +00:00
|
|
|
}*/
|
2013-03-11 16:57:26 +00:00
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
/* TODO Get stars */
|
2013-03-11 16:57:26 +00:00
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
/* Get scattering */
|
|
|
|
AtmosphereResult result;
|
|
|
|
Vector3 location = v3Add(camera_location, v3Scale(direction, 6421.0));
|
|
|
|
switch (definition->model)
|
|
|
|
{
|
|
|
|
case ATMOSPHERE_MODEL_BRUNETON:
|
|
|
|
result = brunetonGetSkyColor(renderer, camera_location, direction, sun_position, base);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = _fakeApplyAerialPerspective(renderer, location, result.base);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply weather effects */
|
2013-03-18 21:07:57 +00:00
|
|
|
_applyWeatherEffects(definition, &result);
|
2013-03-14 17:29:12 +00:00
|
|
|
|
|
|
|
return result;
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
static Vector3 _realGetSunDirection(Renderer* renderer)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
|
|
|
Vector3 result;
|
2012-12-24 15:15:40 +00:00
|
|
|
double sun_angle = (renderer->atmosphere->definition->_daytime + 0.75) * M_PI * 2.0;
|
2012-11-25 21:53:01 +00:00
|
|
|
result.x = cos(sun_angle);
|
|
|
|
result.y = sin(sun_angle);
|
|
|
|
result.z = 0.0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-18 21:07:57 +00:00
|
|
|
void atmosphereInitResult(AtmosphereResult* result)
|
|
|
|
{
|
|
|
|
result->base = COLOR_BLACK;
|
|
|
|
result->inscattering = COLOR_BLACK;
|
|
|
|
result->attenuation = COLOR_WHITE;
|
|
|
|
result->mask = COLOR_TRANSPARENT;
|
|
|
|
result->distance = 0.0;
|
|
|
|
result->final = COLOR_BLACK;
|
|
|
|
}
|
|
|
|
|
2013-03-14 17:29:12 +00:00
|
|
|
void atmosphereUpdateResult(AtmosphereResult* result)
|
|
|
|
{
|
|
|
|
result->final.r = result->base.r * result->attenuation.r + result->inscattering.r;
|
|
|
|
result->final.g = result->base.g * result->attenuation.g + result->inscattering.g;
|
|
|
|
result->final.b = result->base.b * result->attenuation.b + result->inscattering.b;
|
|
|
|
result->final.a = 1.0;
|
2013-03-18 21:07:57 +00:00
|
|
|
|
|
|
|
colorMask(&result->final, &result->mask);
|
2013-03-14 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
/******************** Renderer ********************/
|
|
|
|
static AtmosphereRenderer* _createRenderer()
|
|
|
|
{
|
|
|
|
AtmosphereRenderer* result;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
result = malloc(sizeof(AtmosphereRenderer));
|
|
|
|
result->definition = AtmosphereDefinitionClass.create();
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2013-01-19 22:42:50 +00:00
|
|
|
result->getLightingStatus = _fakeGetLightingStatus;
|
2013-03-11 16:57:26 +00:00
|
|
|
result->getSunDirection = _realGetSunDirection;
|
2012-11-25 21:53:01 +00:00
|
|
|
result->applyAerialPerspective = _fakeApplyAerialPerspective;
|
|
|
|
result->getSkyColor = _fakeGetSkyColor;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _deleteRenderer(AtmosphereRenderer* renderer)
|
|
|
|
{
|
|
|
|
AtmosphereDefinitionClass.destroy(renderer->definition);
|
|
|
|
free(renderer);
|
|
|
|
}
|
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
static void _bindRenderer(Renderer* renderer, AtmosphereDefinition* definition)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
2013-01-20 15:07:45 +00:00
|
|
|
AtmosphereDefinitionClass.copy(definition, renderer->atmosphere->definition);
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2013-03-11 16:57:26 +00:00
|
|
|
renderer->atmosphere->getSkyColor = _realGetSkyColor;
|
|
|
|
renderer->atmosphere->applyAerialPerspective = _realApplyAerialPerspective;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
|
|
|
switch (definition->model)
|
|
|
|
{
|
2012-12-24 11:24:27 +00:00
|
|
|
case ATMOSPHERE_MODEL_BRUNETON:
|
2013-01-20 21:17:03 +00:00
|
|
|
renderer->atmosphere->getLightingStatus = brunetonGetLightingStatus;
|
2012-12-24 11:24:27 +00:00
|
|
|
break;
|
2012-12-02 11:08:56 +00:00
|
|
|
default:
|
2013-03-14 17:29:12 +00:00
|
|
|
renderer->atmosphere->getLightingStatus = _fakeGetLightingStatus;
|
2012-12-02 11:08:56 +00:00
|
|
|
}
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StandardRenderer AtmosphereRendererClass = {
|
|
|
|
(FuncObjectCreate)_createRenderer,
|
|
|
|
(FuncObjectDelete)_deleteRenderer,
|
|
|
|
(FuncObjectBind)_bindRenderer
|
|
|
|
};
|