2012-12-09 17:49:28 +00:00
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "../tools.h"
|
|
|
|
#include "../renderer.h"
|
|
|
|
|
|
|
|
/******************** Definition ********************/
|
|
|
|
static void _validateDefinition(TerrainDefinition* definition)
|
|
|
|
{
|
|
|
|
noiseValidate(definition->_height_noise);
|
|
|
|
|
2013-05-11 21:34:30 +00:00
|
|
|
if (definition->height < 1.0)
|
|
|
|
{
|
|
|
|
definition->height = 1.0;
|
|
|
|
}
|
|
|
|
|
2012-12-09 17:49:28 +00:00
|
|
|
/* Get minimal and maximal height */
|
2013-01-27 19:57:43 +00:00
|
|
|
noiseGetRange(definition->_height_noise, &definition->_min_height, &definition->_max_height);
|
2013-05-11 21:34:30 +00:00
|
|
|
definition->_min_height *= definition->height * definition->scaling;
|
|
|
|
definition->_max_height *= definition->height * definition->scaling;
|
|
|
|
|
|
|
|
/* TODO Alter with heightmap min/max */
|
2012-12-09 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static TerrainDefinition* _createDefinition()
|
|
|
|
{
|
|
|
|
TerrainDefinition* definition = malloc(sizeof(TerrainDefinition));
|
|
|
|
|
2013-06-09 13:11:03 +00:00
|
|
|
definition->height = 1.0;
|
2012-12-09 17:49:28 +00:00
|
|
|
definition->scaling = 1.0;
|
|
|
|
definition->shadow_smoothing = 0.0;
|
2013-01-10 15:41:14 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
definition->height_map = terrainHeightMapCreate(definition);
|
2013-01-10 15:41:14 +00:00
|
|
|
|
2013-06-09 13:11:03 +00:00
|
|
|
definition->water_height = -0.3;
|
2013-05-11 21:34:30 +00:00
|
|
|
|
2012-12-09 17:49:28 +00:00
|
|
|
definition->_height_noise = noiseCreateGenerator();
|
|
|
|
|
|
|
|
terrainAutoPreset(definition, TERRAIN_PRESET_STANDARD);
|
|
|
|
|
|
|
|
return definition;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _deleteDefinition(TerrainDefinition* definition)
|
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainHeightmapDelete(definition->height_map);
|
2012-12-09 17:49:28 +00:00
|
|
|
noiseDeleteGenerator(definition->_height_noise);
|
|
|
|
free(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _copyDefinition(TerrainDefinition* source, TerrainDefinition* destination)
|
|
|
|
{
|
|
|
|
destination->height = source->height;
|
|
|
|
destination->scaling = source->scaling;
|
|
|
|
destination->shadow_smoothing = source->shadow_smoothing;
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainHeightmapCopy(source->height_map, destination->height_map);
|
|
|
|
|
2013-05-11 21:34:30 +00:00
|
|
|
destination->water_height = source->water_height;
|
|
|
|
|
2012-12-24 11:24:27 +00:00
|
|
|
noiseCopy(source->_height_noise, destination->_height_noise);
|
|
|
|
|
2012-12-09 17:49:28 +00:00
|
|
|
_validateDefinition(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _saveDefinition(PackStream* stream, TerrainDefinition* definition)
|
|
|
|
{
|
|
|
|
packWriteDouble(stream, &definition->height);
|
|
|
|
packWriteDouble(stream, &definition->scaling);
|
|
|
|
packWriteDouble(stream, &definition->shadow_smoothing);
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainHeightmapSave(stream, definition->height_map);
|
2013-05-11 21:34:30 +00:00
|
|
|
packWriteDouble(stream, &definition->water_height);
|
2012-12-24 11:24:27 +00:00
|
|
|
noiseSaveGenerator(stream, definition->_height_noise);
|
2012-12-09 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _loadDefinition(PackStream* stream, TerrainDefinition* definition)
|
|
|
|
{
|
|
|
|
packReadDouble(stream, &definition->height);
|
|
|
|
packReadDouble(stream, &definition->scaling);
|
|
|
|
packReadDouble(stream, &definition->shadow_smoothing);
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainHeightmapLoad(stream, definition->height_map);
|
2013-05-11 21:34:30 +00:00
|
|
|
packReadDouble(stream, &definition->water_height);
|
2012-12-24 11:24:27 +00:00
|
|
|
noiseLoadGenerator(stream, definition->_height_noise);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
|
|
|
_validateDefinition(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
StandardDefinition TerrainDefinitionClass = {
|
|
|
|
(FuncObjectCreate)_createDefinition,
|
|
|
|
(FuncObjectDelete)_deleteDefinition,
|
|
|
|
(FuncObjectCopy)_copyDefinition,
|
|
|
|
(FuncObjectValidate)_validateDefinition,
|
|
|
|
(FuncObjectSave)_saveDefinition,
|
|
|
|
(FuncObjectLoad)_loadDefinition
|
|
|
|
};
|
|
|
|
|
2013-01-14 12:08:38 +00:00
|
|
|
/******************** Public tools ********************/
|
|
|
|
double terrainGetGridHeight(TerrainDefinition* definition, int x, int z, int with_painting)
|
|
|
|
{
|
|
|
|
double height;
|
|
|
|
|
2013-04-19 21:51:17 +00:00
|
|
|
if (!with_painting || !terrainHeightmapGetGridHeight(definition->height_map, x, z, &height))
|
2013-01-14 12:08:38 +00:00
|
|
|
{
|
|
|
|
height = noiseGet2DTotal(definition->_height_noise, (double)x, (double)z);
|
|
|
|
}
|
|
|
|
|
2013-05-11 21:34:30 +00:00
|
|
|
return height * definition->height * definition->scaling;
|
2013-01-14 12:08:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-07 16:06:26 +00:00
|
|
|
double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, double z, int with_painting)
|
|
|
|
{
|
|
|
|
double height;
|
|
|
|
x /= definition->scaling;
|
|
|
|
z /= definition->scaling;
|
|
|
|
|
2013-04-19 21:51:17 +00:00
|
|
|
if (!with_painting || !terrainHeightmapGetInterpolatedHeight(definition->height_map, x, z, &height))
|
2013-02-07 16:06:26 +00:00
|
|
|
{
|
|
|
|
height = noiseGet2DTotal(definition->_height_noise, x, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
return height * definition->height * definition->scaling;
|
|
|
|
}
|