2012-01-24 15:47:49 +00:00
|
|
|
#include "sky.h"
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "shared/types.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "color.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
#include "clouds.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "euclid.h"
|
2012-01-22 22:06:11 +00:00
|
|
|
#include "lighting.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "render.h"
|
|
|
|
#include "tools.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
#define SPHERE_SIZE 1000.0
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
/******************************** Configuration ********************************/
|
2011-12-17 10:55:37 +00:00
|
|
|
void skyInit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-12 16:57:29 +00:00
|
|
|
void skyQuit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void skySave(PackStream* stream, SkyDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
packWriteInt(stream, (int*)&definition->model);
|
2012-06-17 09:40:40 +00:00
|
|
|
packWriteDouble(stream, &definition->daytime);
|
2012-06-22 22:13:55 +00:00
|
|
|
colorSave(stream, &definition->sun_color);
|
2012-06-17 09:40:40 +00:00
|
|
|
packWriteDouble(stream, &definition->sun_radius);
|
|
|
|
packWriteDouble(stream, &definition->sun_halo_size);
|
2012-06-07 19:26:23 +00:00
|
|
|
curveSave(stream, definition->sun_halo_profile);
|
2012-06-22 22:13:55 +00:00
|
|
|
|
|
|
|
packWriteInt(stream, &definition->model_custom.auto_from_daytime);
|
|
|
|
colorSave(stream, &definition->model_custom.zenith_color);
|
|
|
|
colorSave(stream, &definition->model_custom.haze_color);
|
|
|
|
packWriteDouble(stream, &definition->model_custom.haze_height);
|
|
|
|
packWriteDouble(stream, &definition->model_custom.haze_smoothing);
|
|
|
|
|
|
|
|
packWriteDouble(stream, &definition->model_preetham.turbidity);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void skyLoad(PackStream* stream, SkyDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
packReadInt(stream, (int*)&definition->model);
|
2012-06-17 09:40:40 +00:00
|
|
|
packReadDouble(stream, &definition->daytime);
|
2012-06-22 22:13:55 +00:00
|
|
|
colorLoad(stream, &definition->sun_color);
|
2012-06-17 09:40:40 +00:00
|
|
|
packReadDouble(stream, &definition->sun_radius);
|
|
|
|
packReadDouble(stream, &definition->sun_halo_size);
|
2012-06-07 19:26:23 +00:00
|
|
|
curveLoad(stream, definition->sun_halo_profile);
|
2012-06-22 22:13:55 +00:00
|
|
|
|
|
|
|
packReadInt(stream, &definition->model_custom.auto_from_daytime);
|
|
|
|
colorLoad(stream, &definition->model_custom.zenith_color);
|
|
|
|
colorLoad(stream, &definition->model_custom.haze_color);
|
|
|
|
packReadDouble(stream, &definition->model_custom.haze_height);
|
|
|
|
packReadDouble(stream, &definition->model_custom.haze_smoothing);
|
|
|
|
|
|
|
|
packReadDouble(stream, &definition->model_preetham.turbidity);
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
skyValidateDefinition(definition);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 10:55:37 +00:00
|
|
|
SkyDefinition skyCreateDefinition()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2011-12-17 10:55:37 +00:00
|
|
|
SkyDefinition def;
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
def.model = SKY_MODEL_CUSTOM;
|
2011-12-17 10:55:37 +00:00
|
|
|
def.daytime = 0.0;
|
2012-06-22 22:13:55 +00:00
|
|
|
def.sun_color = COLOR_BLACK;
|
2011-12-17 10:55:37 +00:00
|
|
|
def.sun_radius = 1.0;
|
2012-06-07 19:26:23 +00:00
|
|
|
def.sun_halo_size = 0.0;
|
|
|
|
def.sun_halo_profile = curveCreate();
|
2012-06-22 22:13:55 +00:00
|
|
|
def.model_custom.auto_from_daytime = 0;
|
|
|
|
def.model_custom.zenith_color = COLOR_BLACK;
|
|
|
|
def.model_custom.haze_color = COLOR_BLACK;
|
|
|
|
def.model_custom.haze_height = 0.0;
|
|
|
|
def.model_custom.haze_smoothing = 0.0;
|
|
|
|
def.model_custom._sky_gradation = colorGradationCreate();
|
|
|
|
def.model_preetham.turbidity = 0.0;
|
2011-12-17 10:55:37 +00:00
|
|
|
|
|
|
|
skyValidateDefinition(&def);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void skyDeleteDefinition(SkyDefinition* definition)
|
2011-12-17 10:55:37 +00:00
|
|
|
{
|
2012-06-07 19:26:23 +00:00
|
|
|
curveDelete(definition->sun_halo_profile);
|
2012-06-22 22:13:55 +00:00
|
|
|
colorGradationDelete(definition->model_custom._sky_gradation);
|
2011-12-17 10:55:37 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void skyCopyDefinition(SkyDefinition* source, SkyDefinition* destination)
|
2011-12-17 10:55:37 +00:00
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
destination->model = source->model;
|
2012-03-08 15:10:25 +00:00
|
|
|
destination->daytime = source->daytime;
|
2012-06-22 22:13:55 +00:00
|
|
|
destination->sun_color = source->sun_color;
|
2012-03-08 15:10:25 +00:00
|
|
|
destination->sun_radius = source->sun_radius;
|
2012-06-07 19:26:23 +00:00
|
|
|
destination->sun_halo_size = source->sun_halo_size;
|
2012-06-22 22:13:55 +00:00
|
|
|
destination->model_custom.auto_from_daytime = source->model_custom.auto_from_daytime;
|
|
|
|
destination->model_custom.zenith_color = source->model_custom.zenith_color;
|
|
|
|
destination->model_custom.haze_color = source->model_custom.haze_color;
|
|
|
|
destination->model_custom.haze_height = source->model_custom.haze_height;
|
|
|
|
destination->model_custom.haze_smoothing = source->model_custom.haze_smoothing;
|
|
|
|
destination->model_preetham.turbidity = source->model_preetham.turbidity;
|
2012-03-08 15:10:25 +00:00
|
|
|
|
2012-06-07 19:26:23 +00:00
|
|
|
curveCopy(source->sun_halo_profile, destination->sun_halo_profile);
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
skyValidateDefinition(destination);
|
2011-12-17 10:55:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
static void _setAutoCustomModel(SkyDefinition* definition)
|
2011-12-17 10:55:37 +00:00
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
ColorGradation* zenith_gradation;
|
|
|
|
ColorGradation* haze_gradation;
|
|
|
|
|
|
|
|
zenith_gradation = colorGradationCreate();
|
|
|
|
haze_gradation = colorGradationCreate();
|
|
|
|
|
|
|
|
colorGradationQuickAddRgb(zenith_gradation, 0.2, 0.03, 0.03, 0.05);
|
|
|
|
colorGradationQuickAddRgb(zenith_gradation, 0.25, 0.25, 0.33, 0.37);
|
|
|
|
colorGradationQuickAddRgb(zenith_gradation, 0.35, 0.52, 0.63, 0.8);
|
|
|
|
colorGradationQuickAddRgb(zenith_gradation, 0.65, 0.52, 0.63, 0.8);
|
|
|
|
colorGradationQuickAddRgb(zenith_gradation, 0.75, 0.25, 0.33, 0.37);
|
|
|
|
colorGradationQuickAddRgb(zenith_gradation, 0.8, 0.03, 0.03, 0.05);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.2, 0.05, 0.05, 0.08);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.25, 0.55, 0.42, 0.42);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.3, 0.6, 0.6, 0.6);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.4, 0.92, 0.93, 1.0);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.6, 0.92, 0.93, 1.0);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.7, 0.6, 0.6, 0.8);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.75, 0.62, 0.50, 0.42);
|
|
|
|
colorGradationQuickAddRgb(haze_gradation, 0.8, 0.05, 0.05, 0.08);
|
|
|
|
|
|
|
|
definition->model_custom.zenith_color = colorGradationGet(zenith_gradation, definition->daytime);
|
|
|
|
definition->model_custom.haze_color = colorGradationGet(haze_gradation, definition->daytime);
|
|
|
|
|
|
|
|
colorGradationDelete(zenith_gradation);
|
|
|
|
colorGradationDelete(haze_gradation);
|
|
|
|
}
|
2011-12-17 10:55:37 +00:00
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
void skyValidateDefinition(SkyDefinition* definition)
|
|
|
|
{
|
|
|
|
if (definition->model == SKY_MODEL_CUSTOM)
|
|
|
|
{
|
|
|
|
if (definition->model_custom.auto_from_daytime)
|
|
|
|
{
|
|
|
|
_setAutoCustomModel(definition);
|
|
|
|
}
|
|
|
|
colorGradationClear(definition->model_custom._sky_gradation);
|
|
|
|
colorGradationQuickAdd(definition->model_custom._sky_gradation, 0.0, &definition->model_custom.haze_color);
|
|
|
|
colorGradationQuickAdd(definition->model_custom._sky_gradation, definition->model_custom.haze_height - definition->model_custom.haze_smoothing, &definition->model_custom.haze_color);
|
|
|
|
colorGradationQuickAdd(definition->model_custom._sky_gradation, definition->model_custom.haze_height, &definition->model_custom.zenith_color);
|
|
|
|
colorGradationQuickAdd(definition->model_custom._sky_gradation, 1.0, &definition->model_custom.zenith_color);
|
|
|
|
}
|
2011-12-17 10:55:37 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights)
|
2012-01-22 22:06:11 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double sun_angle;
|
2012-01-22 22:06:11 +00:00
|
|
|
Vector3 sun_direction;
|
|
|
|
int nblights = 0;
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
sun_angle = (sky->daytime + 0.75) * M_PI * 2.0;
|
2012-01-22 22:06:11 +00:00
|
|
|
sun_direction.x = cos(sun_angle);
|
|
|
|
sun_direction.y = sin(sun_angle);
|
|
|
|
sun_direction.z = 0.0;
|
|
|
|
|
2012-01-25 17:31:36 +00:00
|
|
|
/* TODO Night lights */
|
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
if (max_lights > 0)
|
|
|
|
{
|
2012-01-25 17:31:36 +00:00
|
|
|
/* Light from the sun */
|
2012-01-22 22:06:11 +00:00
|
|
|
lights[0].direction = v3Scale(sun_direction, -1.0);
|
2012-06-22 22:13:55 +00:00
|
|
|
lights[0].color = sky->sun_color;
|
2012-01-26 18:20:19 +00:00
|
|
|
lights[0].reflection = 1.0;
|
2012-01-25 17:31:36 +00:00
|
|
|
lights[0].filtered = 1;
|
|
|
|
lights[0].masked = 1;
|
|
|
|
lights[0].amplitude = 0.0;
|
2012-01-22 22:06:11 +00:00
|
|
|
nblights = 1;
|
2012-01-25 17:31:36 +00:00
|
|
|
if (max_lights > 1)
|
|
|
|
{
|
|
|
|
/* Skydome lighting */
|
|
|
|
lights[1].direction.x = 0.0;
|
|
|
|
lights[1].direction.y = -1.0;
|
|
|
|
lights[1].direction.z = 0.0;
|
2012-06-22 22:13:55 +00:00
|
|
|
lights[1].color = sky->model_custom.zenith_color;
|
2012-02-15 16:39:57 +00:00
|
|
|
lights[1].color.r *= 0.6;
|
|
|
|
lights[1].color.g *= 0.6;
|
|
|
|
lights[1].color.b *= 0.6;
|
2012-01-26 18:20:19 +00:00
|
|
|
lights[1].reflection = 0.0;
|
2012-01-25 17:31:36 +00:00
|
|
|
lights[1].filtered = 1;
|
|
|
|
lights[1].masked = 0;
|
|
|
|
lights[1].amplitude = M_PI / 2.0;
|
|
|
|
nblights = 2;
|
|
|
|
}
|
2012-01-22 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nblights;
|
|
|
|
}
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
/******************************** Preetham Model ********************************/
|
2012-06-20 20:29:58 +00:00
|
|
|
static inline double _angleBetween(double thetav, double phiv, double theta, double phi)
|
|
|
|
{
|
|
|
|
double cospsi = sin(thetav) * sin(theta) * cos(phi-phiv) + cos(thetav) * cos(theta);
|
|
|
|
if (cospsi > 1.0) return 0.0;
|
|
|
|
if (cospsi < -1.0) return M_PI;
|
|
|
|
return acos(cospsi);
|
|
|
|
}
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
static inline Color _xyYToRGB(double x, double y, double Y)
|
2012-06-20 20:29:58 +00:00
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
double fX, fY, fZ;
|
2012-06-21 18:21:21 +00:00
|
|
|
Color result;
|
|
|
|
|
|
|
|
fY = Y;
|
|
|
|
fX = x / y * Y;
|
2012-06-22 22:13:55 +00:00
|
|
|
fZ = ((1.0 - x - y) / y) * Y;
|
2012-06-21 18:21:21 +00:00
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
double r, g, b;
|
|
|
|
r = 3.2404 * fX - 1.5371 * fY - 0.4985 * fZ;
|
|
|
|
g = -0.9692 * fX + 1.8759 * fY + 0.0415 * fZ;
|
|
|
|
b = 0.0556 * fX - 0.2040 * fY + 1.0573 * fZ;
|
2012-06-21 18:21:21 +00:00
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
double expo = -(1.0 / 10000.0);
|
|
|
|
r = 1.0 - exp(expo * r);
|
|
|
|
g = 1.0 - exp(expo * g);
|
|
|
|
b = 1.0 - exp(expo * b);
|
2012-06-21 18:21:21 +00:00
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
if (r < 0.0) r = 0.0;
|
|
|
|
if (g < 0.0) g = 0.0;
|
|
|
|
if (b < 0.0) b = 0.0;
|
2012-06-21 18:21:21 +00:00
|
|
|
|
|
|
|
result.r = r;
|
|
|
|
result.g = g;
|
|
|
|
result.b = b;
|
|
|
|
result.a = 1.0;
|
|
|
|
|
|
|
|
colorNormalize(&result);
|
|
|
|
|
|
|
|
return result;
|
2012-06-20 20:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Color _preethamApproximate(SkyDefinition* definition, double theta, double phi)
|
|
|
|
{
|
|
|
|
double thetaSun;
|
|
|
|
double phiSun;
|
|
|
|
double gamma;
|
2012-06-22 22:13:55 +00:00
|
|
|
double turbidity = definition->model_preetham.turbidity;
|
2012-06-20 20:29:58 +00:00
|
|
|
|
|
|
|
/* Handle angles */
|
2012-06-21 18:21:21 +00:00
|
|
|
if (theta > M_PI / 2.0)
|
|
|
|
{
|
|
|
|
theta = M_PI / 2.0;
|
|
|
|
}
|
2012-06-20 20:29:58 +00:00
|
|
|
if (definition->daytime <= 0.5)
|
|
|
|
{
|
2012-06-21 18:21:21 +00:00
|
|
|
thetaSun = M_PI - definition->daytime * 2.0 * M_PI;
|
2012-06-20 20:29:58 +00:00
|
|
|
phiSun = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-21 18:21:21 +00:00
|
|
|
thetaSun = (definition->daytime - 0.5) * 2.0 * M_PI;
|
2012-06-20 20:29:58 +00:00
|
|
|
phiSun = M_PI;
|
|
|
|
}
|
|
|
|
gamma = _angleBetween(theta, phi, thetaSun, phiSun);
|
2012-06-21 18:21:21 +00:00
|
|
|
|
|
|
|
double cosTheta;
|
|
|
|
if (theta > M_PI / 2.0)
|
|
|
|
{
|
|
|
|
cosTheta = 0.0000001;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cosTheta = cos(theta);
|
|
|
|
}
|
|
|
|
|
|
|
|
double T = turbidity;
|
|
|
|
double T2 = T * T;
|
|
|
|
double suntheta = thetaSun;
|
|
|
|
double suntheta2 = thetaSun * thetaSun;
|
|
|
|
double suntheta3 = thetaSun * suntheta2;
|
|
|
|
|
|
|
|
double Ax = -0.01925 * T - 0.25922;
|
|
|
|
double Bx = -0.06651 * T + 0.00081;
|
|
|
|
double Cx = -0.00041 * T + 0.21247;
|
|
|
|
double Dx = -0.06409 * T - 0.89887;
|
|
|
|
double Ex = -0.00325 * T + 0.04517;
|
|
|
|
|
|
|
|
double Ay = -0.01669 * T - 0.26078;
|
|
|
|
double By = -0.09495 * T + 0.00921;
|
|
|
|
double Cy = -0.00792 * T + 0.21023;
|
|
|
|
double Dy = -0.04405 * T - 1.65369;
|
|
|
|
double Ey = -0.01092 * T + 0.05291;
|
|
|
|
|
|
|
|
double AY = 0.17872 * T - 1.46303;
|
|
|
|
double BY = -0.35540 * T + 0.42749;
|
|
|
|
double CY = -0.02266 * T + 5.32505;
|
|
|
|
double DY = 0.12064 * T - 2.57705;
|
|
|
|
double EY = -0.06696 * T + 0.37027;
|
|
|
|
|
|
|
|
double cosGamma = cos(gamma);
|
|
|
|
cosGamma = cosGamma < 0.0 ? 0.0 : cosGamma;
|
|
|
|
double cosSTheta = fabs(cos(thetaSun));
|
|
|
|
|
|
|
|
double xz = ( 0.00165 * suntheta3 - 0.00375 * suntheta2 + 0.00209 * suntheta + 0.00000) * T2 +
|
|
|
|
(-0.02903 * suntheta3 + 0.06377 * suntheta2 - 0.03202 * suntheta + 0.00394) * T +
|
|
|
|
( 0.11693 * suntheta3 - 0.21196 * suntheta2 + 0.06052 * suntheta + 0.25886);
|
|
|
|
|
|
|
|
double yz = ( 0.00275 * suntheta3 - 0.00610 * suntheta2 + 0.00317 * suntheta + 0.00000) * T2 +
|
|
|
|
(-0.04214 * suntheta3 + 0.08970 * suntheta2 - 0.04153 * suntheta + 0.00516) * T +
|
|
|
|
( 0.15346 * suntheta3 - 0.26756 * suntheta2 + 0.06670 * suntheta + 0.26688);
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
double X = (4.0 / 9.0 - T / 120.0) * (M_PI - 2.0 * suntheta);
|
|
|
|
double Yz = ((4.0453 * T - 4.9710) * tan(X) - 0.2155 * T + 2.4192) * 1000.0;
|
2012-06-21 18:21:21 +00:00
|
|
|
|
|
|
|
double val1, val2;
|
2012-06-22 22:13:55 +00:00
|
|
|
|
|
|
|
val1 = (1.0 + Ax * exp(Bx / cosTheta)) * (1.0 + Cx * exp(Dx * gamma) + Ex * sqrt(cosGamma));
|
|
|
|
val2 = (1.0 + Ax * exp(Bx)) * (1.0 + Cx * exp(Dx * suntheta) + Ex * sqrt(cosSTheta));
|
2012-06-21 18:21:21 +00:00
|
|
|
double x = xz * val1 / val2;
|
2012-06-22 22:13:55 +00:00
|
|
|
|
|
|
|
val1 = (1.0 + Ay * exp(By / cosTheta)) * (1.0 + Cy * exp(Dy * gamma) + Ey * sqrt(cosGamma));
|
|
|
|
val2 = (1.0 + Ay * exp(By)) * (1.0 + Cy * exp(Dy * suntheta) + Ey * sqrt(cosSTheta));
|
2012-06-21 18:21:21 +00:00
|
|
|
double y = yz * val1 / val2;
|
2012-06-22 22:13:55 +00:00
|
|
|
|
|
|
|
val1 = (1.0 + AY * exp(BY / cosTheta)) * (1.0 + CY * exp(DY * gamma) + EY * sqrt(cosGamma));
|
|
|
|
val2 = (1.0 + AY * exp(BY)) * (1.0 + CY * exp(DY * suntheta) + EY * sqrt(cosSTheta));
|
2012-06-21 18:21:21 +00:00
|
|
|
double Y = Yz * val1 / val2;
|
2012-06-20 20:29:58 +00:00
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
return _xyYToRGB(x, y, Y);
|
2012-06-20 20:29:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
/******************************** Rendering ********************************/
|
2012-01-23 23:45:33 +00:00
|
|
|
Color skyGetColor(SkyDefinition* definition, Renderer* renderer, Vector3 eye, Vector3 look)
|
2011-12-17 10:55:37 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double dist;
|
2011-12-18 21:59:33 +00:00
|
|
|
Vector3 sun_position;
|
|
|
|
Color sun_color, sky_color;
|
2012-06-20 20:29:58 +00:00
|
|
|
|
2012-05-08 14:07:47 +00:00
|
|
|
sun_position = skyGetSunDirection(definition);
|
2011-12-18 21:59:33 +00:00
|
|
|
|
2011-12-17 10:55:37 +00:00
|
|
|
look = v3Normalize(look);
|
2011-12-18 21:59:33 +00:00
|
|
|
dist = v3Norm(v3Sub(look, sun_position));
|
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
if (definition->model == SKY_MODEL_PREETHAM)
|
|
|
|
{
|
|
|
|
sky_color = _preethamApproximate(definition, M_PI/2.0 - asin(look.y), atan2(-look.z, look.x));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sky_color = colorGradationGet(definition->model_custom._sky_gradation, look.y * 0.5 + 0.5);
|
|
|
|
}
|
|
|
|
|
2012-06-07 19:26:23 +00:00
|
|
|
if (dist < definition->sun_radius + definition->sun_halo_size)
|
2011-12-18 21:59:33 +00:00
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
sun_color = definition->sun_color;
|
2012-06-07 19:26:23 +00:00
|
|
|
if (dist <= definition->sun_radius)
|
2011-12-18 21:59:33 +00:00
|
|
|
{
|
|
|
|
return sun_color;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-07 19:26:23 +00:00
|
|
|
dist = (dist - definition->sun_radius) / definition->sun_halo_size;
|
|
|
|
sun_color.a = curveGetValue(definition->sun_halo_profile, dist);
|
2011-12-18 21:59:33 +00:00
|
|
|
colorMask(&sky_color, &sun_color);
|
|
|
|
return sky_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return sky_color;
|
|
|
|
}
|
2011-12-17 10:55:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static Color _postProcessFragment(Renderer* renderer, Vector3 location, void* data)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
Vector3 direction;
|
2012-01-24 13:16:20 +00:00
|
|
|
Color result;
|
|
|
|
SkyDefinition* definition;
|
|
|
|
|
|
|
|
definition = (SkyDefinition*)data;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
direction = v3Sub(location, renderer->camera_location);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
result = skyGetColor(definition, renderer, renderer->camera_location, v3Normalize(direction));
|
|
|
|
result = renderer->applyClouds(renderer, result, renderer->camera_location, v3Add(renderer->camera_location, v3Scale(direction, 10.0)));
|
2011-12-17 10:55:37 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
return result;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void skyRender(SkyDefinition* definition, Renderer* renderer)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int res_i, res_j;
|
|
|
|
int i, j;
|
2012-06-17 09:40:40 +00:00
|
|
|
double step_i, step_j;
|
|
|
|
double current_i, current_j;
|
|
|
|
Vector3 vertex1, vertex2, vertex3, vertex4;
|
2011-12-10 13:25:22 +00:00
|
|
|
Vector3 direction;
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
res_i = renderer->render_quality * 40;
|
|
|
|
res_j = renderer->render_quality * 20;
|
2012-06-17 09:40:40 +00:00
|
|
|
step_i = M_PI * 2.0 / (double)res_i;
|
|
|
|
step_j = M_PI / (double)res_j;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
for (j = 0; j < res_j; j++)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
if (!renderer->addRenderProgress(renderer, 0.0))
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-17 10:55:37 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
current_j = (double)(j - res_j / 2) * step_j;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < res_i; i++)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
current_i = (double)i * step_i;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
direction.x = SPHERE_SIZE * cos(current_i) * cos(current_j);
|
|
|
|
direction.y = SPHERE_SIZE * sin(current_j);
|
|
|
|
direction.z = SPHERE_SIZE * sin(current_i) * cos(current_j);
|
2012-06-17 09:40:40 +00:00
|
|
|
vertex1 = v3Add(renderer->camera_location, direction);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
direction.x = SPHERE_SIZE * cos(current_i + step_i) * cos(current_j);
|
|
|
|
direction.y = SPHERE_SIZE * sin(current_j);
|
|
|
|
direction.z = SPHERE_SIZE * sin(current_i + step_i) * cos(current_j);
|
2012-06-17 09:40:40 +00:00
|
|
|
vertex2 = v3Add(renderer->camera_location, direction);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
direction.x = SPHERE_SIZE * cos(current_i + step_i) * cos(current_j + step_j);
|
|
|
|
direction.y = SPHERE_SIZE * sin(current_j + step_j);
|
|
|
|
direction.z = SPHERE_SIZE * sin(current_i + step_i) * cos(current_j + step_j);
|
2012-06-17 09:40:40 +00:00
|
|
|
vertex3 = v3Add(renderer->camera_location, direction);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
direction.x = SPHERE_SIZE * cos(current_i) * cos(current_j + step_j);
|
|
|
|
direction.y = SPHERE_SIZE * sin(current_j + step_j);
|
|
|
|
direction.z = SPHERE_SIZE * sin(current_i) * cos(current_j + step_j);
|
2012-06-17 09:40:40 +00:00
|
|
|
vertex4 = v3Add(renderer->camera_location, direction);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
/* TODO Triangles at poles */
|
2012-06-17 09:40:40 +00:00
|
|
|
renderer->pushQuad(renderer, vertex1, vertex4, vertex3, vertex2, _postProcessFragment, definition);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 14:07:47 +00:00
|
|
|
|
|
|
|
Vector3 skyGetSunDirection(SkyDefinition* definition)
|
|
|
|
{
|
|
|
|
Vector3 result;
|
2012-06-17 09:40:40 +00:00
|
|
|
double sun_angle = (definition->daytime + 0.75) * M_PI * 2.0;
|
2012-05-08 14:07:47 +00:00
|
|
|
result.x = cos(sun_angle);
|
|
|
|
result.y = sin(sun_angle);
|
|
|
|
result.z = 0.0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color skyGetSunColor(SkyDefinition* definition)
|
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
return definition->sun_color;
|
2012-05-08 14:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Color skyGetZenithColor(SkyDefinition* definition)
|
|
|
|
{
|
2012-06-22 22:13:55 +00:00
|
|
|
return definition->model_custom.zenith_color;
|
2012-05-08 14:07:47 +00:00
|
|
|
}
|