2012-07-10 14:28:47 +00:00
|
|
|
#include "terraincanvas.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
TerrainCanvas* terrainCanvasCreate()
|
|
|
|
{
|
|
|
|
TerrainCanvas* result = malloc(sizeof(TerrainCanvas));
|
|
|
|
|
|
|
|
result->area.bounded = 1;
|
2012-07-16 20:34:01 +00:00
|
|
|
result->area.location_x = -40.0;
|
|
|
|
result->area.location_z = -40.0;
|
|
|
|
result->area.size_x = 80.0;
|
|
|
|
result->area.size_z = 80.0;
|
2012-07-19 15:04:27 +00:00
|
|
|
result->offset_y = 0.0;
|
2012-07-13 12:23:58 +00:00
|
|
|
result->height_map = heightmapCreate();
|
2012-07-13 21:24:19 +00:00
|
|
|
heightmapChangeResolution(&result->height_map, 256, 256);
|
2012-07-10 14:28:47 +00:00
|
|
|
result->height_factor = 1.0;
|
|
|
|
result->detail_noise = noiseCreateGenerator();
|
2012-07-19 15:04:27 +00:00
|
|
|
noiseAddLevelsSimple(result->detail_noise, 6, 1.0, 1.0);
|
|
|
|
result->detail_height_factor = 0.002;
|
|
|
|
result->detail_scaling = 0.002;
|
2012-07-18 15:04:23 +00:00
|
|
|
result->mask.mode = INTEGRATIONMASK_MODE_CIRCLE;
|
|
|
|
result->mask.smoothing = 0.1;
|
2012-07-19 15:04:27 +00:00
|
|
|
|
|
|
|
/* DEBUG */
|
|
|
|
/*heightmapImportFromPicture(&result->height_map, "output/height.png");*/
|
|
|
|
|
2012-07-10 14:28:47 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainCanvasDelete(TerrainCanvas* canvas)
|
|
|
|
{
|
2012-07-13 12:23:58 +00:00
|
|
|
heightmapDelete(&canvas->height_map);
|
2012-07-10 14:28:47 +00:00
|
|
|
noiseDeleteGenerator(canvas->detail_noise);
|
|
|
|
free(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainCanvasCopy(TerrainCanvas* source, TerrainCanvas* destination)
|
|
|
|
{
|
|
|
|
destination->area = source->area;
|
2012-07-19 15:04:27 +00:00
|
|
|
destination->offset_y = source->offset_y;
|
2012-07-10 14:28:47 +00:00
|
|
|
destination->height_factor = source->height_factor;
|
2012-07-13 12:23:58 +00:00
|
|
|
heightmapCopy(&source->height_map, &destination->height_map);
|
2012-07-10 14:28:47 +00:00
|
|
|
noiseCopy(source->detail_noise, destination->detail_noise);
|
|
|
|
destination->detail_height_factor = source->detail_height_factor;
|
|
|
|
destination->detail_scaling = source->detail_scaling;
|
2012-07-18 15:04:23 +00:00
|
|
|
destination->mask = source->mask;
|
2012-07-10 14:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void terrainCanvasValidate(TerrainCanvas* canvas)
|
|
|
|
{
|
|
|
|
if (canvas->detail_scaling < 0.00001)
|
|
|
|
{
|
|
|
|
canvas->detail_scaling = 0.00001;
|
|
|
|
}
|
2012-07-13 12:23:58 +00:00
|
|
|
heightmapValidate(&canvas->height_map);
|
2012-07-10 14:28:47 +00:00
|
|
|
noiseValidate(canvas->detail_noise);
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerType terrainCanvasGetLayerType()
|
|
|
|
{
|
|
|
|
LayerType result;
|
|
|
|
|
|
|
|
result.callback_create = (LayerCallbackCreate)terrainCanvasCreate;
|
|
|
|
result.callback_delete = (LayerCallbackDelete)terrainCanvasDelete;
|
|
|
|
result.callback_copy = (LayerCallbackCopy)terrainCanvasCopy;
|
|
|
|
result.callback_validate = (LayerCallbackValidate)terrainCanvasValidate;
|
|
|
|
result.callback_save = (LayerCallbackSave)terrainCanvasSave;
|
|
|
|
result.callback_load = (LayerCallbackLoad)terrainCanvasLoad;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainCanvasSave(PackStream* stream, TerrainCanvas* canvas)
|
|
|
|
{
|
|
|
|
packWriteInt(stream, &canvas->area.bounded);
|
|
|
|
packWriteDouble(stream, &canvas->area.location_x);
|
|
|
|
packWriteDouble(stream, &canvas->area.location_z);
|
|
|
|
packWriteDouble(stream, &canvas->area.size_x);
|
|
|
|
packWriteDouble(stream, &canvas->area.size_z);
|
2012-07-19 15:04:27 +00:00
|
|
|
packWriteDouble(stream, &canvas->offset_y);
|
2012-07-13 12:23:58 +00:00
|
|
|
heightmapSave(stream, &canvas->height_map);
|
2012-07-10 14:28:47 +00:00
|
|
|
packWriteDouble(stream, &canvas->height_factor);
|
|
|
|
noiseSaveGenerator(stream, canvas->detail_noise);
|
|
|
|
packWriteDouble(stream, &canvas->detail_height_factor);
|
|
|
|
packWriteDouble(stream, &canvas->detail_scaling);
|
2012-07-18 15:04:23 +00:00
|
|
|
packWriteInt(stream, &canvas->mask.mode);
|
|
|
|
packWriteDouble(stream, &canvas->mask.smoothing);
|
2012-07-10 14:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void terrainCanvasLoad(PackStream* stream, TerrainCanvas* canvas)
|
|
|
|
{
|
|
|
|
packReadInt(stream, &canvas->area.bounded);
|
|
|
|
packReadDouble(stream, &canvas->area.location_x);
|
|
|
|
packReadDouble(stream, &canvas->area.location_z);
|
|
|
|
packReadDouble(stream, &canvas->area.size_x);
|
|
|
|
packReadDouble(stream, &canvas->area.size_z);
|
2012-07-19 15:04:27 +00:00
|
|
|
packReadDouble(stream, &canvas->offset_y);
|
2012-07-13 12:23:58 +00:00
|
|
|
heightmapLoad(stream, &canvas->height_map);
|
2012-07-10 14:28:47 +00:00
|
|
|
packReadDouble(stream, &canvas->height_factor);
|
|
|
|
noiseLoadGenerator(stream, canvas->detail_noise);
|
|
|
|
packReadDouble(stream, &canvas->detail_height_factor);
|
|
|
|
packReadDouble(stream, &canvas->detail_scaling);
|
2012-07-18 15:04:23 +00:00
|
|
|
packReadInt(stream, &canvas->mask.mode);
|
|
|
|
packReadDouble(stream, &canvas->mask.smoothing);
|
2012-07-10 14:28:47 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 16:21:33 +00:00
|
|
|
double terrainCanvasGetLimits(TerrainCanvas* canvas, double* ymin, double* ymax)
|
|
|
|
{
|
|
|
|
return heightmapGetLimits(canvas->height_map, ymin, ymax);
|
|
|
|
}
|
|
|
|
|
2012-07-10 14:28:47 +00:00
|
|
|
void terrainCanvasRevertToTerrain(TerrainCanvas* canvas, TerrainDefinition* terrain, int only_masked)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
Vector3 terrainCanvasApply(TerrainCanvas* canvas, Vector3 location)
|
2012-07-10 14:28:47 +00:00
|
|
|
{
|
2012-07-16 20:34:01 +00:00
|
|
|
if (location.x >= canvas->area.location_x &&
|
|
|
|
location.z >= canvas->area.location_z &&
|
|
|
|
location.x <= canvas->area.location_x + canvas->area.size_x &&
|
|
|
|
location.z <= canvas->area.location_z + canvas->area.size_z)
|
|
|
|
{
|
2012-07-18 15:04:23 +00:00
|
|
|
double inside_x, inside_z;
|
|
|
|
double height, distance;
|
|
|
|
|
|
|
|
/* Get height map displacement */
|
|
|
|
inside_x = (location.x - canvas->area.location_x) / canvas->area.size_x;
|
|
|
|
inside_z = (location.z - canvas->area.location_z) / canvas->area.size_z;
|
|
|
|
height = heightmapGetValue(&canvas->height_map, inside_x, inside_z);
|
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
/* Apply factor */
|
|
|
|
height = height * canvas->height_factor + canvas->offset_y;
|
|
|
|
|
2012-07-23 09:58:06 +00:00
|
|
|
/* Apply detail noise */
|
|
|
|
height += noiseGet2DTotal(canvas->detail_noise, location.x / canvas->detail_scaling, location.z / canvas->detail_scaling) * canvas->detail_height_factor;
|
2012-07-18 15:04:23 +00:00
|
|
|
|
|
|
|
/* Apply integration mask */
|
|
|
|
inside_x = (inside_x - 0.5) * 2.0;
|
|
|
|
inside_z = (inside_z - 0.5) * 2.0;
|
|
|
|
if (canvas->mask.mode == INTEGRATIONMASK_MODE_SQUARE)
|
|
|
|
{
|
|
|
|
inside_x = fabs(inside_x);
|
|
|
|
inside_z = fabs(inside_z);
|
|
|
|
distance = inside_x > inside_z ? inside_x : inside_z;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
distance = sqrt(inside_x * inside_x + inside_z * inside_z);
|
|
|
|
}
|
|
|
|
if (distance <= 1.0 - canvas->mask.smoothing)
|
|
|
|
{
|
|
|
|
location.y = height;
|
|
|
|
}
|
2012-07-23 09:58:06 +00:00
|
|
|
else if (distance <= 1.0)
|
2012-07-18 15:04:23 +00:00
|
|
|
{
|
|
|
|
double influence = (1.0 - distance) / canvas->mask.smoothing;
|
|
|
|
location.y = influence * height + (1.0 - influence) * location.y;
|
|
|
|
}
|
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
}
|
|
|
|
return location;
|
2012-07-10 14:28:47 +00:00
|
|
|
}
|