2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2012-01-05 11:32:14 +00:00
|
|
|
#include <string.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
#include "shared/types.h"
|
|
|
|
#include "shared/functions.h"
|
|
|
|
#include "shared/globals.h"
|
|
|
|
#include "shared/constants.h"
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
#include "textures.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
#include "water.h"
|
2012-01-03 15:40:50 +00:00
|
|
|
#include "terrain.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
static TerrainDefinition _definition;
|
|
|
|
static TerrainQuality _quality;
|
|
|
|
static TerrainEnvironment _environment;
|
2011-12-10 13:25:22 +00:00
|
|
|
static double _max_height = 1.0;
|
2012-01-03 15:40:50 +00:00
|
|
|
|
|
|
|
void terrainInit()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
_definition = terrainCreateDefinition();
|
|
|
|
_max_height = noiseGetMaxValue(_definition.height_noise);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
|
|
|
_environment.toggle_fog = 1;
|
2012-01-22 22:06:11 +00:00
|
|
|
_environment.lighting_definition = NULL;
|
|
|
|
_environment.lighting_environment = NULL;
|
2012-01-03 15:40:50 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainSave(FILE* f)
|
|
|
|
{
|
2012-01-05 11:32:14 +00:00
|
|
|
int i;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
noiseSave(_definition.height_noise, f);
|
2012-01-05 11:32:14 +00:00
|
|
|
toolsSaveInt(f, _definition.height_modifiers_count);
|
|
|
|
for (i = 0; i < _definition.height_modifiers_count; i++)
|
|
|
|
{
|
|
|
|
modifierSave(_definition.height_modifiers[i], f);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void terrainLoad(FILE* f)
|
|
|
|
{
|
2012-01-05 11:32:14 +00:00
|
|
|
int i;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
noiseLoad(_definition.height_noise, f);
|
|
|
|
_max_height = noiseGetMaxValue(_definition.height_noise);
|
2012-01-05 11:32:14 +00:00
|
|
|
|
|
|
|
for (i = 0; i < _definition.height_modifiers_count; i++)
|
|
|
|
{
|
|
|
|
modifierDelete(_definition.height_modifiers[i]);
|
|
|
|
}
|
|
|
|
_definition.height_modifiers_count = toolsLoadInt(f);
|
|
|
|
for (i = 0; i < _definition.height_modifiers_count; i++)
|
|
|
|
{
|
|
|
|
_definition.height_modifiers[i] = modifierCreate();
|
|
|
|
modifierLoad(_definition.height_modifiers[i], f);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
TerrainDefinition terrainCreateDefinition()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
TerrainDefinition definition;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
definition.height_noise = noiseCreateGenerator();
|
2012-01-05 11:32:14 +00:00
|
|
|
definition.height_modifiers_count = 0;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
return definition;
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainDeleteDefinition(TerrainDefinition definition)
|
|
|
|
{
|
2012-01-05 11:32:14 +00:00
|
|
|
int i;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
noiseDeleteGenerator(definition.height_noise);
|
2012-01-05 11:32:14 +00:00
|
|
|
for (i = 0; i < definition.height_modifiers_count; i++)
|
|
|
|
{
|
|
|
|
modifierDelete(definition.height_modifiers[i]);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainCopyDefinition(TerrainDefinition source, TerrainDefinition* destination)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-05 11:32:14 +00:00
|
|
|
int i;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
noiseCopy(source.height_noise, destination->height_noise);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-05 11:32:14 +00:00
|
|
|
for (i = 0; i < destination->height_modifiers_count; i++)
|
|
|
|
{
|
|
|
|
modifierDelete(destination->height_modifiers[i]);
|
|
|
|
}
|
|
|
|
destination->height_modifiers_count = source.height_modifiers_count;
|
|
|
|
for (i = 0; i < destination->height_modifiers_count; i++)
|
|
|
|
{
|
|
|
|
destination->height_modifiers[i] = modifierCreateCopy(source.height_modifiers[i]);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainSetDefinition(TerrainDefinition definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
terrainCopyDefinition(definition, &_definition);
|
|
|
|
_max_height = noiseGetMaxValue(_definition.height_noise);
|
|
|
|
/* FIXME _max_height depends on modifiers */
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
TerrainDefinition terrainGetDefinition()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
return _definition;
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-05 11:32:14 +00:00
|
|
|
int terrainAddModifier(TerrainDefinition* definition, HeightModifier* modifier)
|
|
|
|
{
|
|
|
|
if (definition->height_modifiers_count < MAX_HEIGHT_MODIFIER_COUNT)
|
|
|
|
{
|
|
|
|
definition->height_modifiers[definition->height_modifiers_count] = modifierCreateCopy(modifier);
|
|
|
|
return definition->height_modifiers_count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainDelModifier(TerrainDefinition* definition, int modifier_position)
|
|
|
|
{
|
|
|
|
if (modifier_position >= 0 && modifier_position < definition->height_modifiers_count)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainSetQuality(TerrainQuality quality)
|
|
|
|
{
|
|
|
|
_quality = quality;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
TerrainQuality terrainGetQuality()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
return _quality;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
static inline double _getHeight(TerrainDefinition* definition, double x, double z, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Vector3 location;
|
2012-01-05 11:32:14 +00:00
|
|
|
int i;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
location.x = x;
|
2012-01-03 15:40:50 +00:00
|
|
|
location.y = noiseGet2DDetail(definition->height_noise, x, z, detail);
|
2011-12-10 13:25:22 +00:00
|
|
|
location.z = z;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-05 11:32:14 +00:00
|
|
|
for (i = 0; i < definition->height_modifiers_count; i++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-05 11:32:14 +00:00
|
|
|
location = modifierApply(definition->height_modifiers[i], location);
|
|
|
|
}
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
return location.y;
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
static inline Vector3 _getPoint(TerrainDefinition* definition, double x, double z, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Vector3 result;
|
|
|
|
|
|
|
|
result.x = x;
|
2012-01-03 15:40:50 +00:00
|
|
|
result.y = _getHeight(definition, x, z, detail);
|
2011-12-10 13:25:22 +00:00
|
|
|
result.z = z;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
Color terrainLightFilter(Color light, Vector3 location, Vector3 light_location, Vector3 direction_to_light, void* custom_data)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Vector3 inc_vector;
|
2012-01-22 22:06:11 +00:00
|
|
|
double inc_value, inc_base, inc_factor, height, diff, light_factor, smoothing, length;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
direction_to_light = v3Normalize(direction_to_light);
|
|
|
|
inc_factor = (double)render_quality; // TODO Configurable
|
2011-12-10 13:25:22 +00:00
|
|
|
inc_base = 1.0;
|
|
|
|
inc_value = inc_base / inc_factor;
|
|
|
|
smoothing = 0.03 * inc_factor;;
|
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
light_factor = 1.0;
|
2011-12-10 13:25:22 +00:00
|
|
|
length = 0.0;
|
|
|
|
do
|
|
|
|
{
|
2012-01-22 22:06:11 +00:00
|
|
|
inc_vector = v3Scale(direction_to_light, inc_value);
|
2011-12-10 13:25:22 +00:00
|
|
|
length += v3Norm(inc_vector);
|
2012-01-22 22:06:11 +00:00
|
|
|
location = v3Add(location, inc_vector);
|
|
|
|
height = _getHeight(&_definition, location.x, location.z, inc_value);
|
|
|
|
diff = location.y - height;
|
2011-12-10 13:25:22 +00:00
|
|
|
if (diff < 0.0)
|
|
|
|
{
|
2012-01-22 22:06:11 +00:00
|
|
|
light_factor += diff / smoothing;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (diff < inc_base / inc_factor)
|
|
|
|
{
|
|
|
|
inc_value = inc_base / inc_factor;
|
|
|
|
}
|
|
|
|
else if (diff > inc_base)
|
|
|
|
{
|
|
|
|
inc_value = inc_base;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inc_value = diff;
|
|
|
|
}
|
2012-01-22 22:06:11 +00:00
|
|
|
} while (light_factor > 0.0 && length < 50.0 && location.y <= _max_height);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
if (light_factor <= 0.0)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-22 22:06:11 +00:00
|
|
|
return COLOR_BLACK;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-22 22:06:11 +00:00
|
|
|
light.r *= light_factor;
|
|
|
|
light.g *= light_factor;
|
|
|
|
light.b *= light_factor;
|
|
|
|
|
|
|
|
return light;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 18:39:42 +00:00
|
|
|
static Color _getColor(TerrainDefinition* definition, TerrainEnvironment* environment, Vector3 point, double precision)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
Color color;
|
2012-01-22 22:06:11 +00:00
|
|
|
TextureEnvironment texenv;
|
|
|
|
|
|
|
|
texenv.lighting_definition = environment->lighting_definition;
|
|
|
|
texenv.lighting_environment = environment->lighting_environment;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
color = texturesGetColorCustom(point, precision, NULL, &texenv);
|
2012-01-22 18:39:42 +00:00
|
|
|
if (environment->toggle_fog)
|
|
|
|
{
|
|
|
|
color = fogApplyToLocation(point, color);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
//color = cloudsApplySegmentResult(color, camera_location, point);
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
int terrainProjectRay(Vector3 start, Vector3 direction, Vector3* hit_point, Color* hit_color)
|
|
|
|
{
|
|
|
|
Vector3 inc_vector;
|
|
|
|
double inc_value, inc_base, inc_factor, height, diff, length;
|
|
|
|
|
|
|
|
direction = v3Normalize(direction);
|
|
|
|
inc_factor = (double)render_quality;
|
|
|
|
inc_base = 1.0;
|
|
|
|
inc_value = inc_base / inc_factor;
|
|
|
|
|
|
|
|
length = 0.0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
inc_vector = v3Scale(direction, inc_value);
|
|
|
|
length += v3Norm(inc_vector);
|
|
|
|
start = v3Add(start, inc_vector);
|
2012-01-03 15:40:50 +00:00
|
|
|
height = _getHeight(&_definition, start.x, start.z, inc_value);
|
2011-12-10 13:25:22 +00:00
|
|
|
diff = start.y - height;
|
|
|
|
if (diff < 0.0)
|
|
|
|
{
|
|
|
|
start.y = height;
|
|
|
|
*hit_point = start;
|
2012-01-22 18:39:42 +00:00
|
|
|
*hit_color = _getColor(&_definition, &_environment, start, inc_value);
|
2011-12-10 13:25:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diff < inc_base / inc_factor)
|
|
|
|
{
|
|
|
|
inc_value = inc_base / inc_factor;
|
|
|
|
}
|
|
|
|
else if (diff > inc_base)
|
|
|
|
{
|
|
|
|
inc_value = inc_base;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inc_value = diff;
|
|
|
|
}
|
|
|
|
} while (length < 50.0 && start.y <= _max_height);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _postProcessFragment(RenderFragment* fragment)
|
|
|
|
{
|
|
|
|
Vector3 point;
|
|
|
|
double precision;
|
|
|
|
|
|
|
|
point = fragment->vertex.location;
|
|
|
|
precision = renderGetPrecision(point);
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
point = _getPoint(&_definition, point.x, point.z, precision);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-22 18:39:42 +00:00
|
|
|
fragment->vertex.color = _getColor(&_definition, &_environment, point, precision);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
static Vertex _getFirstPassVertex(double x, double z, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Vertex result;
|
|
|
|
double value;
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
result.location = _getPoint(&_definition, x, z, 0.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
value = sin(x) * sin(x) * cos(z) * cos(z);
|
|
|
|
result.color.r = value;
|
|
|
|
result.color.g = value;
|
|
|
|
result.color.b = value;
|
|
|
|
result.color.a = 1.0;
|
|
|
|
result.normal.x = result.normal.y = result.normal.z = 0.0;
|
|
|
|
result.callback = _postProcessFragment;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _renderQuad(double x, double z, double size)
|
|
|
|
{
|
|
|
|
Vertex v1, v2, v3, v4;
|
|
|
|
|
|
|
|
v1 = _getFirstPassVertex(x, z, size);
|
|
|
|
v2 = _getFirstPassVertex(x, z + size, size);
|
|
|
|
v3 = _getFirstPassVertex(x + size, z + size, size);
|
|
|
|
v4 = _getFirstPassVertex(x + size, z, size);
|
|
|
|
renderPushQuad(&v1, &v2, &v3, &v4);
|
|
|
|
}
|
|
|
|
|
|
|
|
double terrainGetHeight(double x, double z)
|
|
|
|
{
|
2012-01-03 16:05:08 +00:00
|
|
|
return _getHeight(&_definition, x, z, 0.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double terrainGetHeightNormalized(double x, double z)
|
|
|
|
{
|
2012-01-03 16:05:08 +00:00
|
|
|
return 0.5 + _getHeight(&_definition, x, z, 0.0) / (_max_height * 2.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
Color terrainGetColorCustom(double x, double z, double detail, TerrainDefinition* definition, TerrainQuality* quality, TerrainEnvironment* environment)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-22 18:39:42 +00:00
|
|
|
if (!definition)
|
|
|
|
{
|
|
|
|
definition = &_definition;
|
|
|
|
}
|
|
|
|
if (!quality)
|
|
|
|
{
|
|
|
|
quality = &_quality;
|
|
|
|
}
|
|
|
|
if (!environment)
|
|
|
|
{
|
|
|
|
environment = &_environment;
|
|
|
|
}
|
|
|
|
|
2012-01-22 22:06:11 +00:00
|
|
|
Vector3 point = _getPoint(definition, x, z, detail);
|
|
|
|
return _getColor(definition, environment, point, detail);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
Color terrainGetColor(double x, double z, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
return terrainGetColorCustom(x, z, detail, &_definition, &_quality, &_environment);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void terrainRender(RenderProgressCallback callback)
|
|
|
|
{
|
|
|
|
int chunk_factor, chunk_count, i;
|
|
|
|
double cx = camera_location.x;
|
|
|
|
double cz = camera_location.z;
|
|
|
|
double radius_int, radius_ext, chunk_size;
|
|
|
|
|
|
|
|
chunk_factor = 1;
|
|
|
|
chunk_count = 2;
|
|
|
|
radius_int = 0.0;
|
2012-01-03 15:40:50 +00:00
|
|
|
radius_ext = _quality.min_chunk_size;
|
|
|
|
chunk_size = _quality.min_chunk_size;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
while (radius_ext < 1000.0)
|
|
|
|
{
|
|
|
|
if (!callback(radius_ext / 1000.0))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
for (i = 0; i < chunk_count - 1; i++)
|
|
|
|
{
|
|
|
|
_renderQuad(cx - radius_ext + chunk_size * i, cz - radius_ext, chunk_size);
|
|
|
|
_renderQuad(cx + radius_int, cz - radius_ext + chunk_size * i, chunk_size);
|
|
|
|
_renderQuad(cx + radius_int - chunk_size * i, cz + radius_int, chunk_size);
|
|
|
|
_renderQuad(cx - radius_ext, cz + radius_int - chunk_size * i, chunk_size);
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
if (chunk_count % 64 == 0 && chunk_size / radius_int < _quality.visible_chunk_size)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
chunk_count /= 2;
|
|
|
|
chunk_factor *= 2;
|
|
|
|
/* TODO Fill in gaps with triangles */
|
|
|
|
}
|
|
|
|
chunk_count += 2;
|
2012-01-03 15:40:50 +00:00
|
|
|
chunk_size = _quality.min_chunk_size * chunk_factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
radius_int = radius_ext;
|
|
|
|
radius_ext += chunk_size;
|
|
|
|
}
|
|
|
|
}
|