2012-01-29 17:39:56 +00:00
|
|
|
#include "textures.h"
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-01-03 15:40:50 +00:00
|
|
|
#include <assert.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
#include "shared/types.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "color.h"
|
|
|
|
#include "euclid.h"
|
2012-01-22 22:06:11 +00:00
|
|
|
#include "lighting.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "terrain.h"
|
|
|
|
#include "tools.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
static TextureLayerDefinition _NULL_LAYER;
|
2012-01-03 15:40:50 +00:00
|
|
|
|
2012-05-06 08:49:12 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double thickness;
|
2012-05-06 08:49:12 +00:00
|
|
|
Vector3 location;
|
|
|
|
Vector3 normal;
|
2012-05-07 08:56:22 +00:00
|
|
|
TextureLayerDefinition* definition;
|
2012-05-06 08:49:12 +00:00
|
|
|
} TextureResult;
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void texturesInit()
|
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
_NULL_LAYER = texturesLayerCreateDefinition();
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-02-12 16:57:29 +00:00
|
|
|
void texturesQuit()
|
|
|
|
{
|
|
|
|
texturesLayerDeleteDefinition(&_NULL_LAYER);
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void texturesSave(PackStream* stream, TexturesDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-07-05 17:55:25 +00:00
|
|
|
TextureLayerDefinition* layer;
|
2012-01-03 15:40:50 +00:00
|
|
|
int i;
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
packWriteInt(stream, &definition->nblayers);
|
|
|
|
for (i = 0; i < definition->nblayers; i++)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 17:55:25 +00:00
|
|
|
layer = definition->layers + i;
|
|
|
|
|
|
|
|
packWriteString(stream, layer->name, TEXTURES_MAX_NAME_LENGTH);
|
|
|
|
zoneSave(stream, layer->zone);
|
|
|
|
noiseSaveGenerator(stream, layer->bump_noise);
|
|
|
|
packWriteDouble(stream, &layer->bump_height);
|
|
|
|
packWriteDouble(stream, &layer->bump_scaling);
|
|
|
|
materialSave(stream, &layer->material);
|
|
|
|
packWriteDouble(stream, &layer->thickness);
|
|
|
|
packWriteDouble(stream, &layer->slope_range);
|
|
|
|
packWriteDouble(stream, &layer->thickness_transparency);
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void texturesLoad(PackStream* stream, TexturesDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
TextureLayerDefinition* layer;
|
2012-01-18 18:47:46 +00:00
|
|
|
int i, n;
|
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
while (definition->nblayers > 0)
|
2012-01-18 18:47:46 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
texturesDeleteLayer(definition, 0);
|
2012-01-18 18:47:46 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
packReadInt(stream, &n);
|
2012-01-18 18:47:46 +00:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
layer = definition->layers + texturesAddLayer(definition);
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-07-05 17:05:03 +00:00
|
|
|
packReadString(stream, layer->name, TEXTURES_MAX_NAME_LENGTH);
|
2012-04-22 17:12:39 +00:00
|
|
|
zoneLoad(stream, layer->zone);
|
|
|
|
noiseLoadGenerator(stream, layer->bump_noise);
|
2012-06-17 09:40:40 +00:00
|
|
|
packReadDouble(stream, &layer->bump_height);
|
|
|
|
packReadDouble(stream, &layer->bump_scaling);
|
2012-04-22 17:12:39 +00:00
|
|
|
materialLoad(stream, &layer->material);
|
2012-07-05 17:55:25 +00:00
|
|
|
packReadDouble(stream, &layer->thickness);
|
|
|
|
packReadDouble(stream, &layer->slope_range);
|
|
|
|
packReadDouble(stream, &layer->thickness_transparency);
|
2012-01-18 18:47:46 +00:00
|
|
|
}
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
texturesValidateDefinition(definition);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
TexturesDefinition texturesCreateDefinition()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
TexturesDefinition result;
|
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
result.nblayers = 0;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
return result;
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesDeleteDefinition(TexturesDefinition* definition)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
while (definition->nblayers > 0)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
texturesDeleteLayer(definition, 0);
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesCopyDefinition(TexturesDefinition* source, TexturesDefinition* destination)
|
|
|
|
{
|
|
|
|
TextureLayerDefinition* layer;
|
|
|
|
int i;
|
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
while (destination->nblayers > 0)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
|
|
|
texturesDeleteLayer(destination, 0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-07-05 15:01:58 +00:00
|
|
|
for (i = 0; i < source->nblayers; i++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
layer = texturesGetLayer(destination, texturesAddLayer(destination));
|
2012-07-05 15:01:58 +00:00
|
|
|
texturesLayerCopyDefinition(source->layers + i, layer);
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesValidateDefinition(TexturesDefinition* definition)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
int i;
|
2012-07-05 15:01:58 +00:00
|
|
|
for (i = 0; i < definition->nblayers; i++)
|
2012-01-18 18:47:46 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
texturesLayerValidateDefinition(definition->layers + i);
|
2012-01-18 18:47:46 +00:00
|
|
|
}
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
TextureLayerDefinition texturesLayerCreateDefinition()
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
TextureLayerDefinition result;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-07-05 17:05:03 +00:00
|
|
|
texturesLayerSetName(&result, "Unnamed");
|
2012-01-03 15:40:50 +00:00
|
|
|
result.zone = zoneCreate();
|
|
|
|
result.bump_noise = noiseCreateGenerator();
|
2012-04-27 20:52:05 +00:00
|
|
|
noiseGenerateBaseNoise(result.bump_noise, 102400);
|
|
|
|
noiseAddLevelsSimple(result.bump_noise, 8, 1.0, 1.0);
|
|
|
|
result.bump_height = 0.1;
|
|
|
|
result.bump_scaling = 0.1;
|
|
|
|
result.material.base = COLOR_WHITE;
|
2012-04-03 19:33:40 +00:00
|
|
|
result.material.reflection = 0.0;
|
|
|
|
result.material.shininess = 0.0;
|
2012-05-05 22:07:02 +00:00
|
|
|
result.thickness = 0.0;
|
|
|
|
result.slope_range = 0.001;
|
2012-05-06 08:49:12 +00:00
|
|
|
result.thickness_transparency = 0.0;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
return result;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesLayerDeleteDefinition(TextureLayerDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-24 13:16:20 +00:00
|
|
|
zoneDelete(definition->zone);
|
|
|
|
noiseDeleteGenerator(definition->bump_noise);
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesLayerCopyDefinition(TextureLayerDefinition* source, TextureLayerDefinition* destination)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 17:05:03 +00:00
|
|
|
strncpy(destination->name, source->name, TEXTURES_MAX_NAME_LENGTH);
|
2012-04-03 19:33:40 +00:00
|
|
|
destination->material = source->material;
|
|
|
|
destination->bump_height = source->bump_height;
|
|
|
|
destination->bump_scaling = source->bump_scaling;
|
2012-05-05 22:07:02 +00:00
|
|
|
destination->thickness = source->thickness;
|
|
|
|
destination->slope_range = source->slope_range;
|
2012-05-06 08:49:12 +00:00
|
|
|
destination->thickness_transparency = source->thickness_transparency;
|
2012-01-24 13:16:20 +00:00
|
|
|
noiseCopy(source->bump_noise, destination->bump_noise);
|
|
|
|
zoneCopy(source->zone, destination->zone);
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesLayerValidateDefinition(TextureLayerDefinition* definition)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 17:05:03 +00:00
|
|
|
definition->name[TEXTURES_MAX_NAME_LENGTH] = '\0';
|
2012-04-27 20:52:05 +00:00
|
|
|
if (definition->bump_scaling < 0.000001)
|
|
|
|
{
|
|
|
|
definition->bump_scaling = 0.000001;
|
|
|
|
}
|
2012-05-05 22:07:02 +00:00
|
|
|
if (definition->slope_range < 0.001)
|
|
|
|
{
|
|
|
|
definition->slope_range = 0.001;
|
|
|
|
}
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-07-05 17:05:03 +00:00
|
|
|
void texturesLayerSetName(TextureLayerDefinition* definition, const char* name)
|
|
|
|
{
|
|
|
|
strncpy(definition->name, name, TEXTURES_MAX_NAME_LENGTH);
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
int texturesGetLayerCount(TexturesDefinition* definition)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
return definition->nblayers;
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
TextureLayerDefinition* texturesGetLayer(TexturesDefinition* definition, int layer)
|
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
if (layer >= 0 && layer < definition->nblayers)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
return definition->layers + layer;
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return &_NULL_LAYER;
|
|
|
|
}
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
int texturesAddLayer(TexturesDefinition* definition)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
if (definition->nblayers < TEXTURES_MAX_LAYERS)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
definition->layers[definition->nblayers] = texturesLayerCreateDefinition();
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
return definition->nblayers++;
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void texturesDeleteLayer(TexturesDefinition* definition, int layer)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
if (layer >= 0 && layer < definition->nblayers)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
texturesLayerDeleteDefinition(definition->layers + layer);
|
|
|
|
if (definition->nblayers > 1 && layer < definition->nblayers - 1)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
memmove(definition->layers + layer, definition->layers + layer + 1, sizeof(TextureLayerDefinition) * (definition->nblayers - layer - 1));
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
2012-07-05 15:01:58 +00:00
|
|
|
definition->nblayers--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void texturesMoveLayer(TexturesDefinition* definition, int layer, int new_position)
|
|
|
|
{
|
|
|
|
if (layer >= 0 && layer < definition->nblayers && new_position != layer && new_position >= 0 && new_position < definition->nblayers)
|
|
|
|
{
|
|
|
|
TextureLayerDefinition temp;
|
|
|
|
temp = definition->layers[layer];
|
|
|
|
if (new_position > layer)
|
|
|
|
{
|
|
|
|
memmove(definition->layers + layer, definition->layers + layer + 1, sizeof(TextureLayerDefinition) * (new_position - layer));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-05 15:50:46 +00:00
|
|
|
memmove(definition->layers + new_position + 1, definition->layers + new_position, sizeof(TextureLayerDefinition) * (layer - new_position));
|
2012-07-05 15:01:58 +00:00
|
|
|
}
|
|
|
|
definition->layers[new_position] = temp;
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
static inline Vector3 _getNormal4(Vector3 center, Vector3 north, Vector3 east, Vector3 south, Vector3 west)
|
2012-04-30 19:04:39 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
Vector3 dnorth, deast, dsouth, dwest, normal;
|
|
|
|
|
|
|
|
dnorth = v3Sub(north, center);
|
|
|
|
deast = v3Sub(east, center);
|
|
|
|
dsouth = v3Sub(south, center);
|
|
|
|
dwest = v3Sub(west, center);
|
2012-04-30 19:04:39 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
normal = v3Cross(deast, dnorth);
|
|
|
|
normal = v3Add(normal, v3Cross(dsouth, deast));
|
|
|
|
normal = v3Add(normal, v3Cross(dwest, dsouth));
|
|
|
|
normal = v3Add(normal, v3Cross(dnorth, dwest));
|
2012-04-30 19:04:39 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
return v3Normalize(normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vector3 _getNormal2(Vector3 center, Vector3 east, Vector3 south)
|
|
|
|
{
|
|
|
|
return v3Normalize(v3Cross(v3Sub(south, center), v3Sub(east, center)));
|
2012-04-30 19:04:39 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline TextureResult _getTerrainResult(Renderer* renderer, double x, double z, double detail)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
TextureResult result;
|
|
|
|
Vector3 center, north, east, south, west;
|
2012-04-30 19:04:39 +00:00
|
|
|
|
|
|
|
/* TODO This method is better suited in terrain.c */
|
2012-01-03 15:40:50 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
center.x = x;
|
|
|
|
center.z = z;
|
|
|
|
center.y = renderer->getTerrainHeight(renderer, center.x, center.z);
|
2012-01-03 15:40:50 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
east.x = x + detail;
|
|
|
|
east.z = z;
|
|
|
|
east.y = renderer->getTerrainHeight(renderer, east.x, east.z);
|
2012-01-03 15:40:50 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
south.x = x;
|
|
|
|
south.z = z + detail;
|
|
|
|
south.y = renderer->getTerrainHeight(renderer, south.x, south.z);
|
2012-01-03 15:40:50 +00:00
|
|
|
|
2012-04-30 19:04:39 +00:00
|
|
|
if (renderer->render_quality > 5)
|
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
west.x = x - detail;
|
|
|
|
west.z = z;
|
|
|
|
west.y = renderer->getTerrainHeight(renderer, west.x, west.z);
|
|
|
|
|
|
|
|
north.x = x;
|
|
|
|
north.z = z - detail;
|
|
|
|
north.y = renderer->getTerrainHeight(renderer, north.x, north.z);
|
|
|
|
|
|
|
|
result.normal = _getNormal4(center, north, east, south, west);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.normal = _getNormal2(center, east, south);
|
2012-04-30 19:04:39 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
result.location = center;
|
|
|
|
result.thickness = -100.0;
|
2012-05-07 08:56:22 +00:00
|
|
|
result.definition = NULL;
|
2012-05-05 22:07:02 +00:00
|
|
|
|
|
|
|
return result;
|
2012-04-30 19:04:39 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline void _getLayerThickness(TextureLayerDefinition* definition, Renderer* renderer, double x, double z, TextureResult* result)
|
2012-04-30 19:04:39 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
TextureResult base;
|
2012-06-17 09:40:40 +00:00
|
|
|
double coverage;
|
2012-05-05 22:07:02 +00:00
|
|
|
|
|
|
|
base = _getTerrainResult(renderer, x, z, definition->slope_range);
|
|
|
|
coverage = zoneGetValue(definition->zone, base.location, base.normal);
|
|
|
|
if (coverage > 0.0)
|
|
|
|
{
|
|
|
|
result->thickness = coverage * definition->thickness;
|
|
|
|
result->thickness += noiseGet2DTotal(definition->bump_noise, base.location.x / definition->bump_scaling, base.location.z / definition->bump_scaling) * definition->bump_height;
|
|
|
|
|
|
|
|
result->location = v3Add(base.location, v3Scale(base.normal, result->thickness));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->thickness = -1000.0;
|
|
|
|
result->location = base.location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline TextureResult _getLayerResult(TextureLayerDefinition* definition, Renderer* renderer, double x, double z, double detail)
|
2012-05-05 22:07:02 +00:00
|
|
|
{
|
|
|
|
TextureResult result_center, result_north, result_east, result_south, result_west;
|
2012-04-30 19:04:39 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
_getLayerThickness(definition, renderer, x, z, &result_center);
|
|
|
|
_getLayerThickness(definition, renderer, x + detail, z, &result_east);
|
|
|
|
_getLayerThickness(definition, renderer, x, z + detail, &result_south);
|
2012-04-30 19:04:39 +00:00
|
|
|
|
|
|
|
if (renderer->render_quality > 5)
|
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
_getLayerThickness(definition, renderer, x - detail, z, &result_west);
|
|
|
|
_getLayerThickness(definition, renderer, x, z - detail, &result_north);
|
|
|
|
|
|
|
|
result_center.normal = _getNormal4(result_center.location, result_north.location, result_east.location, result_south.location, result_west.location);
|
2012-04-30 19:04:39 +00:00
|
|
|
}
|
2012-05-05 22:07:02 +00:00
|
|
|
else
|
2012-04-30 19:04:39 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
result_center.normal = _getNormal2(result_center.location, result_east.location, result_south.location);
|
2012-04-30 19:04:39 +00:00
|
|
|
}
|
2012-05-05 22:07:02 +00:00
|
|
|
|
2012-05-07 08:56:22 +00:00
|
|
|
result_center.definition = definition;
|
2012-05-05 22:07:02 +00:00
|
|
|
|
|
|
|
return result_center;
|
|
|
|
}
|
2012-04-30 19:04:39 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
static int _cmpResults(const void* result1, const void* result2)
|
|
|
|
{
|
|
|
|
return ((TextureResult*)result1)->thickness > ((TextureResult*)result2)->thickness;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double texturesGetLayerCoverage(TextureLayerDefinition* definition, Renderer* renderer, Vector3 location, double detail)
|
2012-04-05 17:47:43 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
TextureResult base = _getTerrainResult(renderer, location.x, location.z, definition->slope_range);
|
|
|
|
return zoneGetValue(definition->zone, base.location, base.normal);
|
2012-04-05 17:47:43 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 19:06:16 +00:00
|
|
|
static inline Color _getLayerColor(Renderer* renderer, TextureResult result, LightStatus* light)
|
2012-05-07 08:56:22 +00:00
|
|
|
{
|
2012-06-08 19:06:16 +00:00
|
|
|
return renderer->applyLightStatus(renderer, light, result.location, result.normal, result.definition->material);
|
2012-05-07 08:56:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
Color texturesGetLayerColor(TextureLayerDefinition* definition, Renderer* renderer, Vector3 location, double detail)
|
2012-05-06 10:13:34 +00:00
|
|
|
{
|
2012-06-08 19:06:16 +00:00
|
|
|
LightStatus light;
|
|
|
|
TextureResult result = _getLayerResult(definition, renderer, location.x, location.z, detail);
|
|
|
|
renderer->getLightStatus(renderer, &light, result.location);
|
|
|
|
return _getLayerColor(renderer, result, &light);
|
2012-05-06 10:13:34 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
Color texturesGetColor(TexturesDefinition* definition, Renderer* renderer, double x, double z, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
TextureResult results[TEXTURES_MAX_LAYERS + 1];
|
2012-05-07 08:56:22 +00:00
|
|
|
Color result, color;
|
2012-06-17 09:40:40 +00:00
|
|
|
double thickness, last_height;
|
2012-05-07 08:56:22 +00:00
|
|
|
int i, start;
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-05-05 22:07:02 +00:00
|
|
|
detail *= 0.1;
|
|
|
|
|
|
|
|
results[0] = _getTerrainResult(renderer, x, z, detail);
|
2012-01-03 15:40:50 +00:00
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
for (i = 0; i < definition->nblayers; i++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-07-05 15:01:58 +00:00
|
|
|
results[i + 1] = _getLayerResult(definition->layers + i, renderer, x, z, detail);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-05-05 22:07:02 +00:00
|
|
|
|
2012-07-05 15:01:58 +00:00
|
|
|
qsort(results, definition->nblayers + 1, sizeof(TextureResult), _cmpResults);
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-05-07 08:56:22 +00:00
|
|
|
/* Pre compute alpha channel */
|
|
|
|
start = 0;
|
2012-05-06 08:49:12 +00:00
|
|
|
last_height = results[0].thickness;
|
2012-07-10 19:39:12 +00:00
|
|
|
results[0].thickness = 1.0;
|
2012-07-05 15:01:58 +00:00
|
|
|
for (i = 1; i <= definition->nblayers; i++)
|
2012-01-03 15:40:50 +00:00
|
|
|
{
|
2012-05-06 08:49:12 +00:00
|
|
|
thickness = results[i].thickness - last_height;
|
|
|
|
last_height = results[i].thickness;
|
|
|
|
|
2012-05-07 08:56:22 +00:00
|
|
|
if (results[i].definition)
|
|
|
|
{
|
|
|
|
if (thickness < results[i].definition->thickness_transparency)
|
|
|
|
{
|
|
|
|
results[i].thickness = thickness / results[i].definition->thickness_transparency;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
results[i].thickness = (thickness > 0.0) ? 1.0 : 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
results[i].thickness = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (results[i].thickness >= 0.999999)
|
2012-05-06 08:49:12 +00:00
|
|
|
{
|
2012-05-07 08:56:22 +00:00
|
|
|
start = i;
|
2012-05-06 08:49:12 +00:00
|
|
|
}
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2012-01-18 18:47:46 +00:00
|
|
|
|
2012-05-07 08:56:22 +00:00
|
|
|
/* Apply colors and alphas */
|
2012-06-08 19:06:16 +00:00
|
|
|
LightStatus light;
|
|
|
|
renderer->getLightStatus(renderer, &light, results[start].location);
|
2012-05-07 08:56:22 +00:00
|
|
|
if (results[start].definition)
|
|
|
|
{
|
2012-06-08 19:06:16 +00:00
|
|
|
result = _getLayerColor(renderer, results[start], &light);
|
2012-05-07 08:56:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = COLOR_GREEN;
|
|
|
|
}
|
2012-07-10 19:39:12 +00:00
|
|
|
for (i = start + 1; i <= definition->nblayers; i++)
|
2012-05-07 08:56:22 +00:00
|
|
|
{
|
|
|
|
if (results[i].thickness)
|
|
|
|
{
|
|
|
|
if (results[i].definition)
|
|
|
|
{
|
2012-06-08 19:06:16 +00:00
|
|
|
color = _getLayerColor(renderer, results[i], &light);
|
2012-05-07 08:56:22 +00:00
|
|
|
color.a = results[i].thickness;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
color = COLOR_GREEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
colorMask(&result, &color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
return result;
|
|
|
|
}
|