paysages : Terrain painting (WIP).

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@491 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2013-01-10 15:41:14 +00:00 committed by ThunderK
parent a1a19dffeb
commit f1fcd70582
6 changed files with 134 additions and 39 deletions

View file

@ -6,7 +6,7 @@
#include <GL/glu.h> #include <GL/glu.h>
#include "tools.h" #include "tools.h"
#define HEIGHTMAP_RESOLUTION 512 #define HEIGHTMAP_RESOLUTION 256
WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain): WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain):
QGLWidget(parent) QGLWidget(parent)
@ -18,6 +18,8 @@ WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain):
startTimer(100); startTimer(100);
_terrain = terrain; _terrain = terrain;
_renderer = rendererCreate();
TerrainRendererClass.bind(_renderer.terrain, _terrain);
_vertices = new _VertexInfo[HEIGHTMAP_RESOLUTION * HEIGHTMAP_RESOLUTION]; _vertices = new _VertexInfo[HEIGHTMAP_RESOLUTION * HEIGHTMAP_RESOLUTION];
_dirty = true; _dirty = true;
@ -45,6 +47,7 @@ WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain):
WidgetHeightMap::~WidgetHeightMap() WidgetHeightMap::~WidgetHeightMap()
{ {
rendererDelete(&_renderer);
noiseDeleteGenerator(_brush_noise); noiseDeleteGenerator(_brush_noise);
delete[] _vertices; delete[] _vertices;
} }
@ -159,16 +162,16 @@ void WidgetHeightMap::timerEvent(QTimerEvent*)
switch (_brush_mode) switch (_brush_mode)
{ {
case HEIGHTMAP_BRUSH_RAISE: case HEIGHTMAP_BRUSH_RAISE:
terrainBrushElevation(_terrain, &brush, brush_strength * _last_brush_action * 20.0); terrainBrushElevation(_terrain->height_map, &brush, brush_strength * _last_brush_action * 20.0);
break; break;
case HEIGHTMAP_BRUSH_SMOOTH: case HEIGHTMAP_BRUSH_SMOOTH:
if (_last_brush_action < 0) if (_last_brush_action < 0)
{ {
terrainBrushSmooth(_terrain, &brush, brush_strength * 0.1); terrainBrushSmooth(_terrain->height_map, &brush, brush_strength * 0.1);
} }
else else
{ {
terrainBrushAddNoise(_terrain, &brush, _brush_noise, brush_strength * 10.0); terrainBrushAddNoise(_terrain->height_map, &brush, _brush_noise, brush_strength * 10.0);
} }
break; break;
default: default:
@ -361,8 +364,9 @@ void WidgetHeightMap::updateVertexInfo()
_VertexInfo* vertex = _vertices + z * rx + x; _VertexInfo* vertex = _vertices + z * rx + x;
vertex->point.x = 80.0 * (double)x / (double)(rx - 1) - 40.0; vertex->point.x = 80.0 * (double)x / (double)(rx - 1) - 40.0;
vertex->point.y = 0.0; //_heightmap->data[z * rx + x];
vertex->point.z = 80.0 * (double)z / (double)(rz - 1) - 40.0; vertex->point.z = 80.0 * (double)z / (double)(rz - 1) - 40.0;
vertex->point.y = _renderer.terrain->getHeight(&_renderer, vertex->point.x, vertex->point.z);
} }
} }

View file

@ -4,6 +4,7 @@
#include <QGLWidget> #include <QGLWidget>
#include <QDateTime> #include <QDateTime>
#include "../lib_paysages/euclid.h" #include "../lib_paysages/euclid.h"
#include "../lib_paysages/renderer.h"
#include "../lib_paysages/terrain/public.h" #include "../lib_paysages/terrain/public.h"
typedef struct typedef struct
@ -51,6 +52,7 @@ private:
private: private:
TerrainDefinition* _terrain; TerrainDefinition* _terrain;
Renderer _renderer;
_VertexInfo* _vertices; _VertexInfo* _vertices;
bool _dirty; bool _dirty;

View file

@ -24,6 +24,9 @@ static TerrainDefinition* _createDefinition()
definition->height = 0.0; definition->height = 0.0;
definition->scaling = 1.0; definition->scaling = 1.0;
definition->shadow_smoothing = 0.0; definition->shadow_smoothing = 0.0;
definition->height_map = terrainHeightMapCreate();
definition->_height_noise = noiseCreateGenerator(); definition->_height_noise = noiseCreateGenerator();
terrainAutoPreset(definition, TERRAIN_PRESET_STANDARD); terrainAutoPreset(definition, TERRAIN_PRESET_STANDARD);
@ -33,6 +36,7 @@ static TerrainDefinition* _createDefinition()
static void _deleteDefinition(TerrainDefinition* definition) static void _deleteDefinition(TerrainDefinition* definition)
{ {
terrainHeightmapDelete(definition->height_map);
noiseDeleteGenerator(definition->_height_noise); noiseDeleteGenerator(definition->_height_noise);
free(definition); free(definition);
} }
@ -43,6 +47,8 @@ static void _copyDefinition(TerrainDefinition* source, TerrainDefinition* destin
destination->scaling = source->scaling; destination->scaling = source->scaling;
destination->shadow_smoothing = source->shadow_smoothing; destination->shadow_smoothing = source->shadow_smoothing;
terrainHeightmapCopy(source->height_map, destination->height_map);
noiseCopy(source->_height_noise, destination->_height_noise); noiseCopy(source->_height_noise, destination->_height_noise);
_validateDefinition(destination); _validateDefinition(destination);
@ -53,6 +59,7 @@ static void _saveDefinition(PackStream* stream, TerrainDefinition* definition)
packWriteDouble(stream, &definition->height); packWriteDouble(stream, &definition->height);
packWriteDouble(stream, &definition->scaling); packWriteDouble(stream, &definition->scaling);
packWriteDouble(stream, &definition->shadow_smoothing); packWriteDouble(stream, &definition->shadow_smoothing);
terrainHeightmapSave(stream, definition->height_map);
noiseSaveGenerator(stream, definition->_height_noise); noiseSaveGenerator(stream, definition->_height_noise);
} }
@ -61,6 +68,7 @@ static void _loadDefinition(PackStream* stream, TerrainDefinition* definition)
packReadDouble(stream, &definition->height); packReadDouble(stream, &definition->height);
packReadDouble(stream, &definition->scaling); packReadDouble(stream, &definition->scaling);
packReadDouble(stream, &definition->shadow_smoothing); packReadDouble(stream, &definition->shadow_smoothing);
terrainHeightmapLoad(stream, definition->height_map);
noiseLoadGenerator(stream, definition->_height_noise); noiseLoadGenerator(stream, definition->_height_noise);
_validateDefinition(definition); _validateDefinition(definition);

View file

@ -5,65 +5,140 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
TerrainHeightMap terrainHeightMapCreate() TerrainHeightMap* terrainHeightMapCreate()
{ {
TerrainHeightMap result; TerrainHeightMap* result;
/*result.data = malloc(sizeof(double)); result = malloc(sizeof(TerrainHeightMap));
result.resolution_x = 1; result->fixed_count = 0;
result.resolution_z = 1;*/ result->fixed_data = malloc(sizeof(TerrainHeightMapData));
result->floating_used = 0;
result->floating_data.data = malloc(sizeof(double));
return result; return result;
} }
void terrainHeightmapDelete(TerrainHeightMap* heightmap) void terrainHeightmapDelete(TerrainHeightMap* heightmap)
{ {
/*free(heightmap->data);*/ 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)
{
heightmap->fixed_data = realloc(heightmap->fixed_data, sizeof(TerrainHeightMapData) * new_count);
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)
{
heightmap->fixed_data = realloc(heightmap->fixed_data, sizeof(TerrainHeightMapData) * new_count);
}
}
heightmap->fixed_count = new_count;
} }
void terrainHeightmapCopy(TerrainHeightMap* source, TerrainHeightMap* destination) void terrainHeightmapCopy(TerrainHeightMap* source, TerrainHeightMap* destination)
{ {
/*destination->resolution_x = source->resolution_x; int i, j;
destination->resolution_z = source->resolution_z;
destination->data = realloc(destination->data, sizeof(double) * destination->resolution_x * destination->resolution_z); _setFixedCount(destination, source->fixed_count);
memcpy(destination->data, source->data, sizeof(double) * destination->resolution_x * destination->resolution_z);*/
for (i = 0; i < destination->fixed_count; i++)
{
destination->fixed_data[i].xstart = source->fixed_data[i].xstart;
destination->fixed_data[i].xsize = source->fixed_data[i].xsize;
destination->fixed_data[i].ystart = source->fixed_data[i].ystart;
destination->fixed_data[i].ysize = source->fixed_data[i].ysize;
if (destination->fixed_data[i].xsize * destination->fixed_data[i].ysize > 0)
{
destination->fixed_data[i].data = realloc(destination->fixed_data[i].data, sizeof(double) * destination->fixed_data[i].xsize * destination->fixed_data[i].ysize);
}
memcpy(destination->fixed_data[i].data, source->fixed_data[i].data, sizeof(double) * destination->fixed_data[i].xsize * destination->fixed_data[i].ysize);
}
destination->floating_used = 0;
} }
void terrainHeightmapSave(PackStream* stream, TerrainHeightMap* heightmap) void terrainHeightmapSave(PackStream* stream, TerrainHeightMap* heightmap)
{ {
/*int i; int i, j;
packWriteInt(stream, &heightmap->resolution_x); packWriteInt(stream, &heightmap->fixed_count);
packWriteInt(stream, &heightmap->resolution_z); for (i = 0; i < heightmap->fixed_count; i++)
for (i = 0; i < heightmap->resolution_x * heightmap->resolution_z; i++)
{ {
packWriteDouble(stream, &heightmap->data[i]); packWriteInt(stream, &heightmap->fixed_data[i].xstart);
}*/ packWriteInt(stream, &heightmap->fixed_data[i].xsize);
packWriteInt(stream, &heightmap->fixed_data[i].ystart);
packWriteInt(stream, &heightmap->fixed_data[i].ysize);
for (j = 0; j < heightmap->fixed_data[i].xsize * heightmap->fixed_data[i].ysize; j++)
{
packWriteDouble(stream, &heightmap->fixed_data[i].data[j]);
}
}
} }
void terrainHeightmapLoad(PackStream* stream, TerrainHeightMap* heightmap) void terrainHeightmapLoad(PackStream* stream, TerrainHeightMap* heightmap)
{ {
/*int i; int new_count, i, j;
packReadInt(stream, &heightmap->resolution_x); packReadInt(stream, &new_count);
packReadInt(stream, &heightmap->resolution_z); _setFixedCount(heightmap, new_count);
heightmap->data = realloc(heightmap->data, sizeof(double) * heightmap->resolution_x * heightmap->resolution_z);
for (i = 0; i < heightmap->resolution_x * heightmap->resolution_z; i++) for (i = 0; i < heightmap->fixed_count; i++)
{ {
packReadDouble(stream, &heightmap->data[i]); packReadInt(stream, &heightmap->fixed_data[i].xstart);
}*/ packReadInt(stream, &heightmap->fixed_data[i].xsize);
packReadInt(stream, &heightmap->fixed_data[i].ystart);
packReadInt(stream, &heightmap->fixed_data[i].ysize);
if (heightmap->fixed_data[i].xsize * heightmap->fixed_data[i].ysize > 0)
{
heightmap->fixed_data[i].data = realloc(heightmap->fixed_data[i].data, sizeof(double) * heightmap->fixed_data[i].xsize * heightmap->fixed_data[i].ysize);
}
for (j = 0; j < heightmap->fixed_data[i].xsize * heightmap->fixed_data[i].ysize; j++)
{
packReadDouble(stream, &heightmap->fixed_data[i].data[j]);
}
} }
void terrainBrushElevation(TerrainDefinition* heightmap, TerrainBrush* brush, double value) heightmap->floating_used = 0;
}
void terrainBrushElevation(TerrainHeightMap* heightmap, TerrainBrush* brush, double value)
{ {
} }
void terrainBrushSmooth(TerrainDefinition* heightmap, TerrainBrush* brush, double value) void terrainBrushSmooth(TerrainHeightMap* heightmap, TerrainBrush* brush, double value)
{ {
} }
void terrainBrushAddNoise(TerrainDefinition* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value) void terrainBrushAddNoise(TerrainHeightMap* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value)
{
}
void terrainBrushReset(TerrainHeightMap* heightmap, TerrainBrush* brush, double value)
{ {
} }

View file

@ -11,15 +11,15 @@ typedef struct
int ystart; int ystart;
int xsize; int xsize;
int ysize; int ysize;
void* data; double* data;
} TerrainHeightMapData; } TerrainHeightMapData;
typedef struct struct TerrainHeightMap
{ {
int fixed_count; int fixed_count;
TerrainHeightMapData* fixed_data; TerrainHeightMapData* fixed_data;
int floating_count; int floating_used;
TerrainHeightMapData* floating_data; TerrainHeightMapData floating_data;
} TerrainHeightMap; };
#endif #endif

View file

@ -4,6 +4,7 @@
#include "../shared/types.h" #include "../shared/types.h"
#include "../noise.h" #include "../noise.h"
#include "../lighting.h" #include "../lighting.h"
#include "private.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -14,12 +15,16 @@ typedef enum
TERRAIN_PRESET_STANDARD TERRAIN_PRESET_STANDARD
} TerrainPreset; } TerrainPreset;
typedef struct TerrainHeightMap TerrainHeightMap;
typedef struct typedef struct
{ {
double height; double height;
double scaling; double scaling;
double shadow_smoothing; double shadow_smoothing;
TerrainHeightMap* height_map;
double _detail; double _detail;
NoiseGenerator* _height_noise; NoiseGenerator* _height_noise;
double _min_height; double _min_height;
@ -59,9 +64,10 @@ typedef struct
double total_radius; double total_radius;
} TerrainBrush; } TerrainBrush;
void terrainBrushElevation(TerrainDefinition* heightmap, TerrainBrush* brush, double value); void terrainBrushElevation(TerrainHeightMap* heightmap, TerrainBrush* brush, double value);
void terrainBrushSmooth(TerrainDefinition* heightmap, TerrainBrush* brush, double value); void terrainBrushSmooth(TerrainHeightMap* heightmap, TerrainBrush* brush, double value);
void terrainBrushAddNoise(TerrainDefinition* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value); void terrainBrushAddNoise(TerrainHeightMap* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value);
void terrainBrushReset(TerrainHeightMap* heightmap, TerrainBrush* brush, double value);
#ifdef __cplusplus #ifdef __cplusplus
} }