2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#define MAX_MODIFIERS 50
|
|
|
|
|
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
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
static int _modifiers_count = 0;
|
|
|
|
static HeightModifier* _modifiers[MAX_MODIFIERS];
|
|
|
|
|
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);
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainSave(FILE* f)
|
|
|
|
{
|
|
|
|
noiseSave(_definition.height_noise, f);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
/* TODO Modifiers */
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainLoad(FILE* f)
|
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
noiseLoad(_definition.height_noise, f);
|
|
|
|
_max_height = noiseGetMaxValue(_definition.height_noise);
|
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;
|
|
|
|
|
|
|
|
definition.height_noise = noiseCreateGenerator();
|
|
|
|
|
|
|
|
return definition;
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
void terrainDeleteDefinition(TerrainDefinition definition)
|
|
|
|
{
|
|
|
|
noiseDeleteGenerator(definition.height_noise);
|
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-03 15:40:50 +00:00
|
|
|
noiseCopy(source.height_noise, destination->height_noise);
|
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-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-03 15:40:50 +00:00
|
|
|
/*int i;*/
|
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-03 15:40:50 +00:00
|
|
|
/*for (i = 0; i < _modifiers_count; i++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
location = modifierApply(_modifiers[i], location);
|
2012-01-03 15:40:50 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
double terrainGetShadow(Vector3 start, Vector3 direction)
|
|
|
|
{
|
|
|
|
Vector3 inc_vector;
|
|
|
|
double inc_value, inc_base, inc_factor, height, diff, light, smoothing, length, water;
|
|
|
|
|
|
|
|
direction = v3Normalize(direction);
|
|
|
|
inc_factor = (double)render_quality;
|
|
|
|
inc_base = 1.0;
|
|
|
|
inc_value = inc_base / inc_factor;
|
|
|
|
smoothing = 0.03 * inc_factor;;
|
|
|
|
|
|
|
|
water = waterGetLightFactor(start);
|
|
|
|
|
|
|
|
light = 1.0;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
light += diff / smoothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (light > 0.0 && length < 50.0 && start.y <= _max_height);
|
|
|
|
|
|
|
|
light *= water;
|
|
|
|
if (light < 0.0)
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1.0 - light;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
static Color _getColor(TerrainDefinition* definition, Vector3 point, double precision)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-03 15:40:50 +00:00
|
|
|
Color color;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-03 15:40:50 +00:00
|
|
|
/* FIXME Environment for textures should be customized */
|
|
|
|
color = texturesGetColor(point);
|
2011-12-10 13:25:22 +00:00
|
|
|
color = fogApplyToLocation(point, color);
|
|
|
|
//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-03 15:40:50 +00:00
|
|
|
*hit_color = _getColor(&_definition, 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-03 15:40:50 +00:00
|
|
|
fragment->vertex.color = _getColor(&_definition, 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-03 15:40:50 +00:00
|
|
|
return _getColor(definition, _getPoint(definition, x, z, detail), 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|