Fixed terrain paint brush 'reset'

- Taking scaling into account
- Added unit tests
This commit is contained in:
Michaël Lemaire 2013-06-16 16:03:17 +02:00
parent 2d866442e7
commit 9b9ea22054
2 changed files with 86 additions and 24 deletions

View file

@ -571,7 +571,7 @@ static double _applyBrushReset(TerrainHeightMap* heightmap, TerrainBrush* brush,
UNUSED(brush); UNUSED(brush);
UNUSED(data); 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; return basevalue + (ideal - basevalue) * influence * force;
} }

View file

@ -132,65 +132,76 @@ START_TEST(test_terrain_painting_brush_smooth)
END_TEST 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); 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, 0, 0, 1), center);
ck_assert_double_eq(terrainGetGridHeight(terrain, 1, 0, 1), midhard); 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, 2, 0, 1), hard);
ck_assert_double_eq(terrainGetGridHeight(terrain, 3, 0, 1), midsoft); 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, 4, 0, 1), soft);
ck_assert_double_eq(terrainGetGridHeight(terrain, 5, 0, 1), exter); 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) START_TEST(test_terrain_painting_brush_flatten)
{ {
/* Set up */ /* Set up */
TerrainDefinition* terrain = _setUpDefinition(); TerrainDefinition* terrain = _setUpDefinition();
TerrainBrush brush; TerrainBrush brush = {0.0, 0.0, 2.0, 2.0, 4.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;
terrain->height = 1.0; terrain->height = 1.0;
terrain->scaling = 1.0; terrain->scaling = 1.0;
noiseForceValue(terrain->_height_noise, 0.0); noiseForceValue(terrain->_height_noise, 0.0);
/* Test flattening center at 0.5 */ /* 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); 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 */ /* Test brush strength */
terrainClearPainting(terrain->height_map); 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); 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 */ /* Test cumulative effect */
terrainBrushFlatten(terrain->height_map, &brush, 0.5, 0.01); 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 */ /* Test with height modifier */
terrain->height = 10.0; terrain->height = 10.0;
terrainClearPainting(terrain->height_map); 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); 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 */ /* Test with scaling modifier */
terrain->height = 10.0; terrain->height = 10.0;
terrain->scaling = 2.0; terrain->scaling = 2.0;
terrainClearPainting(terrain->height_map); 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); 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 */ /* Tear down */
_tearDownDefinition(terrain); _tearDownDefinition(terrain);
@ -202,12 +213,63 @@ START_TEST(test_terrain_painting_brush_reset)
{ {
/* Set up */ /* Set up */
TerrainDefinition* terrain = _setUpDefinition(); 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 */ /* Test resetting at center */
/* TODO */ _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 */ /* Tear down */
_tearDownDefinition(terrain); _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 END_TEST