diff --git a/src/interface/desktop/formwater.cpp b/src/interface/desktop/formwater.cpp index a1187ea..0023116 100644 --- a/src/interface/desktop/formwater.cpp +++ b/src/interface/desktop/formwater.cpp @@ -15,215 +15,11 @@ #include "BasePreview.h" #include "CameraDefinition.h" #include "WaterDefinition.h" +#include "WaterCoveragePreviewRenderer.h" +#include "WaterAspectPreviewRenderer.h" static WaterDefinition* _definition; -/**************** Previews ****************/ -class PreviewWaterCoverage : public BasePreview -{ -public: - - PreviewWaterCoverage(QWidget* parent) : BasePreview(parent) - { - _renderer = rendererCreate(); - waterAlterPreviewCoverageRenderer(_renderer); - _highlight_enabled = true; - - addOsd(QString("geolocation")); - addToggle("highlight", tr("Coverage highlight"), true); - - configScaling(20.0, 1000.0, 20.0, 200.0); - configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0); - } -protected: - - Color getColor(double x, double y) - { - return waterGetPreviewCoverage(_renderer, x, y, scaling, _highlight_enabled ? 1 : 0); - } - - void updateData() - { - WaterRendererClass.bind(_renderer, _definition); - TerrainRendererClass.bind(_renderer, RenderingScenery::getCurrent()->getTerrain()); - } - - void toggleChangeEvent(QString key, bool value) - { - if (key == "highlight") - { - _highlight_enabled = value; - redraw(); - } - } -private: - Renderer* _renderer; - bool _highlight_enabled; -}; - -class PreviewWaterColor : public BasePreview -{ -public: - - PreviewWaterColor(QWidget* parent) : BasePreview(parent) - { - _background = 0; - _lighting_enabled = false; - - _renderer = rendererCreate(); - _renderer->atmosphere->getLightingStatus = _getLightingStatus; - _renderer->rayWalking = _rayWalking; - _renderer->customData[0] = this; - //cameraSetTarget(&_renderer->render_camera, 0.0, 0.0, 0.0); - - configScaling(10.0, 1000.0, 10.0, 250.0); - //configScrolling(-30.0, 30.0, 0.0, -20.0, 20.0, 0.0); - - addChoice("bg", tr("Background"), QStringList(tr("None")) << tr("Grid") << tr("Sinusoid"), 2); - addToggle("light", tr("Light reflection"), true); - } - int _background; - bool _lighting_enabled; -protected: - - Color getColor(double x, double y) - { - Vector3 eye, look; - double target_x, target_z; - - eye.x = 0.0; - eye.y = scaling; - eye.z = -10.0 * scaling; - look.x = x * 0.01 / scaling; - look.y = -y * 0.01 / scaling - 0.3; - look.z = 1.0; - look = v3Normalize(look); - - if (look.y > -0.0001) - { - return _rayWalking(_renderer, eye, look, 0, 0, 0, 0).hit_color; - } - - target_x = eye.x - look.x * eye.y / look.y; - target_z = eye.z - look.z * eye.y / look.y; - - if (target_z > 0.0) - { - return _rayWalking(_renderer, eye, look, 0, 0, 0, 0).hit_color; - } - - return _renderer->water->getResult(_renderer, target_x, target_z).final; - } - - void cameraEvent() - { - _renderer->render_camera->setLocation(Vector3(0.0, scaling, -10.0 * scaling)); - } - - static double _getWaterHeight(Renderer*) - { - return 0.0; - } - - void updateData() - { - WaterRendererClass.bind(_renderer, _definition); - _renderer->terrain->getWaterHeight = _getWaterHeight; - } - - void choiceChangeEvent(const QString& key, int position) - { - if (key == "bg") - { - _background = position; - redraw(); - } - } - - void toggleChangeEvent(QString key, bool value) - { - if (key == "light") - { - _lighting_enabled = value; - redraw(); - } - } - -private: - Renderer* _renderer; - - static RayCastingResult _rayWalking(Renderer* renderer, Vector3 location, Vector3 direction, int, int, int, int) - { - RayCastingResult result; - PreviewWaterColor* preview = (PreviewWaterColor*) renderer->customData[0]; - double x, y; - - result.hit = 1; - if (direction.z < 0.0001) - { - result.hit_color = COLOR_WHITE; - result.hit_location = location; - } - else - { - x = location.x + direction.x * (0.0 - location.z) / direction.z; - y = location.y + direction.y * (0.0 - location.z) / direction.z; - - switch (preview->_background) - { - case 1: - result.hit_color = (((int) ceil(x * 0.2) % 2 == 0) ^ ((int) ceil(y * 0.2 - 0.5) % 2 == 0)) ? COLOR_WHITE : COLOR_BLACK; - break; - case 2: - result.hit_color = (y * 0.1 > x * 0.03 + sin(x - M_PI_2)) ? COLOR_WHITE : COLOR_BLACK; - break; - default: - result.hit_color = COLOR_WHITE; - } - result.hit_location.x = x; - result.hit_location.y = y; - result.hit_location.z = 0.0; - - if (result.hit_location.y < 0.0) - { - if (result.hit_location.y < -renderer->water->definition->lighting_depth) - { - result.hit_color = COLOR_BLACK; - } - else - { - double attenuation = -result.hit_location.y / renderer->water->definition->lighting_depth; - result.hit_color.r *= 1.0 - attenuation; - result.hit_color.g *= 1.0 - attenuation; - result.hit_color.b *= 1.0 - attenuation; - } - } - } - - return result; - } - - static void _getLightingStatus(Renderer* renderer, LightStatus* status, Vector3, int) - { - LightDefinition light; - PreviewWaterColor* preview = (PreviewWaterColor*) renderer->customData[0]; - light.color = COLOR_WHITE; - light.direction.x = 0.0; - light.direction.y = -0.4794; - light.direction.z = -0.8776; - light.altered = 0; - if (preview->_lighting_enabled) - { - light.reflection = 1.0; - } - else - { - light.reflection = 0.0; - } - lightingPushLight(status, &light); - } -}; - /**************** Form ****************/ FormWater::FormWater(QWidget *parent) : BaseForm(parent) @@ -233,10 +29,15 @@ BaseForm(parent) _definition = new WaterDefinition(NULL); - previewCoverage = new PreviewWaterCoverage(this); - previewColor = new PreviewWaterColor(this); + previewCoverage = new BasePreview(this); + previewCoverageRenderer = new WaterCoveragePreviewRenderer(_definition); addPreview(previewCoverage, tr("Coverage preview")); + previewCoverage->setRenderer(previewCoverageRenderer); + + previewColor = new BasePreview(this); + previewColorRenderer = new WaterAspectPreviewRenderer(_definition); addPreview(previewColor, tr("Rendered preview")); + previewColor->setRenderer(previewColorRenderer); //addInputDouble(tr("Height"), &_definition->height, -15.0, 15.0, 0.1, 1.0); addInputMaterial(tr("Surface material"), _definition->material); @@ -255,6 +56,17 @@ BaseForm(parent) revertConfig(); } +FormWater::~FormWater() +{ + delete _definition; + + delete previewCoverageRenderer; + delete previewCoverage; + + delete previewColorRenderer; + delete previewCoverage; +} + void FormWater::revertConfig() { RenderingScenery::getCurrent()->getWater(_definition); diff --git a/src/interface/desktop/formwater.h b/src/interface/desktop/formwater.h index c8f3b3f..fe67464 100644 --- a/src/interface/desktop/formwater.h +++ b/src/interface/desktop/formwater.h @@ -12,6 +12,7 @@ class FormWater : public BaseForm public: explicit FormWater(QWidget *parent = 0); + virtual ~FormWater(); public slots: virtual void revertConfig(); @@ -23,7 +24,10 @@ protected slots: private: BasePreview* previewCoverage; + WaterCoveragePreviewRenderer* previewCoverageRenderer; + BasePreview* previewColor; + WaterAspectPreviewRenderer* previewColorRenderer; }; #endif diff --git a/src/render/preview/Base2dPreviewRenderer.cpp b/src/render/preview/Base2dPreviewRenderer.cpp index 5e67e99..f1189f5 100644 --- a/src/render/preview/Base2dPreviewRenderer.cpp +++ b/src/render/preview/Base2dPreviewRenderer.cpp @@ -20,6 +20,10 @@ void Base2dPreviewRenderer::updateEvent() { } +void Base2dPreviewRenderer::cameraEvent(double, double, double) +{ +} + Color Base2dPreviewRenderer::getColor2D(double, double, double) { return COLOR_BLACK; diff --git a/src/render/preview/Base2dPreviewRenderer.h b/src/render/preview/Base2dPreviewRenderer.h index cc84044..0a2c784 100644 --- a/src/render/preview/Base2dPreviewRenderer.h +++ b/src/render/preview/Base2dPreviewRenderer.h @@ -20,6 +20,7 @@ public: virtual void bindEvent(BasePreview* preview); virtual void updateEvent(); + virtual void cameraEvent(double x, double y, double scaling); virtual Color getColor2D(double x, double y, double scaling); }; diff --git a/src/render/preview/BasePreview.cpp b/src/render/preview/BasePreview.cpp index f88ced3..c7b9c6b 100644 --- a/src/render/preview/BasePreview.cpp +++ b/src/render/preview/BasePreview.cpp @@ -498,6 +498,10 @@ void BasePreview::updateData() void BasePreview::cameraEvent() { + if (_renderer) + { + _renderer->cameraEvent(xoffset, yoffset, scaling); + } } Color BasePreview::getColor(double x, double y) diff --git a/src/render/preview/WaterAspectPreviewRenderer.cpp b/src/render/preview/WaterAspectPreviewRenderer.cpp new file mode 100644 index 0000000..0cc7ee8 --- /dev/null +++ b/src/render/preview/WaterAspectPreviewRenderer.cpp @@ -0,0 +1,157 @@ +#include "WaterAspectPreviewRenderer.h" + +#include "BasePreview.h" +#include "Scenery.h" +#include "WaterDefinition.h" +#include "CameraDefinition.h" + +// TEMP +#include "water/public.h" +#include "terrain/public.h" +#include "atmosphere/public.h" + +static double _getWaterHeight(Renderer*) +{ + return 0.0; +} + +static RayCastingResult _rayWalking(Renderer* renderer, Vector3 location, Vector3 direction, int, int, int, int) +{ + RayCastingResult result; + int background = *(int*)renderer->customData[1]; + double x, y; + + result.hit = 1; + if (direction.z < 0.0001) + { + result.hit_color = COLOR_WHITE; + result.hit_location = location; + } + else + { + x = location.x + direction.x * (0.0 - location.z) / direction.z; + y = location.y + direction.y * (0.0 - location.z) / direction.z; + + switch (background) + { + case 1: + result.hit_color = (((int) ceil(x * 0.2) % 2 == 0) ^ ((int) ceil(y * 0.2 - 0.5) % 2 == 0)) ? COLOR_WHITE : COLOR_BLACK; + break; + case 2: + result.hit_color = (y * 0.1 > x * 0.03 + sin(x - M_PI_2)) ? COLOR_WHITE : COLOR_BLACK; + break; + default: + result.hit_color = COLOR_WHITE; + } + result.hit_location.x = x; + result.hit_location.y = y; + result.hit_location.z = 0.0; + + if (result.hit_location.y < 0.0) + { + if (result.hit_location.y < -renderer->water->definition->lighting_depth) + { + result.hit_color = COLOR_BLACK; + } + else + { + double attenuation = -result.hit_location.y / renderer->water->definition->lighting_depth; + result.hit_color.r *= 1.0 - attenuation; + result.hit_color.g *= 1.0 - attenuation; + result.hit_color.b *= 1.0 - attenuation; + } + } + } + + return result; +} + +static void _getLightingStatus(Renderer* renderer, LightStatus* status, Vector3, int) +{ + LightDefinition light; + bool lighting = *(bool*)renderer->customData[0]; + light.color = COLOR_WHITE; + light.direction.x = 0.0; + light.direction.y = -0.4794; + light.direction.z = -0.8776; + light.altered = 0; + if (lighting) + { + light.reflection = 1.0; + } + else + { + light.reflection = 0.0; + } + lightingPushLight(status, &light); +} + +static double _getPrecision(Renderer*, Vector3) +{ + return 0.000001; +} + +WaterAspectPreviewRenderer::WaterAspectPreviewRenderer(WaterDefinition* definition): + definition(definition) +{ + lighting = true; + background = 2; + + customData[0] = &lighting; + customData[1] = &background; +} + +void WaterAspectPreviewRenderer::bindEvent(BasePreview* preview) +{ + preview->configScaling(10.0, 1000.0, 10.0, 250.0); + //configScrolling(-30.0, 30.0, 0.0, -20.0, 20.0, 0.0); + + // TODO Translation + preview->addChoice("bg", "Background", QStringList("None") << "Grid" << "Sinusoid", 2); + preview->addToggle("light", "Light reflection", true); + + //preview->configHdrToneMapping(true); +} + +void WaterAspectPreviewRenderer::updateEvent() +{ + getScenery()->setWater(definition); + getScenery()->getCamera()->setTarget(VECTOR_ZERO); + prepare(); + + terrain->getWaterHeight = _getWaterHeight; + atmosphere->getLightingStatus = _getLightingStatus; + rayWalking = _rayWalking; + getPrecision = _getPrecision; +} + +void WaterAspectPreviewRenderer::cameraEvent(double, double, double scaling) +{ + Vector3 camera_location(0.0, scaling, -10.0 * scaling); + getScenery()->getCamera()->setLocation(camera_location); + render_camera->setLocation(camera_location); +} + +Color WaterAspectPreviewRenderer::getColor2D(double x, double y, double scaling) +{ + Vector3 eye, look; + double target_x, target_z; + + eye = render_camera->getLocation(); + look = Vector3(x * 0.01 / scaling, -y * 0.01 / scaling - 0.3, 1.0).normalize(); + + if (look.y > -0.0001) + { + return _rayWalking(this, eye, look, 0, 0, 0, 0).hit_color; + } + + target_x = eye.x - look.x * eye.y / look.y; + target_z = eye.z - look.z * eye.y / look.y; + + if (target_z > 0.0) + { + return _rayWalking(this, eye, look, 0, 0, 0, 0).hit_color; + } + + return water->getResult(this, target_x, target_z).final; +} diff --git a/src/render/preview/WaterAspectPreviewRenderer.h b/src/render/preview/WaterAspectPreviewRenderer.h new file mode 100644 index 0000000..3305cf7 --- /dev/null +++ b/src/render/preview/WaterAspectPreviewRenderer.h @@ -0,0 +1,30 @@ +#ifndef WATERASPECTPREVIEWRENDERER_H +#define WATERASPECTPREVIEWRENDERER_H + +#include "preview_global.h" + +#include "Base2dPreviewRenderer.h" + +namespace paysages { +namespace preview { + +class PREVIEWSHARED_EXPORT WaterAspectPreviewRenderer : public Base2dPreviewRenderer +{ +public: + WaterAspectPreviewRenderer(WaterDefinition* definition); + + virtual void bindEvent(BasePreview* preview) override; + virtual void updateEvent() override; + virtual void cameraEvent(double x, double y, double scaling) override; + virtual Color getColor2D(double x, double y, double scaling) override; + +private: + WaterDefinition* definition; + bool lighting; + int background; +}; + +} +} + +#endif // WATERASPECTPREVIEWRENDERER_H diff --git a/src/render/preview/WaterCoveragePreviewRenderer.cpp b/src/render/preview/WaterCoveragePreviewRenderer.cpp new file mode 100644 index 0000000..315d4c8 --- /dev/null +++ b/src/render/preview/WaterCoveragePreviewRenderer.cpp @@ -0,0 +1,60 @@ +#include "WaterCoveragePreviewRenderer.h" + +#include "BasePreview.h" +#include "Scenery.h" + +// TEMP +#include "RenderingScenery.h" +#include "terrain/public.h" +#include "water/public.h" + +WaterCoveragePreviewRenderer::WaterCoveragePreviewRenderer(WaterDefinition* definition): + definition(definition) +{ + highlight = true; +} + +void WaterCoveragePreviewRenderer::bindEvent(BasePreview* preview) +{ + preview->addOsd("geolocation"); + // TODO Translation + preview->addToggle("highlight", "Coverage highlight", true); + + preview->configScaling(20.0, 1000.0, 20.0, 200.0); + preview->configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0); + // TODO Keep camera above center (for reflections) +} + +void WaterCoveragePreviewRenderer::updateEvent() +{ + getScenery()->setTerrain(RenderingScenery::getCurrent()->getTerrain()); + getScenery()->setWater(definition); + prepare(); + + terrainAlterPreviewRenderer(this); +} + +Color WaterCoveragePreviewRenderer::getColor2D(double x, double y, double scaling) +{ + double height; + + height = terrain->getHeight(this, x, y, 1); + if (height > terrain->getWaterHeight(this)) + { + return terrainGetPreviewColor(this, x, y, scaling); + } + else + { + Color base; + + base = water->getResult(this, x, y).final; + + if (highlight) + { + Color mask = {0.5, 0.5, 1.0, 0.5}; + colorMask(&base, &mask); + } + + return base; + } +} diff --git a/src/render/preview/WaterCoveragePreviewRenderer.h b/src/render/preview/WaterCoveragePreviewRenderer.h new file mode 100644 index 0000000..3b8644b --- /dev/null +++ b/src/render/preview/WaterCoveragePreviewRenderer.h @@ -0,0 +1,28 @@ +#ifndef WATERCOVERAGEPREVIEWRENDERER_H +#define WATERCOVERAGEPREVIEWRENDERER_H + +#include "preview_global.h" + +#include "Base2dPreviewRenderer.h" + +namespace paysages { +namespace preview { + +class PREVIEWSHARED_EXPORT WaterCoveragePreviewRenderer : public Base2dPreviewRenderer +{ +public: + WaterCoveragePreviewRenderer(WaterDefinition* definition); + + virtual void bindEvent(BasePreview* preview) override; + virtual void updateEvent() override; + virtual Color getColor2D(double x, double y, double scaling) override; + +private: + WaterDefinition* definition; + bool highlight; +}; + +} +} + +#endif // WATERCOVERAGEPREVIEWRENDERER_H diff --git a/src/render/preview/preview.pro b/src/render/preview/preview.pro index 6cd777e..5da08fc 100644 --- a/src/render/preview/preview.pro +++ b/src/render/preview/preview.pro @@ -19,7 +19,9 @@ SOURCES += Base2dPreviewRenderer.cpp \ DrawingWidget.cpp \ PreviewOsdItem.cpp \ CloudsCoveragePreviewRenderer.cpp \ - CloudsAspectPreviewRenderer.cpp + CloudsAspectPreviewRenderer.cpp \ + WaterCoveragePreviewRenderer.cpp \ + WaterAspectPreviewRenderer.cpp HEADERS += Base2dPreviewRenderer.h\ preview_global.h \ @@ -29,7 +31,9 @@ HEADERS += Base2dPreviewRenderer.h\ DrawingWidget.h \ PreviewOsdItem.h \ CloudsCoveragePreviewRenderer.h \ - CloudsAspectPreviewRenderer.h + CloudsAspectPreviewRenderer.h \ + WaterCoveragePreviewRenderer.h \ + WaterAspectPreviewRenderer.h unix:!symbian { maemo5 { diff --git a/src/render/preview/preview_global.h b/src/render/preview/preview_global.h index 26bb212..a5068f1 100644 --- a/src/render/preview/preview_global.h +++ b/src/render/preview/preview_global.h @@ -20,6 +20,8 @@ namespace preview { class AtmosphereColorPreviewRenderer; class CloudsCoveragePreviewRenderer; class CloudsAspectPreviewRenderer; + class WaterCoveragePreviewRenderer; + class WaterAspectPreviewRenderer; } } using namespace paysages::preview; diff --git a/src/rendering/rendering.pro b/src/rendering/rendering.pro index 5dadda9..9f895da 100644 --- a/src/rendering/rendering.pro +++ b/src/rendering/rendering.pro @@ -33,7 +33,6 @@ SOURCES += main.cpp \ tools/cache.cpp \ water/wat_render.cpp \ water/wat_raster.cpp \ - water/wat_preview.cpp \ RenderingScenery.cpp HEADERS += \ diff --git a/src/rendering/water/wat_preview.cpp b/src/rendering/water/wat_preview.cpp deleted file mode 100644 index 17a254b..0000000 --- a/src/rendering/water/wat_preview.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "private.h" - -#include "../terrain/public.h" -#include "../renderer.h" - -void waterAlterPreviewCoverageRenderer(Renderer* renderer) -{ - terrainAlterPreviewRenderer(renderer); -} - -Color waterGetPreviewCoverage(Renderer* renderer, double x, double y, double scaling, int highlight_enabled) -{ - double height; - - height = renderer->terrain->getHeight(renderer, x, y, 1); - if (height > renderer->terrain->getWaterHeight(renderer)) - { - return terrainGetPreviewColor(renderer, x, y, scaling); - } - else - { - Color base; - - base = renderer->water->getResult(renderer, x, y).final; - - if (highlight_enabled) - { - Color mask = {0.5, 0.5, 1.0, 0.5}; - colorMask(&base, &mask); - } - - return base; - } -} - -/*Renderer* waterCreatePreviewColorRenderer() -{ -} - -Color waterGetPreviewColor(Renderer* renderer, double x, double y, double scaling) -{ - -}*/