paysages : Small refactorings.

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@403 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-08-08 13:30:40 +00:00 committed by ThunderK
parent 77397d35bd
commit 11d48cc3c3
18 changed files with 223 additions and 102 deletions

View file

@ -408,9 +408,9 @@ BaseInput* BaseForm::addInputMaterial(QString label, SurfaceMaterial* material)
return addInput(new InputMaterial(_form, label, material));
}
BaseInput* BaseForm::addInputHeightMap(QString label, HeightMap* heightmap)
BaseInput* BaseForm::addInputHeightMap(QString label, HeightMap* heightmap, TerrainCanvas* canvas)
{
return addInput(new InputHeightMap(_form, label, heightmap));
return addInput(new InputHeightMap(_form, label, heightmap, canvas));
}
BaseInput* BaseForm::addInputEnum(QString label, int* value, const QStringList& values)

View file

@ -15,6 +15,7 @@
#include "../lib_paysages/layers.h"
#include "../lib_paysages/heightmap.h"
#include "../lib_paysages/pack.h"
#include "../lib_paysages/terraincanvas.h"
class BaseForm:public QWidget
{
@ -57,7 +58,7 @@ protected:
BaseInput* addInputNoise(QString label, NoiseGenerator* value);
BaseInput* addInputCurve(QString label, Curve* value, double xmin, double xmax, double ymin, double ymax, QString xlabel, QString ylabel);
BaseInput* addInputMaterial(QString label, SurfaceMaterial* material);
BaseInput* addInputHeightMap(QString label, HeightMap* heightmap);
BaseInput* addInputHeightMap(QString label, HeightMap* heightmap, TerrainCanvas* canvas);
BaseInput* addInputEnum(QString label, int* value, const QStringList& values);
BaseInput* addInputLayers(QString label, Layers* value, FormLayerBuilder form_builder);

View file

@ -83,7 +83,7 @@ bool DialogCurve::getCurve(QWidget* parent, Curve* curve, double xmin, double xm
return (result != 0) ? true : false;
}
void DialogCurve::closeEvent(QCloseEvent* e)
void DialogCurve::closeEvent(QCloseEvent*)
{
reject();
}

View file

@ -6,11 +6,14 @@
#include <QPushButton>
#include <QComboBox>
#include <QSlider>
#include <QFileDialog>
#include <math.h>
#include "../lib_paysages/terrain.h"
#include "../lib_paysages/scenery.h"
#include "widgetheightmap.h"
/**************** Dialog form ****************/
DialogHeightMap::DialogHeightMap(QWidget* parent, HeightMap* heightmap) : DialogWithPreview(parent)
DialogHeightMap::DialogHeightMap(QWidget* parent, HeightMap* heightmap, TerrainCanvas* canvas) : DialogWithPreview(parent)
{
QWidget* mainarea;
QWidget* buttons;
@ -23,6 +26,7 @@ DialogHeightMap::DialogHeightMap(QWidget* parent, HeightMap* heightmap) : Dialog
QPushButton* button;
QComboBox* combobox;
_canvas = canvas;
_value_original = heightmap;
_value_modified = heightmapCreate();
heightmapCopy(_value_original, &_value_modified);
@ -62,8 +66,15 @@ DialogHeightMap::DialogHeightMap(QWidget* parent, HeightMap* heightmap) : Dialog
viewer_layout->addWidget(slider, 0, 1);
// Panel layout
if (canvas)
{
button = new QPushButton(tr("Reset to terrain height"), panel);
connect(button, SIGNAL(clicked()), _3dview, SLOT(resetToTerrain()));
connect(button, SIGNAL(clicked()), this, SLOT(resetToTerrain()));
panel->layout()->addWidget(button);
}
button = new QPushButton(tr("Load from picture file"), panel);
connect(button, SIGNAL(clicked()), this, SLOT(loadFromFile()));
panel->layout()->addWidget(button);
combobox = new QComboBox(panel);
@ -115,11 +126,11 @@ DialogHeightMap::DialogHeightMap(QWidget* parent, HeightMap* heightmap) : Dialog
setWindowTitle(tr("Paysages 3D - Height map painting"));
}
bool DialogHeightMap::editHeightMap(QWidget* parent, HeightMap* heightmap)
bool DialogHeightMap::editHeightMap(QWidget* parent, HeightMap* heightmap, TerrainCanvas* canvas)
{
int result;
DialogHeightMap* dialog = new DialogHeightMap(parent, heightmap);
DialogHeightMap* dialog = new DialogHeightMap(parent, heightmap, canvas);
result = dialog->exec();
delete dialog;
@ -168,3 +179,30 @@ void DialogHeightMap::brushStrengthChanged(int value)
{
_3dview->setBrushStrength((double)value / 2000.0);
}
void DialogHeightMap::loadFromFile()
{
QString filepath = QFileDialog::getOpenFileName(this, tr("Paysages 3D - Choose a picture to load"), QString(), tr("Images (*.jpg *.jpeg *.bmp *.png)"));
if (!filepath.isNull())
{
heightmapImportFromPicture(&_value_modified, (char*) filepath.toStdString().c_str());
_3dview->revert();
}
}
void DialogHeightMap::resetToTerrain()
{
if (_canvas)
{
TerrainDefinition terrain;
terrain = terrainCreateDefinition();
sceneryGetTerrain(&terrain);
heightmapRevertToTerrain(&_value_modified, &terrain, &_canvas->area);
terrainDeleteDefinition(&terrain);
_3dview->revert();
}
}

View file

@ -4,13 +4,14 @@
#include "tools.h"
#include "widgetheightmap.h"
#include "../lib_paysages/heightmap.h"
#include "../lib_paysages/terraincanvas.h"
class DialogHeightMap : public DialogWithPreview
{
Q_OBJECT
public:
explicit DialogHeightMap(QWidget* parent, HeightMap* heightmap);
static bool editHeightMap(QWidget* parent, HeightMap* heightmap);
explicit DialogHeightMap(QWidget* parent, HeightMap* heightmap, TerrainCanvas* canvas);
static bool editHeightMap(QWidget* parent, HeightMap* heightmap, TerrainCanvas* canvas);
public slots:
virtual void accept();
@ -23,11 +24,14 @@ private slots:
void brushSizeChanged(int value);
void brushSmoothingChanged(int value);
void brushStrengthChanged(int value);
void loadFromFile();
void resetToTerrain();
private:
HeightMap* _value_original;
HeightMap _value_modified;
WidgetHeightMap* _3dview;
TerrainCanvas* _canvas;
};
#endif

View file

@ -56,7 +56,7 @@ bool DialogLayers::editLayers(QWidget* parent, Layers* layers, QString title, Fo
return result != 0;
}
void DialogLayers::closeEvent(QCloseEvent* e)
void DialogLayers::closeEvent(QCloseEvent*)
{
reject();
}

View file

@ -44,7 +44,7 @@ FormTerrainCanvas::FormTerrainCanvas(QWidget *parent, Layers* layers):
// TODO Area
//addInputDouble(tr("Apply at height"), &_definition->offset_y, -20.0, 20.0, 0.1, 1.0);
addInputHeightMap(tr("Height map"), &_definition->height_map);
addInputHeightMap(tr("Height map"), &_definition->height_map, _definition);
//addInputDouble(tr("Canvas height"), &_definition->height_factor, 0.0, 20.0, 0.1, 1.0);
addInputNoise(tr("Detail noise"), _definition->detail_noise);
addInputDouble(tr("Detail noise height"), &_definition->detail_height_factor, 0.0, 20.0, 0.1, 1.0);

View file

@ -45,9 +45,10 @@ public:
HeightMap* _value;
};
InputHeightMap::InputHeightMap(QWidget* form, QString label, HeightMap* value) : BaseInput(form, label)
InputHeightMap::InputHeightMap(QWidget* form, QString label, HeightMap* value, TerrainCanvas* canvas) : BaseInput(form, label)
{
_value = value;
_canvas = canvas;
_preview = new SmallPreviewHeightMap(form, value);
_preview->setMinimumSize(100, 100);
@ -77,7 +78,7 @@ void InputHeightMap::revert()
void InputHeightMap::editHeightMap()
{
if (DialogHeightMap::editHeightMap(_control, _value))
if (DialogHeightMap::editHeightMap(_control, _value, _canvas))
{
applyValue();
}

View file

@ -5,13 +5,14 @@
#include "baseinput.h"
#include "../lib_paysages/heightmap.h"
#include "../lib_paysages/terraincanvas.h"
class InputHeightMap:public BaseInput
{
Q_OBJECT
public:
InputHeightMap(QWidget* form, QString label, HeightMap* value);
InputHeightMap(QWidget* form, QString label, HeightMap* value, TerrainCanvas* canvas);
public slots:
virtual void updatePreview();
@ -23,6 +24,7 @@ private slots:
private:
HeightMap* _value;
TerrainCanvas* _canvas;
};
#endif

View file

@ -5,8 +5,6 @@
#include <math.h>
#include <GL/glu.h>
#include "tools.h"
#include "../lib_paysages/terrain.h"
#include "../lib_paysages/scenery.h"
WidgetHeightMap::WidgetHeightMap(QWidget *parent, HeightMap* heightmap):
QGLWidget(parent)
@ -18,7 +16,7 @@ WidgetHeightMap::WidgetHeightMap(QWidget *parent, HeightMap* heightmap):
startTimer(100);
_heightmap = heightmap;
_vertexes = new _VertexInfo[heightmap->resolution_x * heightmap->resolution_z];
_vertices = new _VertexInfo[heightmap->resolution_x * heightmap->resolution_z];
_dirty = true;
@ -47,7 +45,7 @@ WidgetHeightMap::WidgetHeightMap(QWidget *parent, HeightMap* heightmap):
WidgetHeightMap::~WidgetHeightMap()
{
noiseDeleteGenerator(_brush_noise);
delete[] _vertexes;
delete[] _vertices;
}
void WidgetHeightMap::setHorizontalViewAngle(double angle_h)
@ -100,28 +98,6 @@ void WidgetHeightMap::revert()
updateGL();
}
void WidgetHeightMap::resetToTerrain()
{
TerrainDefinition terrain;
terrain = terrainCreateDefinition();
sceneryGetTerrain(&terrain);
// TODO Apply geoarea
int rx = _heightmap->resolution_x;
int rz = _heightmap->resolution_z;
for (int x = 0; x < rx; x++)
{
for (int z = 0; z < rz; z++)
{
_heightmap->data[z * rx + x] = terrainGetHeight(&terrain, 80.0 * (double)x / (double)(rx - 1) - 40.0, 80.0 * (double)z / (double)(rz - 1) - 40.0);
}
}
terrainDeleteDefinition(&terrain);
revert();
}
void WidgetHeightMap::mousePressEvent(QMouseEvent* event)
{
_last_mouse_x = event->x();
@ -312,7 +288,7 @@ void WidgetHeightMap::paintGL()
glBegin(GL_QUAD_STRIP);
for (int z = 0; z < rz; z++)
{
_VertexInfo* vertex = _vertexes + z * rx + x;
_VertexInfo* vertex = _vertices + z * rx + x;
double diff_x, diff_z, diff;
diff_x = (vertex + 1)->point.x - _brush_x;
@ -372,12 +348,16 @@ void WidgetHeightMap::updateVertexInfo()
int rx = _heightmap->resolution_x;
int rz = _heightmap->resolution_z;
_VertexInfo* old_vertices = _vertices;
_vertices = new _VertexInfo[rx * rz];
delete[] old_vertices;
// Update positions
for (int x = 0; x < rx; x++)
{
for (int z = 0; z < rz; z++)
{
_VertexInfo* vertex = _vertexes + z * rx + x;
_VertexInfo* vertex = _vertices + z * rx + x;
vertex->point.x = 80.0 * (double)x / (double)(rx - 1) - 40.0;
vertex->point.y = _heightmap->data[z * rx + x];
@ -390,7 +370,7 @@ void WidgetHeightMap::updateVertexInfo()
{
for (int z = 0; z < rz; z++)
{
_VertexInfo* vertex = _vertexes + z * rx + x;
_VertexInfo* vertex = _vertices + z * rx + x;
if (x == rx - 1)
{

View file

@ -34,7 +34,6 @@ public:
public slots:
void revert();
void resetToTerrain();
protected:
void mousePressEvent(QMouseEvent* event);
@ -52,7 +51,7 @@ private:
private:
HeightMap* _heightmap;
_VertexInfo* _vertexes;
_VertexInfo* _vertices;
bool _dirty;

53
lib_paysages/geoarea.c Normal file
View file

@ -0,0 +1,53 @@
#include "geoarea.h"
#include "tools.h"
GeoArea geoareaCreate()
{
GeoArea result;
result.location_x = -40.0;
result.location_z = -40.0;
result.size_x = 80.0;
result.size_z = 80.0;
return result;
}
void geoareaDelete(GeoArea* geoarea)
{
UNUSED(geoarea);
}
void geoareaCopy(GeoArea* source, GeoArea* destination)
{
*destination = *source;
}
void geoareaValidate(GeoArea* geoarea)
{
if (geoarea->size_x < 0.000000001)
{
geoarea->size_x = 0.000000001;
}
if (geoarea->size_z < 0.000000001)
{
geoarea->size_z = 0.000000001;
}
}
void geoareaSave(PackStream* stream, GeoArea* geoarea)
{
packWriteDouble(stream, &geoarea->location_x);
packWriteDouble(stream, &geoarea->location_z);
packWriteDouble(stream, &geoarea->size_x);
packWriteDouble(stream, &geoarea->size_z);
}
void geoareaLoad(PackStream* stream, GeoArea* geoarea)
{
packReadDouble(stream, &geoarea->location_x);
packReadDouble(stream, &geoarea->location_z);
packReadDouble(stream, &geoarea->size_x);
packReadDouble(stream, &geoarea->size_z);
}

32
lib_paysages/geoarea.h Normal file
View file

@ -0,0 +1,32 @@
#ifndef _PAYSAGES_GEOAREA_H_
#define _PAYSAGES_GEOAREA_H_
/* Geographic area definition */
#include "pack.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
double location_x;
double location_z;
double size_x;
double size_z;
} GeoArea;
GeoArea geoareaCreate();
void geoareaDelete(GeoArea* geoarea);
void geoareaCopy(GeoArea* source, GeoArea* destination);
void geoareaValidate(GeoArea* geoarea);
void geoareaSave(PackStream* stream, GeoArea* geoarea);
void geoareaLoad(PackStream* stream, GeoArea* geoarea);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,12 +1,12 @@
#include "heightmap.h"
#include "tools.h"
#include "system.h"
#include "noise.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "tools.h"
#include "system.h"
#include "noise.h"
HeightMap heightmapCreate()
{
@ -34,6 +34,7 @@ void heightmapCopy(HeightMap* source, HeightMap* destination)
void heightmapValidate(HeightMap* heightmap)
{
UNUSED(heightmap);
}
void heightmapSave(PackStream* stream, HeightMap* heightmap)
@ -69,25 +70,6 @@ static void _loadFromFilePixel(HeightMap* heightmap, int x, int y, Color col)
heightmap->data[y * heightmap->resolution_x + x] = (col.r + col.g + col.b) / 3.0;
}
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;
heightmap->resolution_x = resolution_x;
heightmap->resolution_z = resolution_z;
heightmap->data = realloc(heightmap->data, sizeof(double) * heightmap->resolution_x * heightmap->resolution_z);
for (i = 0; i < heightmap->resolution_x * heightmap->resolution_z; i++)
{
heightmap->data[i] = 0.0;
}
}
void heightmapGetLimits(HeightMap* heightmap, double* ymin, double* ymax)
{
double y;
@ -161,6 +143,44 @@ double heightmapGetValue(HeightMap* heightmap, double x, double z)
return toolsBicubicInterpolate(stencil, x * xmax - (double)xlow, z * zmax - (double)zlow);
}
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;
heightmap->resolution_x = resolution_x;
heightmap->resolution_z = resolution_z;
heightmap->data = realloc(heightmap->data, sizeof(double) * heightmap->resolution_x * heightmap->resolution_z);
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;
rx = heightmap->resolution_x;
rz = heightmap->resolution_z;
for (x = 0; x < rx; x++)
{
for (z = 0; z < rz; z++)
{
/* FIXME Apply geoarea */
heightmap->data[z * rx + x] = terrainGetHeight(terrain, 80.0 * (double)x / (double)(rx - 1) - 40.0, 80.0 * (double)z / (double)(rz - 1) - 40.0);
}
}
}
static inline void _getBrushBoundaries(HeightMapBrush* brush, int rx, int rz, int* x1, int* x2, int* z1, int* z2)
{
double cx = brush->relative_x * rx;
@ -211,10 +231,9 @@ typedef double (*BrushCallback)(HeightMap* heightmap, HeightMapBrush* brush, dou
static inline void _applyBrush(HeightMap* heightmap, HeightMapBrush* brush, double force, void* data, BrushCallback callback)
{
int x, x1, x2, z, z1, z2;
double dx, dz, distance, influence, brush_size;
double dx, dz, distance, influence;
_getBrushBoundaries(brush, heightmap->resolution_x - 1, heightmap->resolution_z - 1, &x1, &x2, &z1, &z2);
brush_size = brush->hard_radius + brush->smoothed_size;
for (x = x1; x <= x2; x++)
{

View file

@ -5,6 +5,8 @@
#include "pack.h"
#include "noise.h"
#include "geoarea.h"
#include "terrain.h"
#ifdef __cplusplus
extern "C" {
@ -40,6 +42,7 @@ double heightmapGetValue(HeightMap* heightmap, double x, double z);
void heightmapChangeResolution(HeightMap* heightmap, int resolution_x, int resolution_z);
void heightmapImportFromPicture(HeightMap* heightmap, const char* picturepath);
void heightmapRevertToTerrain(HeightMap* heightmap, TerrainDefinition* terrain, GeoArea* area);
void heightmapBrushElevation(HeightMap* heightmap, HeightMapBrush* brush, double value);
void heightmapBrushSmooth(HeightMap* heightmap, HeightMapBrush* brush, double value);

View file

@ -1,4 +1,5 @@
#include "terraincanvas.h"
#include "scenery.h"
#include <stdlib.h>
#include <string.h>
@ -8,11 +9,7 @@ TerrainCanvas* terrainCanvasCreate()
{
TerrainCanvas* result = malloc(sizeof(TerrainCanvas));
result->area.bounded = 1;
result->area.location_x = -40.0;
result->area.location_z = -40.0;
result->area.size_x = 80.0;
result->area.size_z = 80.0;
result->area = geoareaCreate();
result->offset_y = 0.0;
result->height_map = heightmapCreate();
heightmapChangeResolution(&result->height_map, 256, 256);
@ -25,9 +22,6 @@ TerrainCanvas* terrainCanvasCreate()
result->mask.mode = INTEGRATIONMASK_MODE_CIRCLE;
result->mask.smoothing = 0.1;
/* DEBUG */
/*heightmapImportFromPicture(&result->height_map, "output/height.png");*/
return result;
}
@ -40,7 +34,7 @@ void terrainCanvasDelete(TerrainCanvas* canvas)
void terrainCanvasCopy(TerrainCanvas* source, TerrainCanvas* destination)
{
destination->area = source->area;
geoareaCopy(&source->area, &destination->area);
destination->offset_y = source->offset_y;
destination->height_factor = source->height_factor;
heightmapCopy(&source->height_map, &destination->height_map);
@ -56,6 +50,7 @@ void terrainCanvasValidate(TerrainCanvas* canvas)
{
canvas->detail_scaling = 0.00001;
}
geoareaValidate(&canvas->area);
heightmapValidate(&canvas->height_map);
noiseValidate(canvas->detail_noise);
}
@ -76,11 +71,7 @@ LayerType terrainCanvasGetLayerType()
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);
geoareaSave(stream, &canvas->area);
packWriteDouble(stream, &canvas->offset_y);
heightmapSave(stream, &canvas->height_map);
packWriteDouble(stream, &canvas->height_factor);
@ -93,11 +84,7 @@ void terrainCanvasSave(PackStream* stream, TerrainCanvas* canvas)
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);
geoareaLoad(stream, &canvas->area);
packReadDouble(stream, &canvas->offset_y);
heightmapLoad(stream, &canvas->height_map);
packReadDouble(stream, &canvas->height_factor);
@ -116,8 +103,16 @@ void terrainCanvasGetLimits(TerrainCanvas* canvas, double* ymin, double* ymax)
*ymax += noise_max;
}
void terrainCanvasRevertToTerrain(TerrainCanvas* canvas, TerrainDefinition* terrain, int only_masked)
void terrainCanvasRevertToTerrain(TerrainCanvas* canvas)
{
TerrainDefinition terrain;
terrain = terrainCreateDefinition();
sceneryGetTerrain(&terrain);
heightmapRevertToTerrain(&canvas->height_map, &terrain, &canvas->area);
terrainDeleteDefinition(&terrain);
}
Vector3 terrainCanvasApply(TerrainCanvas* canvas, Vector3 location)

View file

@ -8,20 +8,12 @@
#include "terrain.h"
#include "layers.h"
#include "heightmap.h"
#include "geoarea.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
int bounded;
double location_x;
double location_z;
double size_x;
double size_z;
} GeoArea;
typedef struct
{
int mode;
@ -53,7 +45,7 @@ void terrainCanvasSave(PackStream* stream, TerrainCanvas* canvas);
void terrainCanvasLoad(PackStream* stream, TerrainCanvas* canvas);
void terrainCanvasGetLimits(TerrainCanvas* canvas, double* ymin, double* ymax);
void terrainCanvasRevertToTerrain(TerrainCanvas* canvas, TerrainDefinition* terrain, int only_masked);
void terrainCanvasRevertToTerrain(TerrainCanvas* canvas);
Vector3 terrainCanvasApply(TerrainCanvas* canvas, Vector3 location);
#ifdef __cplusplus

View file

@ -8,6 +8,8 @@
extern "C" {
#endif
#define UNUSED(_x_) ((void)(_x_))
double toolsRandom();
double toolsBicubicInterpolate(double stencil[16], double x, double y);
void toolsFloat2DMapCopy(double* src, double* dest, int src_xstart, int src_ystart, int dest_xstart, int dest_ystart, int xsize, int ysize, int src_xstep, int src_ystep, int dest_xstep, int dest_ystep);