2012-12-10 22:05:53 +00:00
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Terrain height map painting.
|
|
|
|
*/
|
2012-07-13 12:23:58 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
#include <assert.h>
|
2012-07-13 12:23:58 +00:00
|
|
|
#include <stdlib.h>
|
2013-01-10 15:41:14 +00:00
|
|
|
#include <string.h>
|
2013-01-10 21:21:56 +00:00
|
|
|
#include <math.h>
|
2013-01-14 13:28:42 +00:00
|
|
|
#include "../tools/memory.h"
|
2013-02-05 21:25:30 +00:00
|
|
|
#include "../tools.h"
|
2012-12-10 22:05:53 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
TerrainHeightMap* terrainHeightMapCreate(TerrainDefinition* terrain)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
TerrainHeightMap* result;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
result = malloc(sizeof(TerrainHeightMap));
|
2013-01-14 13:28:42 +00:00
|
|
|
result->terrain = terrain;
|
2013-01-10 15:41:14 +00:00
|
|
|
result->fixed_count = 0;
|
2013-01-14 13:28:42 +00:00
|
|
|
result->fixed_data = malloc(sizeof(TerrainHeightMapChunk));
|
2013-01-10 15:41:14 +00:00
|
|
|
result->floating_used = 0;
|
|
|
|
result->floating_data.data = malloc(sizeof(double));
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-13 12:23:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainHeightmapDelete(TerrainHeightMap* heightmap)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < heightmap->fixed_count; i++)
|
|
|
|
{
|
|
|
|
free(heightmap->fixed_data[i].data);
|
|
|
|
}
|
|
|
|
free(heightmap->fixed_data);
|
|
|
|
free(heightmap->floating_data.data);
|
|
|
|
free(heightmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _setFixedCount(TerrainHeightMap* heightmap, int new_count)
|
|
|
|
{
|
|
|
|
int old_count = heightmap->fixed_count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (new_count > old_count)
|
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
heightmap->fixed_data = realloc(heightmap->fixed_data, sizeof(TerrainHeightMapChunk) * new_count);
|
2013-01-10 15:41:14 +00:00
|
|
|
for (i = old_count; i < new_count; i++)
|
|
|
|
{
|
|
|
|
heightmap->fixed_data[i].data = malloc(sizeof(double));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (new_count < old_count)
|
|
|
|
{
|
|
|
|
for (i = new_count; i < old_count; i++)
|
|
|
|
{
|
|
|
|
free(heightmap->fixed_data[i].data);
|
|
|
|
}
|
|
|
|
if (new_count > 0)
|
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
heightmap->fixed_data = realloc(heightmap->fixed_data, sizeof(TerrainHeightMapChunk) * new_count);
|
2013-01-10 15:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
heightmap->fixed_count = new_count;
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainHeightmapCopy(TerrainHeightMap* source, TerrainHeightMap* destination)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-10 21:21:56 +00:00
|
|
|
int i;
|
2013-01-10 15:41:14 +00:00
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
destination->terrain = source->terrain;
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
_setFixedCount(destination, source->fixed_count);
|
|
|
|
|
|
|
|
for (i = 0; i < destination->fixed_count; i++)
|
|
|
|
{
|
2013-02-03 21:18:32 +00:00
|
|
|
TerrainHeightMapChunk* chunk_source = source->fixed_data + i;
|
|
|
|
TerrainHeightMapChunk* chunk_destination = destination->fixed_data + i;
|
|
|
|
size_t mapsize = sizeof(double) * chunk_source->rect.xsize * chunk_source->rect.zsize;
|
2013-01-14 13:28:42 +00:00
|
|
|
|
2013-02-03 21:18:32 +00:00
|
|
|
chunk_destination->rect = chunk_source->rect;
|
|
|
|
if (chunk_source->rect.xsize * chunk_source->rect.zsize > 0)
|
2013-01-10 15:41:14 +00:00
|
|
|
{
|
2013-02-03 21:18:32 +00:00
|
|
|
chunk_destination->data = realloc(chunk_destination->data, mapsize);
|
|
|
|
memcpy(chunk_destination->data, chunk_source->data, mapsize);
|
2013-01-10 15:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destination->floating_used = 0;
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainHeightmapSave(PackStream* stream, TerrainHeightMap* heightmap)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
int i, j;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
packWriteInt(stream, &heightmap->fixed_count);
|
|
|
|
for (i = 0; i < heightmap->fixed_count; i++)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
TerrainHeightMapChunk* chunk = heightmap->fixed_data + i;
|
|
|
|
|
|
|
|
packWriteInt(stream, &chunk->rect.xstart);
|
|
|
|
packWriteInt(stream, &chunk->rect.xend);
|
|
|
|
packWriteInt(stream, &chunk->rect.xsize);
|
|
|
|
packWriteInt(stream, &chunk->rect.zstart);
|
|
|
|
packWriteInt(stream, &chunk->rect.zend);
|
|
|
|
packWriteInt(stream, &chunk->rect.zsize);
|
|
|
|
for (j = 0; j < chunk->rect.xsize * chunk->rect.zsize; j++)
|
2013-01-10 15:41:14 +00:00
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
packWriteDouble(stream, &chunk->data[j]);
|
2013-01-10 15:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainHeightmapLoad(PackStream* stream, TerrainHeightMap* heightmap)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-10 15:41:14 +00:00
|
|
|
int new_count, i, j;
|
|
|
|
|
|
|
|
packReadInt(stream, &new_count);
|
|
|
|
_setFixedCount(heightmap, new_count);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
for (i = 0; i < heightmap->fixed_count; i++)
|
2012-07-13 12:23:58 +00:00
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
TerrainHeightMapChunk* chunk = heightmap->fixed_data + i;
|
|
|
|
|
|
|
|
packReadInt(stream, &chunk->rect.xstart);
|
|
|
|
packReadInt(stream, &chunk->rect.xend);
|
|
|
|
packReadInt(stream, &chunk->rect.xsize);
|
|
|
|
packReadInt(stream, &chunk->rect.zstart);
|
|
|
|
packReadInt(stream, &chunk->rect.zend);
|
|
|
|
packReadInt(stream, &chunk->rect.zsize);
|
|
|
|
if (chunk->rect.xsize * chunk->rect.zsize > 0)
|
2013-01-10 15:41:14 +00:00
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
chunk->data = realloc(chunk->data, sizeof(double) * chunk->rect.xsize * chunk->rect.zsize);
|
2013-01-10 15:41:14 +00:00
|
|
|
}
|
2013-01-14 13:28:42 +00:00
|
|
|
for (j = 0; j < chunk->rect.xsize * chunk->rect.zsize; j++)
|
2013-01-10 15:41:14 +00:00
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
packReadDouble(stream, &chunk->data[j]);
|
2013-01-10 15:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
heightmap->floating_used = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
static inline int _checkDataHit(TerrainHeightMapChunk* chunk, double x, double z, double* result)
|
2013-01-14 12:08:38 +00:00
|
|
|
{
|
2013-02-05 21:25:30 +00:00
|
|
|
if (x >= (double)chunk->rect.xstart && x <= (double)chunk->rect.xend && z >= (double)chunk->rect.zstart && z <= (double)chunk->rect.zend)
|
2013-01-14 12:08:38 +00:00
|
|
|
{
|
2013-02-05 21:25:30 +00:00
|
|
|
double stencil[16];
|
|
|
|
int ix, iz, cx, cz;
|
|
|
|
int xmax = chunk->rect.xsize - 1;
|
|
|
|
int zmax = chunk->rect.zsize - 1;
|
|
|
|
int xlow;
|
|
|
|
int zlow;
|
|
|
|
|
|
|
|
x -= chunk->rect.xstart;
|
|
|
|
z -= chunk->rect.zstart;
|
|
|
|
|
|
|
|
xlow = floor(x);
|
|
|
|
zlow = floor(z);
|
|
|
|
|
|
|
|
for (ix = xlow - 1; ix <= xlow + 2; ix++)
|
|
|
|
{
|
|
|
|
for (iz = zlow - 1; iz <= zlow + 2; iz++)
|
|
|
|
{
|
|
|
|
cx = ix < 0 ? 0 : ix;
|
|
|
|
cx = cx > xmax ? xmax : cx;
|
|
|
|
cz = iz < 0 ? 0 : iz;
|
|
|
|
cz = cz > zmax ? zmax : cz;
|
|
|
|
stencil[(iz - (zlow - 1)) * 4 + ix - (xlow - 1)] = chunk->data[cz * chunk->rect.xsize + cx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = toolsBicubicInterpolate(stencil, x - (double)xlow, z - (double)zlow);
|
|
|
|
|
2013-01-14 12:08:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int terrainHeightmapGetHeight(TerrainHeightMap* heightmap, double x, double z, double* result)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < heightmap->fixed_count; i++)
|
|
|
|
{
|
|
|
|
if (_checkDataHit(heightmap->fixed_data + i, x, z, result))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heightmap->floating_used && _checkDataHit(&heightmap->floating_data, x, z, result))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:28:42 +00:00
|
|
|
static inline void _resetRect(TerrainHeightMap* heightmap, int x1, int x2, int z1, int z2)
|
|
|
|
{
|
|
|
|
int x, z;
|
|
|
|
|
|
|
|
for (x = x1; x <= x2; x++)
|
|
|
|
{
|
|
|
|
for (z = z1; z <= z2; z++)
|
|
|
|
{
|
|
|
|
heightmap->floating_data.data[z * heightmap->floating_data.rect.xsize + x] = terrainGetGridHeight(heightmap->terrain, x + heightmap->floating_data.rect.xstart, z + heightmap->floating_data.rect.zstart, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 21:21:56 +00:00
|
|
|
static void _prepareBrushStroke(TerrainHeightMap* heightmap, TerrainBrush* brush)
|
|
|
|
{
|
|
|
|
double s = brush->smoothed_size + brush->hard_radius;
|
2013-01-14 13:28:42 +00:00
|
|
|
int x1 = (int)floor(brush->relative_x - s);
|
|
|
|
int x2 = (int)ceil(brush->relative_x + s);
|
|
|
|
int z1 = (int)floor(brush->relative_z - s);
|
|
|
|
int z2 = (int)ceil(brush->relative_z + s);
|
2013-01-10 21:21:56 +00:00
|
|
|
|
2013-01-14 12:08:38 +00:00
|
|
|
/* Prepare floating data */
|
|
|
|
if (heightmap->floating_used)
|
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
int gx1 = (x1 < heightmap->floating_data.rect.xstart) ? heightmap->floating_data.rect.xstart - x1 : 0;
|
|
|
|
int gx2 = (x2 > heightmap->floating_data.rect.xend) ? x2 - heightmap->floating_data.rect.xend : 0;
|
|
|
|
int gz1 = (z1 < heightmap->floating_data.rect.zstart) ? heightmap->floating_data.rect.zstart - z1 : 0;
|
|
|
|
int gz2 = (z2 > heightmap->floating_data.rect.zend) ? z2 - heightmap->floating_data.rect.zend : 0;
|
|
|
|
if (gx1 || gx2 || gz1 || gz2)
|
|
|
|
{
|
|
|
|
/* Floating area needs growing */
|
|
|
|
int new_width = heightmap->floating_data.rect.xsize + gx1 + gx2;
|
|
|
|
int new_height = heightmap->floating_data.rect.zsize + gz1 + gz2;
|
|
|
|
|
|
|
|
heightmap->floating_data.data = memory2dRealloc(heightmap->floating_data.data, sizeof(double), heightmap->floating_data.rect.xsize, heightmap->floating_data.rect.zsize, new_width, new_height, gx1, gz1);
|
|
|
|
|
|
|
|
heightmap->floating_data.rect.xstart -= gx1;
|
|
|
|
heightmap->floating_data.rect.xend += gx2;
|
|
|
|
heightmap->floating_data.rect.xsize += gx1 + gx2;
|
|
|
|
heightmap->floating_data.rect.zstart -= gz1;
|
|
|
|
heightmap->floating_data.rect.zend += gz2;
|
|
|
|
heightmap->floating_data.rect.zsize += gz1 + gz2;
|
|
|
|
|
|
|
|
_resetRect(heightmap, 0, new_width - 1, 0, gz1 - 1);
|
2013-02-03 21:18:32 +00:00
|
|
|
_resetRect(heightmap, 0, new_width - 1, new_height - gz2, new_height - 1);
|
|
|
|
_resetRect(heightmap, 0, gx1 - 1, gz1, new_height - 1 - gz2);
|
|
|
|
_resetRect(heightmap, new_width - gx2, new_width - 1, gz1, new_height - 1 - gz2);
|
2013-01-14 13:28:42 +00:00
|
|
|
}
|
2013-01-14 12:08:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-14 13:28:42 +00:00
|
|
|
/* Init floating area */
|
|
|
|
heightmap->floating_data.rect.xstart = x1;
|
|
|
|
heightmap->floating_data.rect.xend = x2;
|
|
|
|
heightmap->floating_data.rect.xsize = x2 - x1 + 1;
|
|
|
|
heightmap->floating_data.rect.zstart = z1;
|
|
|
|
heightmap->floating_data.rect.zend = z2;
|
|
|
|
heightmap->floating_data.rect.zsize = z2 - z1 + 1;
|
2013-01-14 12:08:38 +00:00
|
|
|
|
|
|
|
size_t new_size;
|
2013-01-14 13:28:42 +00:00
|
|
|
new_size = sizeof(double) * heightmap->floating_data.rect.xsize * heightmap->floating_data.rect.zsize;
|
2013-01-14 12:08:38 +00:00
|
|
|
heightmap->floating_data.data = realloc(heightmap->floating_data.data, new_size);
|
2013-01-14 13:28:42 +00:00
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
_resetRect(heightmap, 0, heightmap->floating_data.rect.xsize - 1, 0, heightmap->floating_data.rect.zsize - 1);
|
2013-01-14 13:28:42 +00:00
|
|
|
|
|
|
|
heightmap->floating_used = 1;
|
2013-01-14 12:08:38 +00:00
|
|
|
}
|
2013-01-10 21:21:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 21:18:32 +00:00
|
|
|
size_t terrainGetMemoryStats(TerrainDefinition* definition)
|
|
|
|
{
|
|
|
|
TerrainHeightMap* heightmap = definition->height_map;
|
|
|
|
size_t result = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (heightmap->floating_used)
|
|
|
|
{
|
|
|
|
result += sizeof(double) * heightmap->floating_data.rect.xsize * heightmap->floating_data.rect.zsize;
|
|
|
|
}
|
|
|
|
for (i = 0; i < heightmap->fixed_count; i++)
|
|
|
|
{
|
|
|
|
result += sizeof(double) * heightmap->fixed_data[i].rect.xsize * heightmap->fixed_data[i].rect.zsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
void terrainBrushElevation(TerrainHeightMap* heightmap, TerrainBrush* brush, double value)
|
|
|
|
{
|
2013-01-14 12:08:38 +00:00
|
|
|
_prepareBrushStroke(heightmap, brush);
|
2012-12-10 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
void terrainBrushSmooth(TerrainHeightMap* heightmap, TerrainBrush* brush, double value)
|
2012-12-10 22:05:53 +00:00
|
|
|
{
|
2013-01-14 12:08:38 +00:00
|
|
|
_prepareBrushStroke(heightmap, brush);
|
2012-12-10 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
void terrainBrushAddNoise(TerrainHeightMap* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value)
|
2012-12-10 22:05:53 +00:00
|
|
|
{
|
2013-01-14 12:08:38 +00:00
|
|
|
_prepareBrushStroke(heightmap, brush);
|
2012-12-10 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 15:41:14 +00:00
|
|
|
void terrainBrushReset(TerrainHeightMap* heightmap, TerrainBrush* brush, double value)
|
2012-12-10 22:05:53 +00:00
|
|
|
{
|
2012-07-13 12:23:58 +00:00
|
|
|
}
|
2012-07-13 21:24:19 +00:00
|
|
|
|
2013-02-03 21:18:32 +00:00
|
|
|
void terrainEndBrushStroke(TerrainHeightMap* heightmap)
|
|
|
|
{
|
|
|
|
/* Commit floating data to fixed */
|
|
|
|
if (heightmap->floating_used)
|
|
|
|
{
|
|
|
|
/* TODO Find overlapping data and merge with them */
|
|
|
|
|
|
|
|
size_t mapsize = sizeof(double) * heightmap->floating_data.rect.xsize * heightmap->floating_data.rect.zsize;
|
|
|
|
heightmap->fixed_data = realloc(heightmap->fixed_data, sizeof(TerrainHeightMapChunk) * (heightmap->fixed_count + 1));
|
|
|
|
heightmap->fixed_data[heightmap->fixed_count].rect = heightmap->floating_data.rect;
|
|
|
|
heightmap->fixed_data[heightmap->fixed_count].data = malloc(mapsize);
|
|
|
|
memcpy(heightmap->fixed_data[heightmap->fixed_count].data, heightmap->floating_data.data, mapsize);
|
|
|
|
|
|
|
|
heightmap->fixed_count++;
|
|
|
|
heightmap->floating_used = 0;
|
|
|
|
heightmap->floating_data.data = realloc(heightmap->floating_data.data, sizeof(double));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
#if 0
|
2012-07-19 15:04:27 +00:00
|
|
|
static void _loadFromFilePixel(HeightMap* heightmap, int x, int y, Color col)
|
|
|
|
{
|
|
|
|
assert(x >= 0 && x < heightmap->resolution_x);
|
|
|
|
assert(y >= 0 && y < heightmap->resolution_z);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
heightmap->data[y * heightmap->resolution_x + x] = (col.r + col.g + col.b) / 3.0;
|
|
|
|
}
|
|
|
|
|
2012-07-23 21:14:26 +00:00
|
|
|
void heightmapGetLimits(HeightMap* heightmap, double* ymin, double* ymax)
|
2012-07-23 09:58:06 +00:00
|
|
|
{
|
|
|
|
double y;
|
|
|
|
int i;
|
|
|
|
*ymin = 1000000.0;
|
|
|
|
*ymax = -1000000.0;
|
|
|
|
/* TODO Keep the value in cache */
|
|
|
|
for (i = 0; i < heightmap->resolution_x * heightmap->resolution_z; i++)
|
|
|
|
{
|
|
|
|
y = heightmap->data[i];
|
|
|
|
if (y < *ymin)
|
|
|
|
{
|
|
|
|
*ymin = y;
|
|
|
|
}
|
|
|
|
if (y > *ymax)
|
|
|
|
{
|
|
|
|
*ymax = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double heightmapGetRawValue(HeightMap* heightmap, double x, double z)
|
|
|
|
{
|
|
|
|
assert(x >= 0.0 && x <= 1.0);
|
|
|
|
assert(z >= 0.0 && z <= 1.0);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-23 09:58:06 +00:00
|
|
|
return heightmap->data[((int)(z * (double)(heightmap->resolution_z - 1))) * heightmap->resolution_x + ((int)(x * (double)(heightmap->resolution_x - 1)))];
|
|
|
|
}
|
|
|
|
|
2012-07-16 20:34:01 +00:00
|
|
|
double heightmapGetValue(HeightMap* heightmap, double x, double z)
|
|
|
|
{
|
2012-07-18 15:04:23 +00:00
|
|
|
int xmax = heightmap->resolution_x - 1;
|
|
|
|
int zmax = heightmap->resolution_z - 1;
|
2012-07-26 21:10:12 +00:00
|
|
|
int xlow;
|
|
|
|
int zlow;
|
2012-07-18 15:04:23 +00:00
|
|
|
double stencil[16];
|
|
|
|
int ix, iz, cx, cz;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-26 21:10:12 +00:00
|
|
|
if (x < 0.0)
|
|
|
|
{
|
|
|
|
x = 0.0;
|
|
|
|
}
|
|
|
|
else if (x > 1.0)
|
|
|
|
{
|
|
|
|
x = 1.0;
|
|
|
|
}
|
|
|
|
if (z < 0.0)
|
|
|
|
{
|
|
|
|
z = 0.0;
|
|
|
|
}
|
|
|
|
else if (z > 1.0)
|
|
|
|
{
|
|
|
|
z = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlow = floor(x * xmax);
|
|
|
|
zlow = floor(z * zmax);
|
|
|
|
|
2012-07-18 15:04:23 +00:00
|
|
|
for (ix = xlow - 1; ix <= xlow + 2; ix++)
|
|
|
|
{
|
|
|
|
for (iz = zlow - 1; iz <= zlow + 2; iz++)
|
|
|
|
{
|
|
|
|
cx = ix < 0 ? 0 : ix;
|
|
|
|
cx = cx > xmax ? xmax : cx;
|
|
|
|
cz = iz < 0 ? 0 : iz;
|
|
|
|
cz = cz > zmax ? zmax : cz;
|
|
|
|
stencil[(iz - (zlow - 1)) * 4 + ix - (xlow - 1)] = heightmap->data[cz * heightmap->resolution_x + cx];
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-18 15:04:23 +00:00
|
|
|
return toolsBicubicInterpolate(stencil, x * xmax - (double)xlow, z * zmax - (double)zlow);
|
2012-07-16 20:34:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 13:30:40 +00:00
|
|
|
|
|
|
|
void heightmapImportFromPicture(HeightMap* heightmap, const char* picturepath)
|
|
|
|
{
|
|
|
|
systemLoadPictureFile(picturepath, (PictureCallbackLoadStarted)heightmapChangeResolution, (PictureCallbackLoadPixel)_loadFromFilePixel, heightmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void heightmapChangeResolution(HeightMap* heightmap, int resolution_x, int resolution_z)
|
|
|
|
{
|
|
|
|
int i;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-08-08 13:30:40 +00:00
|
|
|
heightmap->resolution_x = resolution_x;
|
|
|
|
heightmap->resolution_z = resolution_z;
|
|
|
|
heightmap->data = realloc(heightmap->data, sizeof(double) * heightmap->resolution_x * heightmap->resolution_z);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-08-08 13:30:40 +00:00
|
|
|
for (i = 0; i < heightmap->resolution_x * heightmap->resolution_z; i++)
|
|
|
|
{
|
|
|
|
heightmap->data[i] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void heightmapRevertToTerrain(HeightMap* heightmap, TerrainDefinition* terrain, GeoArea* area)
|
|
|
|
{
|
|
|
|
int rx, rz;
|
|
|
|
int x, z;
|
2012-08-08 14:44:37 +00:00
|
|
|
double dx, dz;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-08-08 13:30:40 +00:00
|
|
|
rx = heightmap->resolution_x;
|
|
|
|
rz = heightmap->resolution_z;
|
|
|
|
for (x = 0; x < rx; x++)
|
|
|
|
{
|
|
|
|
for (z = 0; z < rz; z++)
|
|
|
|
{
|
2012-08-08 14:44:37 +00:00
|
|
|
dx = (double)x / (double)(rx - 1);
|
|
|
|
dz = (double)z / (double)(rz - 1);
|
|
|
|
geoareaFromLocal(area, dx, dz, &dx, &dz);
|
2012-12-09 17:49:28 +00:00
|
|
|
// heightmap->data[z * rx + x] = terrainGetHeight(terrain, dx, dz);
|
2012-08-08 13:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
static inline void _getBrushBoundaries(TerrainBrush* brush, int rx, int rz, int* x1, int* x2, int* z1, int* z2)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
|
|
|
double cx = brush->relative_x * rx;
|
|
|
|
double cz = brush->relative_z * rz;
|
|
|
|
double s = brush->smoothed_size + brush->hard_radius;
|
|
|
|
double sx = s * rx;
|
|
|
|
double sz = s * rz;
|
|
|
|
*x1 = (int)floor(cx - sx);
|
|
|
|
*x2 = (int)ceil(cx + sx);
|
|
|
|
*z1 = (int)floor(cz - sz);
|
|
|
|
*z2 = (int)ceil(cz + sz);
|
2012-07-26 21:10:12 +00:00
|
|
|
if (*x1 < 0)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
|
|
|
*x1 = 0;
|
|
|
|
}
|
2012-07-26 21:10:12 +00:00
|
|
|
else if (*x1 > rx)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
|
|
|
*x1 = rx;
|
|
|
|
}
|
2012-07-26 21:10:12 +00:00
|
|
|
if (*x2 < 0)
|
|
|
|
{
|
|
|
|
*x2 = 0;
|
|
|
|
}
|
|
|
|
else if (*x2 > rx)
|
|
|
|
{
|
|
|
|
*x2 = rx;
|
|
|
|
}
|
|
|
|
if (*z1 < 0)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
|
|
|
*z1 = 0;
|
|
|
|
}
|
2012-07-26 21:10:12 +00:00
|
|
|
else if (*z1 > rz)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
|
|
|
*z1 = rz;
|
|
|
|
}
|
2012-07-26 21:10:12 +00:00
|
|
|
if (*z2 < 0)
|
|
|
|
{
|
|
|
|
*z2 = 0;
|
|
|
|
}
|
|
|
|
else if (*z2 > rz)
|
|
|
|
{
|
|
|
|
*z2 = rz;
|
|
|
|
}
|
2012-07-24 14:59:06 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
typedef double (*BrushCallback)(HeightMap* heightmap, TerrainBrush* brush, double x, double z, double basevalue, double influence, double force, void* data);
|
2012-07-26 11:55:19 +00:00
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
static inline void _applyBrush(HeightMap* heightmap, TerrainBrush* brush, double force, void* data, BrushCallback callback)
|
2012-07-16 15:48:05 +00:00
|
|
|
{
|
2012-07-24 14:59:06 +00:00
|
|
|
int x, x1, x2, z, z1, z2;
|
2012-08-08 13:30:40 +00:00
|
|
|
double dx, dz, distance, influence;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-24 14:59:06 +00:00
|
|
|
_getBrushBoundaries(brush, heightmap->resolution_x - 1, heightmap->resolution_z - 1, &x1, &x2, &z1, &z2);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-24 14:59:06 +00:00
|
|
|
for (x = x1; x <= x2; x++)
|
2012-07-16 15:48:05 +00:00
|
|
|
{
|
|
|
|
dx = (double)x / (double)heightmap->resolution_x;
|
2012-07-24 14:59:06 +00:00
|
|
|
for (z = z1; z <= z2; z++)
|
2012-07-16 15:48:05 +00:00
|
|
|
{
|
|
|
|
dz = (double)z / (double)heightmap->resolution_z;
|
|
|
|
distance = sqrt((brush->relative_x - dx) * (brush->relative_x - dx) + (brush->relative_z - dz) * (brush->relative_z - dz));
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-16 15:48:05 +00:00
|
|
|
if (distance > brush->hard_radius)
|
|
|
|
{
|
|
|
|
if (distance <= brush->hard_radius + brush->smoothed_size)
|
|
|
|
{
|
2012-07-26 11:55:19 +00:00
|
|
|
influence = (1.0 - (distance - brush->hard_radius) / brush->smoothed_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
2012-07-16 15:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-26 11:55:19 +00:00
|
|
|
influence = 1.0;
|
2012-07-16 15:48:05 +00:00
|
|
|
}
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-07-26 11:55:19 +00:00
|
|
|
heightmap->data[z * heightmap->resolution_x + x] = callback(heightmap, brush, dx, dz, heightmap->data[z * heightmap->resolution_x + x], influence, force, data);
|
2012-07-16 15:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 14:59:06 +00:00
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
static double _applyBrushElevation(HeightMap* heightmap, TerrainBrush* brush, double x, double z, double basevalue, double influence, double force, void* data)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
2012-07-26 11:55:19 +00:00
|
|
|
return basevalue + influence * force * brush->total_radius;
|
2012-07-24 14:59:06 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainBrushElevation(HeightMap* heightmap, TerrainBrush* brush, double value)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
2012-07-26 11:55:19 +00:00
|
|
|
_applyBrush(heightmap, brush, value, NULL, _applyBrushElevation);
|
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
static double _applyBrushSmooth(HeightMap* heightmap, TerrainBrush* brush, double x, double z, double basevalue, double influence, double force, void* data)
|
2012-07-26 11:55:19 +00:00
|
|
|
{
|
|
|
|
double ideal, factor;
|
|
|
|
ideal = heightmapGetValue(heightmap, x + brush->total_radius * 0.5, z);
|
|
|
|
ideal += heightmapGetValue(heightmap, x - brush->total_radius * 0.5, z);
|
|
|
|
ideal += heightmapGetValue(heightmap, x, z - brush->total_radius * 0.5);
|
|
|
|
ideal += heightmapGetValue(heightmap, x, z + brush->total_radius * 0.5);
|
|
|
|
ideal /= 4.0;
|
|
|
|
factor = influence * force;
|
|
|
|
if (factor > 1.0)
|
2012-07-24 14:59:06 +00:00
|
|
|
{
|
2012-07-26 11:55:19 +00:00
|
|
|
factor = 0.0;
|
2012-07-24 14:59:06 +00:00
|
|
|
}
|
2012-07-26 11:55:19 +00:00
|
|
|
return basevalue + (ideal - basevalue) * factor;
|
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainBrushSmooth(HeightMap* heightmap, TerrainBrush* brush, double value)
|
2012-07-26 11:55:19 +00:00
|
|
|
{
|
|
|
|
_applyBrush(heightmap, brush, value, NULL, _applyBrushSmooth);
|
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
static double _applyBrushAddNoise(HeightMap* heightmap, TerrainBrush* brush, double x, double z, double basevalue, double influence, double force, void* data)
|
2012-07-26 11:55:19 +00:00
|
|
|
{
|
|
|
|
return basevalue + noiseGet2DTotal((NoiseGenerator*)data, x / brush->total_radius, z / brush->total_radius) * influence * force * brush->total_radius;
|
|
|
|
}
|
|
|
|
|
2012-12-10 22:05:53 +00:00
|
|
|
void terrainBrushAddNoise(HeightMap* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value)
|
2012-07-26 11:55:19 +00:00
|
|
|
{
|
|
|
|
_applyBrush(heightmap, brush, value, generator, _applyBrushAddNoise);
|
2012-07-24 14:59:06 +00:00
|
|
|
}
|
2012-12-10 22:05:53 +00:00
|
|
|
#endif
|