From 47de5680043f0cc6420cdffb46c867aa7470efc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 9 Jun 2013 23:03:16 +0200 Subject: [PATCH 1/6] Added scaling adjustements This is not enough, there are still problems with painting brush offsets. --- src/editing/terrain/paintingbrush.cpp | 8 ++++---- src/editing/terrain/widgetheightmap.cpp | 8 ++++---- src/rendering/terrain/public.h | 2 +- src/rendering/terrain/ter_definition.c | 13 ++++++++++--- src/rendering/terrain/ter_painting.c | 18 +++++++++--------- src/rendering/terrain/ter_render.c | 2 +- 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/editing/terrain/paintingbrush.cpp b/src/editing/terrain/paintingbrush.cpp index 8f18651..de1cf40 100644 --- a/src/editing/terrain/paintingbrush.cpp +++ b/src/editing/terrain/paintingbrush.cpp @@ -123,7 +123,7 @@ void PaintingBrush::applyToTerrain(TerrainDefinition* terrain, double x, double case PAINTING_BRUSH_SMOOTH: if (reverse) { - terrainBrushSmooth(terrain->height_map, &brush, brush_strength); + terrainBrushSmooth(terrain->height_map, &brush, brush_strength * 30.0); } else { @@ -133,15 +133,15 @@ void PaintingBrush::applyToTerrain(TerrainDefinition* terrain, double x, double case PAINTING_BRUSH_FLATTEN: if (reverse) { - _height = terrainGetInterpolatedHeight(terrain, x, z, 1); + _height = terrainGetInterpolatedHeight(terrain, x, z, 0, 1); } else { - terrainBrushFlatten(terrain->height_map, &brush, _height, brush_strength); + terrainBrushFlatten(terrain->height_map, &brush, _height, brush_strength * 30.0); } break; case PAINTING_BRUSH_RESTORE: - terrainBrushReset(terrain->height_map, &brush, brush_strength); + terrainBrushReset(terrain->height_map, &brush, brush_strength * 30.0); break; default: return; diff --git a/src/editing/terrain/widgetheightmap.cpp b/src/editing/terrain/widgetheightmap.cpp index f6cde1e..299d7b4 100644 --- a/src/editing/terrain/widgetheightmap.cpp +++ b/src/editing/terrain/widgetheightmap.cpp @@ -29,7 +29,7 @@ QGLWidget(parent) _wireframe = true; WaterDefinition* water_definition = (WaterDefinition*)WaterDefinitionClass.create(); sceneryGetWater(water_definition); - _water_height = _renderer->terrain->getWaterHeight(_renderer); + _water_height = 0.0; WaterDefinitionClass.destroy(water_definition); _average_frame_time = 0.0; @@ -74,7 +74,7 @@ void WidgetHeightMap::setTerrain(TerrainDefinition* terrain) { _terrain = terrain; TerrainRendererClass.bind(_renderer, _terrain); - _water_height = _renderer->terrain->getWaterHeight(_renderer); + _water_height = _renderer->terrain->getWaterHeight(_renderer) / _terrain->scaling; revert(); } @@ -210,7 +210,7 @@ void WidgetHeightMap::timerEvent(QTimerEvent*) _last_time = new_time; // Update top camera - Vector3 target = {_target_x, terrainGetInterpolatedHeight(_terrain, _target_x, _target_z, 1), _target_z}; + Vector3 target = {_target_x, terrainGetInterpolatedHeight(_terrain, _target_x, _target_z, 1, 1), _target_z}; cameraSetLocationCoords(_top_camera, target.x, target.y + 1.0, target.z + 0.1); cameraSetTarget(_top_camera, target); cameraSetZoomToTarget(_top_camera, _zoom); @@ -531,7 +531,7 @@ void WidgetHeightMap::updateVertexInfo() vertex->point.x = (double) dx; vertex->point.z = (double) dz; - vertex->point.y = terrainGetGridHeight(_terrain, dx, dz, 1); + vertex->point.y = terrainGetGridHeight(_terrain, dx, dz, 1) * _terrain->height; vertex->painted = terrainIsPainted(_terrain->height_map, dx, dz); } diff --git a/src/rendering/terrain/public.h b/src/rendering/terrain/public.h index 905e626..dddf26f 100644 --- a/src/rendering/terrain/public.h +++ b/src/rendering/terrain/public.h @@ -64,7 +64,7 @@ extern StandardRenderer TerrainRendererClass; void terrainAutoPreset(TerrainDefinition* definition, TerrainPreset preset); void terrainRenderSurface(Renderer* renderer); double terrainGetGridHeight(TerrainDefinition* definition, int x, int z, int with_painting); -double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, double z, int with_painting); +double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, double z, int scaled, int with_painting); size_t terrainGetMemoryStats(TerrainDefinition* definition); Renderer* terrainCreatePreviewRenderer(); diff --git a/src/rendering/terrain/ter_definition.c b/src/rendering/terrain/ter_definition.c index be387c2..d61ef46 100644 --- a/src/rendering/terrain/ter_definition.c +++ b/src/rendering/terrain/ter_definition.c @@ -105,10 +105,10 @@ double terrainGetGridHeight(TerrainDefinition* definition, int x, int z, int wit height = noiseGet2DTotal(definition->_height_noise, (double)x, (double)z); } - return height * definition->height * definition->scaling; + return height; } -double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, double z, int with_painting) +double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, double z, int scaled, int with_painting) { double height; x /= definition->scaling; @@ -119,5 +119,12 @@ double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, dou height = noiseGet2DTotal(definition->_height_noise, x, z); } - return height * definition->height * definition->scaling; + if (scaled) + { + return height * definition->height * definition->scaling; + } + else + { + return height; + } } diff --git a/src/rendering/terrain/ter_painting.c b/src/rendering/terrain/ter_painting.c index b94ab59..fd90ddf 100644 --- a/src/rendering/terrain/ter_painting.c +++ b/src/rendering/terrain/ter_painting.c @@ -307,12 +307,12 @@ static double* _getDataPointer(HeightMapData* data, int x, int z, HeightMapData* } else if (terrain) { - *pixel = terrainGetGridHeight(terrain, x, z, 0) / (terrain->height * terrain->scaling); + *pixel = terrainGetGridHeight(terrain, x, z, 0); } } else if (terrain) { - *pixel = terrainGetGridHeight(terrain, x, z, 0) / (terrain->height * terrain->scaling); + *pixel = terrainGetGridHeight(terrain, x, z, 0); } } return pixel; @@ -411,7 +411,7 @@ int terrainHeightmapGetInterpolatedHeight(TerrainHeightMap* heightmap, double x, { if (!terrainHeightmapGetGridHeight(heightmap, ix, iz, &value)) { - value = terrainGetGridHeight(heightmap->terrain, ix, iz, 0) / (heightmap->terrain->scaling * heightmap->terrain->height); + value = terrainGetGridHeight(heightmap->terrain, ix, iz, 0); } stencil[(iz - (zlow - 1)) * 4 + ix - (xlow - 1)] = value; } @@ -468,7 +468,7 @@ static inline void _applyBrush(TerrainHeightMap* heightmap, TerrainBrush* brush, int x, z; double dx, dz, distance, influence; - force /= (heightmap->terrain->height * heightmap->terrain->scaling); + force /= heightmap->terrain->height; for (x = brush_rect.xstart; x <= brush_rect.xend; x++) { @@ -536,10 +536,10 @@ static double _applyBrushSmooth(TerrainHeightMap* heightmap, TerrainBrush* brush UNUSED(data); double ideal, factor; - ideal = terrainGetInterpolatedHeight(heightmap->terrain, x + brush->total_radius * 0.5, z, 1); - ideal += terrainGetInterpolatedHeight(heightmap->terrain, x - brush->total_radius * 0.5, z, 1); - ideal += terrainGetInterpolatedHeight(heightmap->terrain, x, z - brush->total_radius * 0.5, 1); - ideal += terrainGetInterpolatedHeight(heightmap->terrain, x, z + brush->total_radius * 0.5, 1); + ideal = terrainGetInterpolatedHeight(heightmap->terrain, x + brush->total_radius * 0.5, z, 0, 1); + ideal += terrainGetInterpolatedHeight(heightmap->terrain, x - brush->total_radius * 0.5, z, 0, 1); + ideal += terrainGetInterpolatedHeight(heightmap->terrain, x, z - brush->total_radius * 0.5, 0, 1); + ideal += terrainGetInterpolatedHeight(heightmap->terrain, x, z + brush->total_radius * 0.5, 0, 1); ideal /= 4.0; factor = influence * force; if (factor > 1.0) @@ -571,7 +571,7 @@ static double _applyBrushReset(TerrainHeightMap* heightmap, TerrainBrush* brush, UNUSED(brush); UNUSED(data); - double ideal = terrainGetInterpolatedHeight(heightmap->terrain, x, z, 0); + double ideal = terrainGetInterpolatedHeight(heightmap->terrain, x, z, 0, 0); return basevalue + (ideal - basevalue) * influence * force; } diff --git a/src/rendering/terrain/ter_render.c b/src/rendering/terrain/ter_render.c index c6d15a0..0a1e72e 100644 --- a/src/rendering/terrain/ter_render.c +++ b/src/rendering/terrain/ter_render.c @@ -18,7 +18,7 @@ static double _fakeGetHeight(Renderer* renderer, double x, double z, int with_pa static double _realGetHeight(Renderer* renderer, double x, double z, int with_painting) { - return terrainGetInterpolatedHeight(renderer->terrain->definition, x, z, with_painting); + return terrainGetInterpolatedHeight(renderer->terrain->definition, x, z, 1, with_painting); } static TerrainResult _fakeGetResult(Renderer* renderer, double x, double z, int with_painting, int with_textures) From a4ea010b86ad4c4a5b388ffd9ee9fdbc6bb43369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 9 Jun 2013 23:03:37 +0200 Subject: [PATCH 2/6] Added unit testing for terrain grid --- src/rendering/noise.c | 7 +++ src/rendering/noise.h | 1 + src/testing/main.c | 2 + src/testing/test_terrain_painting.c | 95 +++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 src/testing/test_terrain_painting.c diff --git a/src/rendering/noise.c b/src/rendering/noise.c index 60b652f..0baa57e 100644 --- a/src/rendering/noise.c +++ b/src/rendering/noise.c @@ -229,6 +229,13 @@ void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function) noiseValidate(generator); } +void noiseSetCustomFunction(NoiseGenerator* generator, double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z)) +{ + generator->_func_noise_1d = func1d; + generator->_func_noise_2d = func2d; + generator->_func_noise_3d = func3d; +} + void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor) { NoiseFunction function = {algorithm, ridge_factor, curve_factor}; diff --git a/src/rendering/noise.h b/src/rendering/noise.h index 5a30114..a8dea1a 100644 --- a/src/rendering/noise.h +++ b/src/rendering/noise.h @@ -46,6 +46,7 @@ void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination); void noiseValidate(NoiseGenerator* generator); void noiseRandomizeOffsets(NoiseGenerator* generator); NoiseFunction noiseGetFunction(NoiseGenerator* generator); +void noiseSetCustomFunction(NoiseGenerator* generator, double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z)); void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function); void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor); void noiseForceValue(NoiseGenerator* generator, double value); diff --git a/src/testing/main.c b/src/testing/main.c index 78b74d9..10ca434 100644 --- a/src/testing/main.c +++ b/src/testing/main.c @@ -7,6 +7,7 @@ extern void test_euclid_case(Suite* s); extern void test_camera_case(Suite* s); extern void test_clouds_case(Suite* s); extern void test_noise_case(Suite* s); +extern void test_terrain_painting_case(Suite* s); int main(int argc, char** argv) { @@ -20,6 +21,7 @@ int main(int argc, char** argv) test_camera_case(s); test_clouds_case(s); test_noise_case(s); + test_terrain_painting_case(s); SRunner *sr = srunner_create(s); srunner_run_all(sr, CK_NORMAL); diff --git a/src/testing/test_terrain_painting.c b/src/testing/test_terrain_painting.c new file mode 100644 index 0000000..c7750a7 --- /dev/null +++ b/src/testing/test_terrain_painting.c @@ -0,0 +1,95 @@ +#include "testing/common.h" + +#include +#include "rendering/tools.h" +#include "rendering/terrain/public.h" + +/* Noise sin period is defined at 20.0 */ +#define X_FACTOR (M_PI / 10.0) + +static double _noise1dMock(double x) +{ + return sin(x * X_FACTOR) * 0.5 + 0.5; +} + +static double _noise2dMock(double x, double y) +{ + UNUSED(y); + return sin(x * X_FACTOR) * 0.5 + 0.5; +} + +static double _noise3dMock(double x, double y, double z) +{ + UNUSED(y); + UNUSED(z); + return sin(x * X_FACTOR) * 0.5 + 0.5; +} + +static TerrainDefinition* _setUpDefinition() +{ + TerrainDefinition* terrain = (TerrainDefinition*)TerrainDefinitionClass.create(); + terrain->height = 3.0; + terrain->scaling = 1.0; + noiseClearLevels(terrain->_height_noise); + NoiseLevel level = {1.0, 2.0, -1.0, 0.0, 0.0, 0.0}; + noiseAddLevel(terrain->_height_noise, level, 0); + noiseSetCustomFunction(terrain->_height_noise, _noise1dMock, _noise2dMock, _noise3dMock); + return terrain; +} + +static void _tearDownDefinition(TerrainDefinition* terrain) +{ + TerrainDefinitionClass.destroy(terrain); +} + +START_TEST(test_terrain_painting_grid) +{ + /* Set up */ + TerrainDefinition* terrain = _setUpDefinition(); + + /* Test base grid */ + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 0), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 1, 0), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 0), sin(1.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 0), sin(2.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 0), sin(3.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 0), sin(4.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 0), 1.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 6, 0, 0), sin(4.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 0), -sin(1.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 10, 0, 0), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 15, 0, 0), -1.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 20, 0, 0), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 0), -1.0); + + /* Test interpolated result */ + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0.0, 0.0, 0, 0), 0.0); + ck_assert_double_in_range(terrainGetInterpolatedHeight(terrain, 0.5, 0.0, 0, 0), 0.1564, 0.1566); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1.0, 0.0, 0, 0), sin(1.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0.0, 0.0, 1, 0), 0.0); + ck_assert_double_in_range(terrainGetInterpolatedHeight(terrain, 0.5, 0.0, 1, 0), 3.0 * 0.1564, 3.0 * 0.1566); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1.0, 0.0, 1, 0), 3.0 * sin(1.0 * X_FACTOR)); + + /* Test scaling */ + terrain->scaling = 2.0; + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 0), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 0), sin(1.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 0), sin(2.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 0), sin(3.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0, 0, 0, 0), 0.0); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1, 0, 0, 0), sin(0.5 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 2, 0, 0, 0), sin(1.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 3, 0, 0, 0), sin(1.5 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 0, 0, 1, 0), 0.0); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 1, 0, 1, 0), 6.0 * sin(0.5 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 2, 0, 1, 0), 6.0 * sin(1.0 * X_FACTOR)); + ck_assert_double_eq(terrainGetInterpolatedHeight(terrain, 3, 0, 1, 0), 6.0 * sin(1.5 * X_FACTOR)); + + /* Tear down */ + _tearDownDefinition(terrain); +} +END_TEST + +TEST_CASE(terrain_painting, test_terrain_painting_grid) + From db51b3af81dbfd9ca8c92c4422b2a0bb009d3371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 16 Jun 2013 15:23:37 +0200 Subject: [PATCH 3/6] Fixed terrain paint brush 'flatten' - Now taking scaling into account - Added unit tests on the brush --- src/editing/terrain/paintingbrush.cpp | 2 +- src/testing/test_terrain_painting.c | 186 +++++++++++++++++++++++++- 2 files changed, 186 insertions(+), 2 deletions(-) diff --git a/src/editing/terrain/paintingbrush.cpp b/src/editing/terrain/paintingbrush.cpp index de1cf40..c24c396 100644 --- a/src/editing/terrain/paintingbrush.cpp +++ b/src/editing/terrain/paintingbrush.cpp @@ -133,7 +133,7 @@ void PaintingBrush::applyToTerrain(TerrainDefinition* terrain, double x, double case PAINTING_BRUSH_FLATTEN: if (reverse) { - _height = terrainGetInterpolatedHeight(terrain, x, z, 0, 1); + _height = terrainGetInterpolatedHeight(terrain, x * terrain->scaling, z * terrain->scaling, 0, 1); } else { diff --git a/src/testing/test_terrain_painting.c b/src/testing/test_terrain_painting.c index c7750a7..8ad85b5 100644 --- a/src/testing/test_terrain_painting.c +++ b/src/testing/test_terrain_painting.c @@ -91,5 +91,189 @@ START_TEST(test_terrain_painting_grid) } END_TEST -TEST_CASE(terrain_painting, test_terrain_painting_grid) +START_TEST(test_terrain_painting_brush_elevation) +{ + /* Set up */ + TerrainDefinition* terrain = _setUpDefinition(); + + /* Test */ + /* TODO */ + + /* Tear down */ + _tearDownDefinition(terrain); +} +END_TEST + +START_TEST(test_terrain_painting_brush_noise) +{ + /* Set up */ + TerrainDefinition* terrain = _setUpDefinition(); + + /* Test */ + /* TODO */ + + /* Tear down */ + _tearDownDefinition(terrain); +} + +END_TEST + +START_TEST(test_terrain_painting_brush_smooth) +{ + /* Set up */ + TerrainDefinition* terrain = _setUpDefinition(); + + /* Test */ + /* TODO */ + + /* Tear down */ + _tearDownDefinition(terrain); +} + +END_TEST + +START_TEST(test_terrain_painting_brush_flatten) +{ + /* Set up */ + TerrainDefinition* terrain = _setUpDefinition(); + TerrainBrush brush; + terrain->height = 1.0; + terrain->scaling = 1.0; + noiseForceValue(terrain->_height_noise, 0.0); + + /* Test flattening center at 0.5 */ + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + brush.relative_x = 0.0; + brush.relative_z = 0.0; + brush.hard_radius = 2.0; + brush.smoothed_size = 2.0; + brush.total_radius = 4.0; + terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.5); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.5); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.5); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.5); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.25); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + + /* Test brush strength */ + terrainClearPainting(terrain->height_map); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + brush.relative_x = 0.0; + brush.relative_z = 0.0; + brush.hard_radius = 2.0; + brush.smoothed_size = 2.0; + brush.total_radius = 4.0; + terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.005); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.005); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.005); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.005); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0025); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + + /* Test cumulative effect */ + brush.relative_x = 0.0; + brush.relative_z = 0.0; + brush.hard_radius = 2.0; + brush.smoothed_size = 2.0; + brush.total_radius = 4.0; + terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.00995); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.00995); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.00995); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.00995); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0049875); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + + /* Test with height modifier */ + terrain->height = 10.0; + terrainClearPainting(terrain->height_map); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + brush.relative_x = 0.0; + brush.relative_z = 0.0; + brush.hard_radius = 2.0; + brush.smoothed_size = 2.0; + brush.total_radius = 4.0; + terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.025); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + + /* Test with scaling modifier */ + terrain->height = 10.0; + terrain->scaling = 2.0; + terrainClearPainting(terrain->height_map); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + brush.relative_x = 0.0; + brush.relative_z = 0.0; + brush.hard_radius = 2.0; + brush.smoothed_size = 2.0; + brush.total_radius = 4.0; + terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.05); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.025); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + + /* Tear down */ + _tearDownDefinition(terrain); +} + +END_TEST + +START_TEST(test_terrain_painting_brush_reset) +{ + /* Set up */ + TerrainDefinition* terrain = _setUpDefinition(); + + /* Test */ + /* TODO */ + + /* Tear down */ + _tearDownDefinition(terrain); +} +END_TEST + +TEST_CASE(terrain_painting, + test_terrain_painting_grid, + test_terrain_painting_brush_elevation, + test_terrain_painting_brush_noise, + test_terrain_painting_brush_smooth, + test_terrain_painting_brush_flatten, + test_terrain_painting_brush_reset) From 2d866442e7f739cbda897b8d37a0d56004023448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 16 Jun 2013 15:30:56 +0200 Subject: [PATCH 4/6] Improved previous tests readability --- src/testing/test_terrain_painting.c | 118 +++++++--------------------- 1 file changed, 30 insertions(+), 88 deletions(-) diff --git a/src/testing/test_terrain_painting.c b/src/testing/test_terrain_painting.c index 8ad85b5..4366baa 100644 --- a/src/testing/test_terrain_painting.c +++ b/src/testing/test_terrain_painting.c @@ -132,123 +132,65 @@ START_TEST(test_terrain_painting_brush_smooth) END_TEST +static void _checkBrushResult(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter) +{ + UNUSED(brush); + ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 1), exter); + ck_assert_double_eq(terrainGetGridHeight(terrain, -4, 0, 1), soft); + ck_assert_double_eq(terrainGetGridHeight(terrain, -3, 0, 1), midsoft); + ck_assert_double_eq(terrainGetGridHeight(terrain, -2, 0, 1), hard); + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), midhard); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), center); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), midhard); + ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), hard); + ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), midsoft); + ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), soft); + ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), exter); +} + START_TEST(test_terrain_painting_brush_flatten) { /* Set up */ TerrainDefinition* terrain = _setUpDefinition(); TerrainBrush brush; + brush.relative_x = 0.0; + brush.relative_z = 0.0; + brush.hard_radius = 2.0; + brush.smoothed_size = 2.0; + brush.total_radius = 4.0; terrain->height = 1.0; terrain->scaling = 1.0; noiseForceValue(terrain->_height_noise, 0.0); /* Test flattening center at 0.5 */ - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); - brush.relative_x = 0.0; - brush.relative_z = 0.0; - brush.hard_radius = 2.0; - brush.smoothed_size = 2.0; - brush.total_radius = 4.0; + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.5); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.5); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.5); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.5); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.25); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + _checkBrushResult(terrain, &brush, 0.5, 0.5, 0.5, 0.25, 0.0, 0.0); /* Test brush strength */ terrainClearPainting(terrain->height_map); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); - brush.relative_x = 0.0; - brush.relative_z = 0.0; - brush.hard_radius = 2.0; - brush.smoothed_size = 2.0; - brush.total_radius = 4.0; + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.005); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.005); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.005); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.005); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0025); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + _checkBrushResult(terrain, &brush, 0.005, 0.005, 0.005, 0.0025, 0.0, 0.0); /* Test cumulative effect */ - brush.relative_x = 0.0; - brush.relative_z = 0.0; - brush.hard_radius = 2.0; - brush.smoothed_size = 2.0; - brush.total_radius = 4.0; terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.00995); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.00995); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.00995); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.00995); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0049875); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + _checkBrushResult(terrain, &brush, 0.00995, 0.00995, 0.00995, 0.0049875, 0.0, 0.0); /* Test with height modifier */ terrain->height = 10.0; terrainClearPainting(terrain->height_map); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); - brush.relative_x = 0.0; - brush.relative_z = 0.0; - brush.hard_radius = 2.0; - brush.smoothed_size = 2.0; - brush.total_radius = 4.0; + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.025); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0); /* Test with scaling modifier */ terrain->height = 10.0; terrain->scaling = 2.0; terrainClearPainting(terrain->height_map); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); - brush.relative_x = 0.0; - brush.relative_z = 0.0; - brush.hard_radius = 2.0; - brush.smoothed_size = 2.0; - brush.total_radius = 4.0; + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), 0.05); - ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), 0.025); - ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), 0.0); - ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), 0.0); + _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0); /* Tear down */ _tearDownDefinition(terrain); From 9b9ea22054c9570521420bc7d757892ae8da66ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 16 Jun 2013 16:03:17 +0200 Subject: [PATCH 5/6] Fixed terrain paint brush 'reset' - Taking scaling into account - Added unit tests --- src/rendering/terrain/ter_painting.c | 2 +- src/testing/test_terrain_painting.c | 108 +++++++++++++++++++++------ 2 files changed, 86 insertions(+), 24 deletions(-) diff --git a/src/rendering/terrain/ter_painting.c b/src/rendering/terrain/ter_painting.c index fd90ddf..1adc5ea 100644 --- a/src/rendering/terrain/ter_painting.c +++ b/src/rendering/terrain/ter_painting.c @@ -571,7 +571,7 @@ static double _applyBrushReset(TerrainHeightMap* heightmap, TerrainBrush* brush, UNUSED(brush); UNUSED(data); - double ideal = terrainGetInterpolatedHeight(heightmap->terrain, x, z, 0, 0); + double ideal = terrainGetInterpolatedHeight(heightmap->terrain, x * heightmap->terrain->scaling, z * heightmap->terrain->scaling, 0, 0); return basevalue + (ideal - basevalue) * influence * force; } diff --git a/src/testing/test_terrain_painting.c b/src/testing/test_terrain_painting.c index 4366baa..8fd2f20 100644 --- a/src/testing/test_terrain_painting.c +++ b/src/testing/test_terrain_painting.c @@ -132,65 +132,76 @@ START_TEST(test_terrain_painting_brush_smooth) END_TEST -static void _checkBrushResult(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter) +static void _checkBrushResultSides(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter, double neg_midhard, double neg_hard, double neg_midsoft, double neg_soft, double neg_exter) { UNUSED(brush); - ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 1), exter); - ck_assert_double_eq(terrainGetGridHeight(terrain, -4, 0, 1), soft); - ck_assert_double_eq(terrainGetGridHeight(terrain, -3, 0, 1), midsoft); - ck_assert_double_eq(terrainGetGridHeight(terrain, -2, 0, 1), hard); - ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), midhard); + ck_assert_double_eq(terrainGetGridHeight(terrain, 0, 0, 1), center); + ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), midhard); ck_assert_double_eq(terrainGetGridHeight(terrain, 2, 0, 1), hard); ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), midsoft); ck_assert_double_eq(terrainGetGridHeight(terrain, 4, 0, 1), soft); ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), exter); + + ck_assert_double_eq(terrainGetGridHeight(terrain, -1, 0, 1), neg_midhard); + ck_assert_double_eq(terrainGetGridHeight(terrain, -2, 0, 1), neg_hard); + ck_assert_double_eq(terrainGetGridHeight(terrain, -3, 0, 1), neg_midsoft); + ck_assert_double_eq(terrainGetGridHeight(terrain, -4, 0, 1), neg_soft); + ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 1), neg_exter); +} + + +static void _checkBrushResult(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter, int mirror) +{ + if (mirror) + { + _checkBrushResultSides(terrain, brush, center, midhard, hard, midsoft, soft, exter, -midhard, -hard, -midsoft, -soft, -exter); + } + else + { + _checkBrushResultSides(terrain, brush, center, midhard, hard, midsoft, soft, exter, midhard, hard, midsoft, soft, exter); + } } START_TEST(test_terrain_painting_brush_flatten) { /* Set up */ TerrainDefinition* terrain = _setUpDefinition(); - TerrainBrush brush; - brush.relative_x = 0.0; - brush.relative_z = 0.0; - brush.hard_radius = 2.0; - brush.smoothed_size = 2.0; - brush.total_radius = 4.0; + TerrainBrush brush = {0.0, 0.0, 2.0, 2.0, 4.0}; terrain->height = 1.0; terrain->scaling = 1.0; noiseForceValue(terrain->_height_noise, 0.0); /* Test flattening center at 0.5 */ - _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); - _checkBrushResult(terrain, &brush, 0.5, 0.5, 0.5, 0.25, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.5, 0.5, 0.5, 0.25, 0.0, 0.0, 0); /* Test brush strength */ terrainClearPainting(terrain->height_map); - _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); - _checkBrushResult(terrain, &brush, 0.005, 0.005, 0.005, 0.0025, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.005, 0.005, 0.005, 0.0025, 0.0, 0.0, 0); /* Test cumulative effect */ terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); - _checkBrushResult(terrain, &brush, 0.00995, 0.00995, 0.00995, 0.0049875, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.00995, 0.00995, 0.00995, 0.0049875, 0.0, 0.0, 0); /* Test with height modifier */ terrain->height = 10.0; terrainClearPainting(terrain->height_map); - _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); - _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0, 0); /* Test with scaling modifier */ terrain->height = 10.0; terrain->scaling = 2.0; terrainClearPainting(terrain->height_map); - _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); terrainBrushFlatten(terrain->height_map, &brush, 0.5, 1.0); - _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0); + _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0, 0); /* Tear down */ _tearDownDefinition(terrain); @@ -202,12 +213,63 @@ START_TEST(test_terrain_painting_brush_reset) { /* Set up */ TerrainDefinition* terrain = _setUpDefinition(); + TerrainBrush brush = {0.0, 0.0, 2.0, 2.0, 4.0}; + TerrainBrush brush_full = {0.0, 0.0, 4.0, 0.0, 4.0}; + terrain->height = 1.0; + terrain->scaling = 1.0; + noiseForceValue(terrain->_height_noise, 1.0); - /* Test */ - /* TODO */ + /* Test resetting at center */ + _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); + terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0); + _checkBrushResult(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 0); + terrainBrushReset(terrain->height_map, &brush, 1.0); + _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.5, 2.0, 1.0, 0); + + /* Test brush strength */ + terrainClearPainting(terrain->height_map); + _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); + terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0); + _checkBrushResult(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 0); + terrainBrushReset(terrain->height_map, &brush, 0.1); + _checkBrushResult(terrain, &brush, 1.9, 1.9, 1.9, 1.95, 2.0, 1.0, 0); + + /* Test cumulative effect */ + terrainBrushReset(terrain->height_map, &brush, 0.1); + _checkBrushResult(terrain, &brush, 1.81, 1.81, 1.81, 1.9025, 2.0, 1.0, 0); + + /* Test with height modifier */ + terrain->height = 10.0; + terrainClearPainting(terrain->height_map); + _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); + terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0); + _checkBrushResult(terrain, &brush, 1.1, 1.1, 1.1, 1.1, 1.1, 1.0, 0); + terrainBrushReset(terrain->height_map, &brush, 0.1); + _checkBrushResult(terrain, &brush, 1.099, 1.099, 1.099, 1.0995, 1.1, 1.0, 0); + + /* Test with scaling modifier */ + terrain->height = 10.0; + terrain->scaling = 2.0; + terrainClearPainting(terrain->height_map); + _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); + terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0); + _checkBrushResult(terrain, &brush, 1.1, 1.1, 1.1, 1.1, 1.1, 1.0, 0); + terrainBrushReset(terrain->height_map, &brush, 0.1); + _checkBrushResult(terrain, &brush, 1.099, 1.099, 1.099, 1.0995, 1.1, 1.0, 0); /* Tear down */ _tearDownDefinition(terrain); + + /* Test with scaling and the sinusoid setup (to test the basevalue sampling) */ + terrain = _setUpDefinition(); + terrain->height = 1.0; + terrain->scaling = 2.0; + _checkBrushResult(terrain, &brush, 0.0, 0.309016994375, 0.587785252292, 0.809016994375, 0.951056516295, 1.0, 1); + terrainBrushFlatten(terrain->height_map, &brush_full, 2.0, 1.0); + _checkBrushResultSides(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 2.0, 2.0, 2.0, -1.0); + terrainBrushReset(terrain->height_map, &brush, 1.0); + _checkBrushResultSides(terrain, &brush, 0.0, 0.309016994375, 0.587785252292, 2.0 - (2.0 - 0.809016994375) * 0.5, 2.0, 1.0, -0.309016994375, -0.587785252292, 2.0 - (2.0 + 0.809016994375) * 0.5, 2.0, -1.0); + _tearDownDefinition(terrain); } END_TEST From 4658db3cea032f555f05683b2b2b664f37c48903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 16 Jun 2013 16:06:46 +0200 Subject: [PATCH 6/6] Fixed terrain paint brush 'smooth' --- src/rendering/terrain/ter_painting.c | 8 ++--- src/testing/test_terrain_painting.c | 46 ---------------------------- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/src/rendering/terrain/ter_painting.c b/src/rendering/terrain/ter_painting.c index 1adc5ea..cefe248 100644 --- a/src/rendering/terrain/ter_painting.c +++ b/src/rendering/terrain/ter_painting.c @@ -536,10 +536,10 @@ static double _applyBrushSmooth(TerrainHeightMap* heightmap, TerrainBrush* brush UNUSED(data); double ideal, factor; - ideal = terrainGetInterpolatedHeight(heightmap->terrain, x + brush->total_radius * 0.5, z, 0, 1); - ideal += terrainGetInterpolatedHeight(heightmap->terrain, x - brush->total_radius * 0.5, z, 0, 1); - ideal += terrainGetInterpolatedHeight(heightmap->terrain, x, z - brush->total_radius * 0.5, 0, 1); - ideal += terrainGetInterpolatedHeight(heightmap->terrain, x, z + brush->total_radius * 0.5, 0, 1); + ideal = terrainGetInterpolatedHeight(heightmap->terrain, (x + brush->total_radius * 0.5) * heightmap->terrain->scaling, z * heightmap->terrain->scaling, 0, 1); + ideal += terrainGetInterpolatedHeight(heightmap->terrain, (x - brush->total_radius * 0.5) * heightmap->terrain->scaling, z * heightmap->terrain->scaling, 0, 1); + ideal += terrainGetInterpolatedHeight(heightmap->terrain, x * heightmap->terrain->scaling, (z - brush->total_radius * 0.5) * heightmap->terrain->scaling, 0, 1); + ideal += terrainGetInterpolatedHeight(heightmap->terrain, x * heightmap->terrain->scaling, (z + brush->total_radius * 0.5) * heightmap->terrain->scaling, 0, 1); ideal /= 4.0; factor = influence * force; if (factor > 1.0) diff --git a/src/testing/test_terrain_painting.c b/src/testing/test_terrain_painting.c index 8fd2f20..23cbcb7 100644 --- a/src/testing/test_terrain_painting.c +++ b/src/testing/test_terrain_painting.c @@ -91,47 +91,6 @@ START_TEST(test_terrain_painting_grid) } END_TEST -START_TEST(test_terrain_painting_brush_elevation) -{ - /* Set up */ - TerrainDefinition* terrain = _setUpDefinition(); - - /* Test */ - /* TODO */ - - /* Tear down */ - _tearDownDefinition(terrain); -} -END_TEST - -START_TEST(test_terrain_painting_brush_noise) -{ - /* Set up */ - TerrainDefinition* terrain = _setUpDefinition(); - - /* Test */ - /* TODO */ - - /* Tear down */ - _tearDownDefinition(terrain); -} - -END_TEST - -START_TEST(test_terrain_painting_brush_smooth) -{ - /* Set up */ - TerrainDefinition* terrain = _setUpDefinition(); - - /* Test */ - /* TODO */ - - /* Tear down */ - _tearDownDefinition(terrain); -} - -END_TEST - static void _checkBrushResultSides(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter, double neg_midhard, double neg_hard, double neg_midsoft, double neg_soft, double neg_exter) { UNUSED(brush); @@ -151,7 +110,6 @@ static void _checkBrushResultSides(TerrainDefinition* terrain, TerrainBrush* bru ck_assert_double_eq(terrainGetGridHeight(terrain, -5, 0, 1), neg_exter); } - static void _checkBrushResult(TerrainDefinition* terrain, TerrainBrush* brush, double center, double midhard, double hard, double midsoft, double soft, double exter, int mirror) { if (mirror) @@ -206,7 +164,6 @@ START_TEST(test_terrain_painting_brush_flatten) /* Tear down */ _tearDownDefinition(terrain); } - END_TEST START_TEST(test_terrain_painting_brush_reset) @@ -275,9 +232,6 @@ END_TEST TEST_CASE(terrain_painting, test_terrain_painting_grid, - test_terrain_painting_brush_elevation, - test_terrain_painting_brush_noise, - test_terrain_painting_brush_smooth, test_terrain_painting_brush_flatten, test_terrain_painting_brush_reset)