From 701e8faba2cc7219c7292fdac6260c704119a24c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Fri, 28 Dec 2012 11:28:01 +0000 Subject: [PATCH] paysages : Restored terrain and water previews. git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@489 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- gui_qt/formterrain.cpp | 112 ++------------------------------- gui_qt/formterrain.h | 2 +- gui_qt/formwater.cpp | 54 +++++++++++++--- lib_paysages/terrain/preview.c | 95 ++++++++++++++++++++++++++++ lib_paysages/terrain/public.h | 5 +- 5 files changed, 148 insertions(+), 120 deletions(-) create mode 100644 lib_paysages/terrain/preview.c diff --git a/gui_qt/formterrain.cpp b/gui_qt/formterrain.cpp index 871f093..9d004b0 100644 --- a/gui_qt/formterrain.cpp +++ b/gui_qt/formterrain.cpp @@ -1,97 +1,20 @@ #include "formterrain.h" -#include #include #include #include "tools.h" - -#include "../lib_paysages/scenery.h" -#include "../lib_paysages/euclid.h" #include "dialogheightmap.h" +#include "../lib_paysages/scenery.h" static TerrainDefinition* _definition; /**************** Previews ****************/ -class PreviewTerrainHeight:public BasePreview -{ -public: - PreviewTerrainHeight(QWidget* parent):BasePreview(parent) - { - _renderer = rendererCreate(); - - addOsd(QString("geolocation")); - - configScaling(0.5, 200.0, 3.0, 50.0); - configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0); - } -protected: - QColor getColor(double x, double y) - { - double height = 0.0; - - // TODO - //height = terrainGetHeightNormalized(&_preview_definition, x, y); - return QColor((int)(255.0 * height), (int)(255.0 * height), (int)(255.0 * height)); - } - void updateData() - { - TerrainRendererClass.bind(_renderer.terrain, _definition); - } -private: - Renderer _renderer; -}; - class PreviewTerrainColor:public BasePreview { public: PreviewTerrainColor(QWidget* parent):BasePreview(parent) { - LightDefinition light; - TextureLayerDefinition* texture; - - _renderer = rendererCreate(); - _renderer.render_quality = 3; - _renderer.applyTextures = _applyTextures; - _renderer.getLightStatus = _getLightStatus; - _renderer.alterLight = _alterLight; - _renderer.camera_location.x = 0.0; - _renderer.camera_location.y = 50.0; - _renderer.camera_location.z = 0.0; - - _lighting = lightingCreateDefinition(); - light.color.r = 0.6; - light.color.g = 0.6; - light.color.b = 0.6; - light.direction.x = -1.0; - light.direction.y = -0.5; - light.direction.z = 1.0; - light.direction = v3Normalize(light.direction); - light.filtered = 0; - light.masked = 1; - light.reflection = 1.0; - lightingAddLight(&_lighting, light); - light.color.r = 0.3; - light.color.g = 0.3; - light.color.b = 0.3; - light.direction.x = 0.5; - light.direction.y = 0.7071; - light.direction.z = -0.5; - light.filtered = 0; - light.masked = 0; - light.reflection = 0.0; - lightingAddLight(&_lighting, light); - lightingValidateDefinition(&_lighting); - - _textures = texturesCreateDefinition(); - texture = (TextureLayerDefinition*)layersGetLayer(_textures.layers, layersAddLayer(_textures.layers, NULL)); - texture->material.base = COLOR_WHITE; - texture->material.reflection = 0.3; - texture->material.shininess = 2.0; - texture->bump_height = 0.0; - texturesLayerValidateDefinition(texture); - - _renderer.customData[1] = &_textures; - _renderer.customData[2] = &_lighting; + _renderer = terrainCreatePreviewRenderer(); addOsd(QString("geolocation")); @@ -101,39 +24,14 @@ public: protected: QColor getColor(double x, double y) { - Vector3 point; - - point.x = x; - point.y = _renderer.terrain->getHeight(&_renderer, x, y); - point.z = y; - - return colorToQColor(_renderer.terrain->getFinalColor(&_renderer, point, scaling)); + return colorToQColor(terrainGetPreviewColor(&_renderer, x, -y, scaling)); } void updateData() { TerrainRendererClass.bind(_renderer.terrain, _definition); - //sceneryGetTextures(&_textures); } private: Renderer _renderer; - TexturesDefinition _textures; - LightingDefinition _lighting; - - static Color _applyTextures(Renderer* renderer, Vector3 location, double precision) - { - return texturesGetColor((TexturesDefinition*)(renderer->customData[1]), renderer, location.x, location.z, precision); - } - - static void _getLightStatus(Renderer* renderer, LightStatus* status, Vector3 location) - { - lightingGetStatus((LightingDefinition*)renderer->customData[2], renderer, location, status); - } - - static void _alterLight(Renderer* renderer, LightDefinition* light, Vector3 location) - { - *light = renderer->terrain->alterLight(renderer, light, location); - } - }; /**************** Form ****************/ @@ -145,9 +43,9 @@ FormTerrain::FormTerrain(QWidget *parent): QPushButton* button = addButton(tr("Paint")); connect(button, SIGNAL(clicked()), this, SLOT(startPainting())); - previewHeight = new PreviewTerrainHeight(this); + /*previewHeight = new PreviewTerrainHeight(this);*/ previewColor = new PreviewTerrainColor(this); - addPreview(previewHeight, tr("Height preview (normalized)")); + /*addPreview(previewHeight, tr("Height preview (normalized)"));*/ addPreview(previewColor, tr("Lighted preview (no texture)")); //addInputNoise(tr("Noise"), _definition.height_noise); diff --git a/gui_qt/formterrain.h b/gui_qt/formterrain.h index 7e474b4..a9b2c9c 100644 --- a/gui_qt/formterrain.h +++ b/gui_qt/formterrain.h @@ -23,7 +23,7 @@ private slots: void startPainting(); private: - BasePreview* previewHeight; + /*BasePreview* previewHeight;*/ BasePreview* previewColor; }; diff --git a/gui_qt/formwater.cpp b/gui_qt/formwater.cpp index 4f2bc7c..bec674d 100644 --- a/gui_qt/formwater.cpp +++ b/gui_qt/formwater.cpp @@ -20,38 +20,72 @@ class PreviewWaterCoverage:public BasePreview public: PreviewWaterCoverage(QWidget* parent):BasePreview(parent) { - /*_water = waterCreateDefinition(); - _terrain = terrainCreateDefinition();*/ + _renderer = terrainCreatePreviewRenderer(); + _water = waterCreateDefinition(); + _highlight_enabled = true; addOsd(QString("geolocation")); + addToggle("highlight", tr("Coverage highlight"), true); configScaling(0.5, 200.0, 3.0, 50.0); configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0); } -/*protected: +protected: QColor getColor(double x, double y) { double height; - height = terrainGetHeight(&_terrain, x, y); + height = _renderer.terrain->getHeight(&_renderer, x, -y); if (height > _definition.height) { - height = terrainGetHeightNormalized(&_terrain, x, y); - return QColor((int)(255.0 * height), (int)(255.0 * height), (int)(255.0 * height)); + return colorToQColor(terrainGetPreviewColor(&_renderer, x, -y, scaling)); } else { - return colorToQColor(_definition.material.base); + Vector3 location, look; + Color base; + + location.x = x; + location.y = _water.height; + location.z = -y; + + look.x = 0.0; + look.y = -1.0; + look.z = 0.0; + + base = waterGetColor(&_water, &_renderer, location, look); + + if (_highlight_enabled) + { + Color mask = {0.5, 0.5, 1.0, 0.5}; + colorMask(&base, &mask); + } + + return colorToQColor(base); } } void updateData() { waterCopyDefinition(&_definition, &_water); - sceneryGetTerrain(&_terrain); - }*/ + + // TODO Do this only on full refresh + TerrainDefinition* terrain = (TerrainDefinition*)TerrainDefinitionClass.create(); + sceneryGetTerrain(terrain); + TerrainRendererClass.bind(_renderer.terrain, terrain); + TerrainDefinitionClass.destroy(terrain); + } + void toggleChangeEvent(QString key, bool value) + { + if (key == "highlight") + { + _highlight_enabled = value; + redraw(); + } + } private: + Renderer _renderer; WaterDefinition _water; - TerrainDefinition _terrain; + bool _highlight_enabled; }; class PreviewWaterColor:public BasePreview diff --git a/lib_paysages/terrain/preview.c b/lib_paysages/terrain/preview.c new file mode 100644 index 0000000..0c68067 --- /dev/null +++ b/lib_paysages/terrain/preview.c @@ -0,0 +1,95 @@ +#include "public.h" + +#include +#include "../renderer.h" +#include "../lighting.h" +#include "../textures.h" + +/* + * Terrain previews. + */ + +static TexturesDefinition _textures; +static LightingDefinition _lighting; +static int _inited = 0; + +static Color _applyTextures(Renderer* renderer, Vector3 location, double precision) +{ + return texturesGetColor(&_textures, renderer, location.x, location.z, precision); +} + +static void _getLightStatus(Renderer* renderer, LightStatus* status, Vector3 location) +{ + lightingGetStatus(&_lighting, renderer, location, status); +} + +static void _alterLight(Renderer* renderer, LightDefinition* light, Vector3 location) +{ + *light = renderer->terrain->alterLight(renderer, light, location); +} + +Renderer terrainCreatePreviewRenderer() +{ + Renderer result = rendererCreate(); + + result.render_quality = 3; + result.applyTextures = _applyTextures; + result.getLightStatus = _getLightStatus; + result.alterLight = _alterLight; + result.camera_location.x = 0.0; + result.camera_location.y = 50.0; + result.camera_location.z = 0.0; + + if (!_inited) + { + LightDefinition light; + TextureLayerDefinition* texture; + + _inited = 1; + + _lighting = lightingCreateDefinition(); + light.color.r = 0.6; + light.color.g = 0.6; + light.color.b = 0.6; + light.direction.x = -1.0; + light.direction.y = -0.5; + light.direction.z = -1.0; + light.direction = v3Normalize(light.direction); + light.filtered = 0; + light.masked = 1; + light.reflection = 1.0; + lightingAddLight(&_lighting, light); + light.color.r = 0.3; + light.color.g = 0.3; + light.color.b = 0.3; + light.direction.x = 0.5; + light.direction.y = 0.7071; + light.direction.z = 0.5; + light.filtered = 0; + light.masked = 0; + light.reflection = 0.0; + lightingAddLight(&_lighting, light); + lightingValidateDefinition(&_lighting); + + _textures = texturesCreateDefinition(); + texture = (TextureLayerDefinition*)layersGetLayer(_textures.layers, layersAddLayer(_textures.layers, NULL)); + texture->material.base = COLOR_WHITE; + texture->material.reflection = 0.3; + texture->material.shininess = 2.0; + texture->bump_height = 0.0; + texturesLayerValidateDefinition(texture); + } + + return result; +} + +Color terrainGetPreviewColor(Renderer* renderer, double x, double z, double detail) +{ + Vector3 point; + + point.x = x; + point.y = renderer->terrain->getHeight(renderer, x, z); + point.z = z; + + return renderer->terrain->getFinalColor(renderer, point, detail); +} diff --git a/lib_paysages/terrain/public.h b/lib_paysages/terrain/public.h index 0591f47..0138fad 100644 --- a/lib_paysages/terrain/public.h +++ b/lib_paysages/terrain/public.h @@ -46,8 +46,9 @@ extern StandardRenderer TerrainRendererClass; void terrainAutoPreset(TerrainDefinition* definition, TerrainPreset preset); void terrainRenderSurface(Renderer* renderer); -/*Renderer terrainCreatePreviewRenderer(); -Color terrainGetPreview(Renderer* renderer, double x, double y);*/ + +Renderer terrainCreatePreviewRenderer(); +Color terrainGetPreviewColor(Renderer* renderer, double x, double z, double detail); typedef struct {