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()
|
|
|
|
{
|
2013-11-02 15:43:43 +00:00
|
|
|
TerrainDefinition* definition = new TerrainDefinition;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
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);
|
2013-11-02 15:43:43 +00:00
|
|
|
delete definition;
|
2012-12-09 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-11-03 12:00:31 +00:00
|
|
|
stream->write(&definition->height);
|
|
|
|
stream->write(&definition->scaling);
|
|
|
|
stream->write(&definition->shadow_smoothing);
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainHeightmapSave(stream, definition->height_map);
|
2013-11-03 12:00:31 +00:00
|
|
|
stream->write(&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)
|
|
|
|
{
|
2013-11-03 12:00:31 +00:00
|
|
|
stream->read(&definition->height);
|
|
|
|
stream->read(&definition->scaling);
|
|
|
|
stream->read(&definition->shadow_smoothing);
|
2013-01-10 15:41:14 +00:00
|
|
|
terrainHeightmapLoad(stream, definition->height_map);
|
2013-11-03 12:00:31 +00:00
|
|
|
stream->read(&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-06-09 21:03:16 +00:00
|
|
|
return height;
|
2013-01-14 12:08:38 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 21:03:16 +00:00
|
|
|
double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, double z, int scaled, int with_painting)
|
2013-02-07 16:06:26 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-09 21:03:16 +00:00
|
|
|
if (scaled)
|
|
|
|
{
|
|
|
|
return height * definition->height * definition->scaling;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return height;
|
|
|
|
}
|
2013-02-07 16:06:26 +00:00
|
|
|
}
|
2013-09-27 21:28:06 +00:00
|
|
|
|
|
|
|
HeightInfo terrainGetHeightInfo(TerrainDefinition* definition)
|
|
|
|
{
|
|
|
|
HeightInfo result;
|
|
|
|
|
|
|
|
result.min_height = definition->_min_height;
|
|
|
|
result.max_height = definition->_max_height;
|
|
|
|
/* TODO This is duplicated in ter_render.c (_realGetWaterHeight) */
|
|
|
|
result.base_height = definition->water_height * definition->height * definition->scaling;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|