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"
|
|
|
|
|
|
|
|
#define MAX_SKYDOME_LIGHTS 100
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Mutex* lock;
|
|
|
|
int nblights;
|
|
|
|
LightDefinition lights[MAX_SKYDOME_LIGHTS];
|
|
|
|
} AtmosphereRendererCache;
|
|
|
|
|
2012-12-15 10:14:57 +00:00
|
|
|
static int _inited = 0;
|
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
/******************** Definition ********************/
|
|
|
|
static void _validateDefinition(AtmosphereDefinition* definition)
|
|
|
|
{
|
2012-12-24 15:15:40 +00:00
|
|
|
if (definition->hour < 0)
|
|
|
|
{
|
|
|
|
definition->hour = 0;
|
|
|
|
}
|
|
|
|
if (definition->hour > 23)
|
|
|
|
{
|
|
|
|
definition->hour = 23;
|
|
|
|
}
|
|
|
|
if (definition->minute < 0)
|
|
|
|
{
|
|
|
|
definition->minute = 0;
|
|
|
|
}
|
|
|
|
if (definition->minute > 59)
|
|
|
|
{
|
|
|
|
definition->minute = 59;
|
|
|
|
}
|
|
|
|
|
|
|
|
definition->_daytime = (double)definition->hour / 24.0 + (double)definition->minute / 1440.0;
|
2012-11-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static AtmosphereDefinition* _createDefinition()
|
|
|
|
{
|
|
|
|
AtmosphereDefinition* result;
|
|
|
|
|
2012-12-15 10:14:57 +00:00
|
|
|
/* TODO Find a better place for this */
|
|
|
|
if (!_inited)
|
|
|
|
{
|
|
|
|
_inited = 1;
|
|
|
|
brunetonInit();
|
|
|
|
}
|
2012-12-12 13:21:46 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
result = malloc(sizeof(AtmosphereDefinition));
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-12-24 13:59:17 +00:00
|
|
|
atmosphereAutoPreset(result, ATMOSPHERE_PRESET_CLEAR_DAY);
|
2012-11-25 21:53:01 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _deleteDefinition(AtmosphereDefinition* definition)
|
|
|
|
{
|
|
|
|
free(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _copyDefinition(AtmosphereDefinition* source, AtmosphereDefinition* destination)
|
|
|
|
{
|
|
|
|
destination->model = source->model;
|
2012-12-24 15:15:40 +00:00
|
|
|
destination->hour = source->hour;
|
|
|
|
destination->minute = source->minute;
|
2012-11-25 21:53:01 +00:00
|
|
|
destination->sun_color = source->sun_color;
|
|
|
|
destination->sun_radius = source->sun_radius;
|
|
|
|
destination->dome_lighting = source->dome_lighting;
|
|
|
|
destination->humidity = source->humidity;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
_validateDefinition(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _saveDefinition(PackStream* stream, AtmosphereDefinition* definition)
|
|
|
|
{
|
|
|
|
packWriteInt(stream, (int*)&definition->model);
|
2012-12-24 15:15:40 +00:00
|
|
|
packWriteInt(stream, &definition->hour);
|
|
|
|
packWriteInt(stream, &definition->minute);
|
2012-11-25 21:53:01 +00:00
|
|
|
colorSave(stream, &definition->sun_color);
|
|
|
|
packWriteDouble(stream, &definition->sun_radius);
|
|
|
|
packWriteDouble(stream, &definition->dome_lighting);
|
|
|
|
packWriteDouble(stream, &definition->humidity);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _loadDefinition(PackStream* stream, AtmosphereDefinition* definition)
|
|
|
|
{
|
|
|
|
packReadInt(stream, (int*)&definition->model);
|
2012-12-24 15:15:40 +00:00
|
|
|
packReadInt(stream, &definition->hour);
|
|
|
|
packReadInt(stream, &definition->minute);
|
2012-11-25 21:53:01 +00:00
|
|
|
colorLoad(stream, &definition->sun_color);
|
|
|
|
packReadDouble(stream, &definition->sun_radius);
|
|
|
|
packReadDouble(stream, &definition->dome_lighting);
|
|
|
|
packReadDouble(stream, &definition->humidity);
|
|
|
|
|
|
|
|
_validateDefinition(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
StandardDefinition AtmosphereDefinitionClass = {
|
|
|
|
(FuncObjectCreate)_createDefinition,
|
|
|
|
(FuncObjectDelete)_deleteDefinition,
|
|
|
|
(FuncObjectCopy)_copyDefinition,
|
|
|
|
(FuncObjectValidate)_validateDefinition,
|
|
|
|
(FuncObjectSave)_saveDefinition,
|
|
|
|
(FuncObjectLoad)_loadDefinition
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************** Binding ********************/
|
2012-12-02 11:08:56 +00:00
|
|
|
static Color _fakeApplyAerialPerspective(Renderer* renderer, Vector3 location, Color base)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
2012-12-02 11:08:56 +00:00
|
|
|
UNUSED(location);
|
2012-11-25 21:53:01 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
static Color _fakeGetSkyColor(Renderer* renderer, Vector3 direction)
|
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(direction);
|
|
|
|
return COLOR_WHITE;
|
|
|
|
}
|
|
|
|
static int _fakeGetSkydomeLights(Renderer* renderer, LightDefinition* lights, int max_lights)
|
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(lights);
|
|
|
|
UNUSED(max_lights);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Color _getSkyColor(Renderer* renderer, Vector3 direction)
|
|
|
|
{
|
|
|
|
AtmosphereDefinition* definition;
|
|
|
|
double dist;
|
2012-12-23 22:05:12 +00:00
|
|
|
Vector3 sun_direction, sun_position;
|
2012-11-25 21:53:01 +00:00
|
|
|
Color sky_color, sun_color;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
definition = renderer->atmosphere->definition;
|
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);
|
2012-12-23 22:05:12 +00:00
|
|
|
dist = v3Norm(v3Sub(direction, sun_direction));
|
|
|
|
|
|
|
|
sun_position = v3Scale(sun_direction, 149597870.0);
|
2012-11-25 21:53:01 +00:00
|
|
|
|
|
|
|
/* Get base scattering*/
|
|
|
|
switch (definition->model)
|
|
|
|
{
|
|
|
|
case ATMOSPHERE_MODEL_BRUNETON:
|
|
|
|
sky_color = brunetonGetSkyColor(definition, renderer->camera_location, direction, sun_position);
|
|
|
|
break;
|
|
|
|
case ATMOSPHERE_MODEL_PREETHAM:
|
|
|
|
sky_color = preethamGetSkyColor(definition, renderer->camera_location, direction, sun_position);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sky_color = COLOR_BLUE;
|
|
|
|
}
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
/* Get sun halo */
|
2013-01-16 15:26:33 +00:00
|
|
|
if (dist < definition->sun_radius)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
|
|
|
sun_color = definition->sun_color;
|
2013-01-16 15:26:33 +00:00
|
|
|
sun_color.r *= 100.0;
|
|
|
|
sun_color.g *= 100.0;
|
|
|
|
sun_color.b *= 100.0;
|
|
|
|
if (dist <= definition->sun_radius * 0.9)
|
2012-11-25 21:53:01 +00:00
|
|
|
{
|
|
|
|
return sun_color;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-16 15:26:33 +00:00
|
|
|
sun_color.a = (dist - definition->sun_radius * 0.9) / (definition->sun_radius * 0.1);
|
2012-11-25 21:53:01 +00:00
|
|
|
colorMask(&sky_color, &sun_color);
|
|
|
|
return sky_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return sky_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _getSunDirection(Renderer* renderer)
|
|
|
|
{
|
|
|
|
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-01-19 22:42:50 +00:00
|
|
|
#if 0
|
2012-11-25 21:53:01 +00:00
|
|
|
static inline void _addDomeLight(Renderer* renderer, LightDefinition* light, Vector3 direction, double factor)
|
|
|
|
{
|
|
|
|
light->direction = v3Scale(direction, -1.0);
|
|
|
|
light->color = renderer->atmosphere->getSkyColor(renderer, direction);
|
|
|
|
light->color.r *= factor;
|
|
|
|
light->color.g *= factor;
|
|
|
|
light->color.b *= factor;
|
|
|
|
light->reflection = 0.0;
|
|
|
|
light->filtered = 0;
|
|
|
|
light->masked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _getSkydomeLights(Renderer* renderer, LightDefinition* lights, int max_lights)
|
|
|
|
{
|
|
|
|
AtmosphereRendererCache* cache = (AtmosphereRendererCache*)renderer->atmosphere->_internal_data;
|
|
|
|
AtmosphereDefinition* definition;
|
|
|
|
double sun_angle;
|
|
|
|
Vector3 sun_direction;
|
|
|
|
int nblights = 0;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
mutexAcquire(cache->lock);
|
|
|
|
if (cache->nblights < 0)
|
|
|
|
{
|
|
|
|
definition = renderer->atmosphere->definition;
|
|
|
|
|
2012-12-24 15:15:40 +00:00
|
|
|
sun_angle = (definition->_daytime + 0.75) * M_PI * 2.0;
|
2012-11-25 21:53:01 +00:00
|
|
|
sun_direction.x = cos(sun_angle);
|
|
|
|
sun_direction.y = sin(sun_angle);
|
|
|
|
sun_direction.z = 0.0;
|
|
|
|
|
|
|
|
/* TODO Moon light */
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
if (max_lights > MAX_SKYDOME_LIGHTS)
|
|
|
|
{
|
|
|
|
max_lights = MAX_SKYDOME_LIGHTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_lights > 0)
|
|
|
|
{
|
|
|
|
/* Direct light from the sun */
|
|
|
|
cache->lights[0].direction = v3Scale(sun_direction, -1.0);
|
|
|
|
cache->lights[0].color = definition->sun_color;
|
|
|
|
cache->lights[0].reflection = 1.0;
|
|
|
|
cache->lights[0].filtered = 1;
|
|
|
|
cache->lights[0].masked = 1;
|
|
|
|
nblights = 1;
|
|
|
|
max_lights--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_lights > 0)
|
|
|
|
{
|
|
|
|
/* Indirect lighting by skydome scattering */
|
|
|
|
int xsamples, ysamples, samples, x, y;
|
|
|
|
double xstep, ystep, factor;
|
|
|
|
Vector3 direction;
|
|
|
|
|
|
|
|
samples = (renderer->render_quality < 5) ? 9 : (renderer->render_quality * 4 + 1);
|
|
|
|
samples = samples > max_lights ? max_lights : samples;
|
|
|
|
|
|
|
|
factor = definition->dome_lighting / (double)samples;
|
|
|
|
|
|
|
|
_addDomeLight(renderer, cache->lights + nblights, VECTOR_UP, factor);
|
|
|
|
nblights++;
|
|
|
|
samples--;
|
|
|
|
|
|
|
|
if (samples >= 2)
|
|
|
|
{
|
|
|
|
xsamples = samples / 2;
|
|
|
|
ysamples = samples / xsamples;
|
|
|
|
|
|
|
|
xstep = M_PI * 2.0 / (double)xsamples;
|
|
|
|
ystep = M_PI * 0.5 / (double)ysamples;
|
|
|
|
|
|
|
|
for (x = 0; x < xsamples; x++)
|
|
|
|
{
|
|
|
|
for (y = 0; y < ysamples; y++)
|
|
|
|
{
|
|
|
|
direction.x = cos(x * xstep) * cos(y * ystep);
|
|
|
|
direction.y = -sin(y * ystep);
|
|
|
|
direction.z = sin(x * xstep) * cos(y * ystep);
|
|
|
|
|
|
|
|
_addDomeLight(renderer, cache->lights + nblights, direction, factor);
|
|
|
|
nblights++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->nblights = nblights;
|
|
|
|
}
|
|
|
|
mutexRelease(cache->lock);
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
memcpy(lights, cache->lights, sizeof(LightDefinition) * cache->nblights);
|
|
|
|
return cache->nblights;
|
|
|
|
}
|
2013-01-19 22:42:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void _fakeGetLightingStatus(Renderer* renderer, LightStatus* status, Vector3 normal, int opaque)
|
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(status);
|
|
|
|
UNUSED(normal);
|
|
|
|
UNUSED(opaque);
|
|
|
|
}
|
2012-11-25 21:53:01 +00:00
|
|
|
|
|
|
|
/******************** Renderer ********************/
|
|
|
|
static AtmosphereRenderer* _createRenderer()
|
|
|
|
{
|
|
|
|
AtmosphereRenderer* result;
|
|
|
|
AtmosphereRendererCache* cache;
|
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;
|
2012-11-25 21:53:01 +00:00
|
|
|
result->getSunDirection = _getSunDirection;
|
|
|
|
result->applyAerialPerspective = _fakeApplyAerialPerspective;
|
|
|
|
result->getSkyColor = _fakeGetSkyColor;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
cache = malloc(sizeof(AtmosphereRendererCache));
|
|
|
|
cache->lock = mutexCreate();
|
|
|
|
cache->nblights = -1;
|
|
|
|
result->_internal_data = cache;
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _deleteRenderer(AtmosphereRenderer* renderer)
|
|
|
|
{
|
|
|
|
AtmosphereRendererCache* cache = (AtmosphereRendererCache*)renderer->_internal_data;
|
|
|
|
mutexDestroy(cache->lock);
|
|
|
|
free(cache);
|
2012-12-02 11:08:56 +00:00
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
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
|
|
|
AtmosphereRendererCache* cache = (AtmosphereRendererCache*)renderer->atmosphere->_internal_data;
|
2012-12-02 11:08:56 +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-01-20 15:07:45 +00:00
|
|
|
renderer->atmosphere->getSkyColor = _getSkyColor;
|
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 15:07:45 +00:00
|
|
|
renderer->atmosphere->applyAerialPerspective = brunetonApplyAerialPerspective;
|
|
|
|
/*renderer->atmosphere->getLightingStatus = brunetonGetLightingStatus;*/
|
|
|
|
renderer->atmosphere->getLightingStatus = basicGetLightingStatus;
|
2012-12-24 11:24:27 +00:00
|
|
|
break;
|
2012-12-02 11:08:56 +00:00
|
|
|
default:
|
2013-01-20 15:07:45 +00:00
|
|
|
renderer->atmosphere->applyAerialPerspective = basicApplyAerialPerspective;
|
|
|
|
renderer->atmosphere->getLightingStatus = basicGetLightingStatus;
|
2012-12-02 11:08:56 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
mutexAcquire(cache->lock);
|
|
|
|
cache->nblights = -1;
|
|
|
|
mutexRelease(cache->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
StandardRenderer AtmosphereRendererClass = {
|
|
|
|
(FuncObjectCreate)_createRenderer,
|
|
|
|
(FuncObjectDelete)_deleteRenderer,
|
|
|
|
(FuncObjectBind)_bindRenderer
|
|
|
|
};
|