2012-01-22 18:39:42 +00:00
|
|
|
#include "lighting.h"
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2012-01-22 18:39:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
#include "shared/types.h"
|
|
|
|
#include "shared/functions.h"
|
|
|
|
#include "shared/constants.h"
|
|
|
|
#include "shared/globals.h"
|
2012-01-22 22:06:11 +00:00
|
|
|
#include "sky.h"
|
|
|
|
#include "water.h"
|
|
|
|
#include "terrain.h"
|
2012-01-23 23:45:33 +00:00
|
|
|
#include "renderer.h"
|
|
|
|
#include "scenery.h"
|
2012-01-22 18:39:42 +00:00
|
|
|
|
|
|
|
static LightDefinition _LIGHT_NULL;
|
|
|
|
|
|
|
|
void lightingInit()
|
|
|
|
{
|
|
|
|
_LIGHT_NULL.color = COLOR_BLACK;
|
|
|
|
_LIGHT_NULL.direction.x = 0.0;
|
|
|
|
_LIGHT_NULL.direction.y = 1.0;
|
|
|
|
_LIGHT_NULL.direction.z = 0.0;
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
void lightingSave(FILE* f, LightingDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-24 13:16:20 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
toolsSaveInt(f, definition->autosetfromsky);
|
|
|
|
toolsSaveInt(f, definition->nblights);
|
|
|
|
for (i = 0; i < definition->nblights; i++)
|
|
|
|
{
|
|
|
|
colorSave(definition->lights[i].color, f);
|
|
|
|
v3Save(definition->lights[i].direction, f);
|
|
|
|
toolsSaveDouble(f, definition->lights[i].maxshadow);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
void lightingLoad(FILE* f, LightingDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-24 13:16:20 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
definition->autosetfromsky = toolsLoadInt(f);
|
|
|
|
definition->nblights = toolsLoadInt(f);
|
|
|
|
for (i = 0; i < definition->nblights; i++)
|
|
|
|
{
|
|
|
|
definition->lights[i].color = colorLoad(f);
|
|
|
|
definition->lights[i].direction = v3Load(f);
|
|
|
|
definition->lights[i].maxshadow = toolsLoadDouble(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
lightingValidateDefinition(definition);
|
2012-01-22 18:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LightingDefinition lightingCreateDefinition()
|
|
|
|
{
|
|
|
|
LightingDefinition definition;
|
|
|
|
|
|
|
|
definition.autosetfromsky = 0;
|
|
|
|
definition.nblights = 0;
|
|
|
|
definition._nbautolights = 0;
|
|
|
|
|
|
|
|
return definition;
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void lightingDeleteDefinition(LightingDefinition* definition)
|
2012-01-22 18:39:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void lightingCopyDefinition(LightingDefinition* source, LightingDefinition* destination)
|
2012-01-22 18:39:42 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
*destination = *source;
|
2012-01-22 18:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lightingValidateDefinition(LightingDefinition* definition)
|
|
|
|
{
|
|
|
|
if (definition->autosetfromsky)
|
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
SkyDefinition sky;
|
|
|
|
|
|
|
|
sky = skyCreateDefinition();
|
|
|
|
sceneryGetSky(&sky);
|
|
|
|
definition->_nbautolights = skyGetLights(&sky, definition->_autolights, LIGHTING_MAX_LIGHTS);
|
|
|
|
skyDeleteDefinition(&sky);
|
2012-01-22 18:39:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
definition->_nbautolights = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int lightingGetLightCount(LightingDefinition* definition)
|
|
|
|
{
|
|
|
|
return definition->nblights;
|
|
|
|
}
|
|
|
|
|
|
|
|
LightDefinition lightingGetLight(LightingDefinition* definition, int light)
|
|
|
|
{
|
|
|
|
if (light >= 0 && light < definition->nblights)
|
|
|
|
{
|
|
|
|
return definition->lights[light];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _LIGHT_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int lightingAddLight(LightingDefinition* definition, LightDefinition light)
|
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
if (definition->nblights < LIGHTING_MAX_LIGHTS)
|
2012-01-22 18:39:42 +00:00
|
|
|
{
|
|
|
|
definition->lights[definition->nblights] = light;
|
|
|
|
return definition->nblights++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lightingDeleteLight(LightingDefinition* definition, int light)
|
|
|
|
{
|
|
|
|
if (light >= 0 && light < definition->nblights)
|
|
|
|
{
|
|
|
|
if (definition->nblights > 1 && light < definition->nblights - 1)
|
|
|
|
{
|
|
|
|
memmove(definition->lights + light, definition->lights + light + 1, sizeof(LightDefinition) * definition->nblights - light - 1);
|
|
|
|
}
|
|
|
|
definition->nblights--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
static Color _applyLightCustom(LightDefinition* definition, Renderer* renderer, Vector3 location, Vector3 normal, SurfaceMaterial material)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Color result, light;
|
2012-01-22 22:06:11 +00:00
|
|
|
double diffuse, specular;
|
|
|
|
Vector3 view, reflect, direction_inv;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
light = definition->color;
|
|
|
|
|
|
|
|
direction_inv = v3Scale(definition->direction, -1.0);
|
2012-01-23 23:45:33 +00:00
|
|
|
light = renderer->filterLight(renderer, light, location, v3Add(location, direction_inv), direction_inv);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
normal = v3Normalize(normal);
|
2012-01-23 23:45:33 +00:00
|
|
|
view = v3Normalize(v3Sub(location, renderer->camera_location));
|
2012-01-22 22:06:11 +00:00
|
|
|
reflect = v3Sub(v3Scale(normal, 2.0 * v3Dot(direction_inv, normal)), direction_inv);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
diffuse = v3Dot(direction_inv, normal);
|
|
|
|
//diffuse = pow(diffuse * 0.5 + 0.5, 2.0);
|
2012-01-23 23:45:33 +00:00
|
|
|
diffuse = diffuse * 0.5 + 0.5;
|
2011-12-10 13:25:22 +00:00
|
|
|
if (diffuse > 0.0)
|
|
|
|
{
|
2012-01-22 22:06:11 +00:00
|
|
|
if (material.shininess > 0.0)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-22 22:06:11 +00:00
|
|
|
specular = pow(v3Dot(reflect, view) * material.reflection, material.shininess * 10.0 + 1.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
specular = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diffuse = 0.0;
|
|
|
|
specular = 0.0;
|
|
|
|
}
|
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
result.r = material.base.r * diffuse * light.r + material.base.r * specular * light.r;
|
|
|
|
result.g = material.base.g * diffuse * light.g + material.base.g * specular * light.g;
|
|
|
|
result.b = material.base.b * diffuse * light.b + material.base.b * specular * light.b;
|
|
|
|
result.a = material.base.a;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
Color lightingApplyToSurface(LightingDefinition* definition, Renderer* renderer, Vector3 location, Vector3 normal, SurfaceMaterial material)
|
2012-01-22 22:06:11 +00:00
|
|
|
{
|
|
|
|
Color result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* TODO Merge lights */
|
|
|
|
result = material.base;
|
|
|
|
for (i = 0; i < definition->nblights; i++)
|
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
result = _applyLightCustom(definition->lights + i, renderer, location, normal, material);
|
2012-01-22 22:06:11 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < definition->_nbautolights; i++)
|
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
result = _applyLightCustom(definition->_autolights + i, renderer, location, normal, material);
|
2012-01-22 22:06:11 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
return result;
|
2012-01-22 22:06:11 +00:00
|
|
|
}
|