From 3ad79c9b7ca69749be0bb55e29ce5c892b8542fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 23 Jul 2012 16:21:33 +0000 Subject: [PATCH] paysages: Apply canvas height limits to terrain git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@396 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- lib_paysages/terrain.c | 26 +++++++++++++++++++++++--- lib_paysages/terrain.h | 1 + lib_paysages/terraincanvas.c | 5 +++++ lib_paysages/terraincanvas.h | 1 + 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib_paysages/terrain.c b/lib_paysages/terrain.c index 5485a83..58a1ca9 100644 --- a/lib_paysages/terrain.c +++ b/lib_paysages/terrain.c @@ -72,11 +72,31 @@ void terrainCopyDefinition(TerrainDefinition* source, TerrainDefinition* destina void terrainValidateDefinition(TerrainDefinition* definition) { + int i, n; + TerrainCanvas* canvas; + double min, max; + noiseValidate(definition->height_noise); layersValidate(definition->canvases); + /* Get minimal and maximal height */ + definition->_min_height = -noiseGetMaxValue(definition->height_noise) * definition->height_factor; definition->_max_height = noiseGetMaxValue(definition->height_noise) * definition->height_factor; - /* FIXME _max_height depends on canvases/modifiers */ + + n = layersCount(definition->canvases); + for (i = 0; i < n; i++) + { + canvas = layersGetLayer(definition->canvases, i); + terrainCanvasGetLimits(canvas, &min, &max); + if (min < definition->_min_height) + { + definition->_min_height = min; + } + if (max > definition->_max_height) + { + definition->_max_height = max; + } + } } static inline double _getHeight(TerrainDefinition* definition, double x, double z) @@ -309,13 +329,13 @@ double terrainGetHeight(TerrainDefinition* definition, double x, double z) double terrainGetHeightNormalized(TerrainDefinition* definition, double x, double z) { - if (definition->_max_height == 0.0) + if (definition->_max_height - definition->_min_height <= 0.0000001) { return 0.5; } else { - return 0.5 + _getHeight(definition, x, z) / (definition->_max_height * 2.0); + return _getHeight(definition, x, z) - definition->_min_height / (definition->_max_height - definition->_min_height); } } diff --git a/lib_paysages/terrain.h b/lib_paysages/terrain.h index a65f1fa..e962c7f 100644 --- a/lib_paysages/terrain.h +++ b/lib_paysages/terrain.h @@ -22,6 +22,7 @@ typedef struct Layers* canvases; double shadow_smoothing; + double _min_height; double _max_height; } TerrainDefinition; diff --git a/lib_paysages/terraincanvas.c b/lib_paysages/terraincanvas.c index 2b7611a..cab84db 100644 --- a/lib_paysages/terraincanvas.c +++ b/lib_paysages/terraincanvas.c @@ -106,6 +106,11 @@ void terrainCanvasLoad(PackStream* stream, TerrainCanvas* canvas) packReadDouble(stream, &canvas->mask.smoothing); } +double terrainCanvasGetLimits(TerrainCanvas* canvas, double* ymin, double* ymax) +{ + return heightmapGetLimits(canvas->height_map, ymin, ymax); +} + void terrainCanvasRevertToTerrain(TerrainCanvas* canvas, TerrainDefinition* terrain, int only_masked) { } diff --git a/lib_paysages/terraincanvas.h b/lib_paysages/terraincanvas.h index f180fc2..47ffdcc 100644 --- a/lib_paysages/terraincanvas.h +++ b/lib_paysages/terraincanvas.h @@ -52,6 +52,7 @@ LayerType terrainCanvasGetLayerType(); void terrainCanvasSave(PackStream* stream, TerrainCanvas* canvas); void terrainCanvasLoad(PackStream* stream, TerrainCanvas* canvas); +double terrainCanvasGetLimits(TerrainCanvas* canvas, double* ymin, double* ymax); void terrainCanvasRevertToTerrain(TerrainCanvas* canvas, TerrainDefinition* terrain, int only_masked); Vector3 terrainCanvasApply(TerrainCanvas* canvas, Vector3 location);