Refactored water previews

This commit is contained in:
Michaël Lemaire 2013-11-20 13:06:16 +01:00 committed by Michael Lemaire
parent ee094d9b18
commit 7fd79672be
13 changed files with 316 additions and 254 deletions

View file

@ -15,215 +15,11 @@
#include "BasePreview.h" #include "BasePreview.h"
#include "CameraDefinition.h" #include "CameraDefinition.h"
#include "WaterDefinition.h" #include "WaterDefinition.h"
#include "WaterCoveragePreviewRenderer.h"
#include "WaterAspectPreviewRenderer.h"
static WaterDefinition* _definition; 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 ****************/ /**************** Form ****************/
FormWater::FormWater(QWidget *parent) : FormWater::FormWater(QWidget *parent) :
BaseForm(parent) BaseForm(parent)
@ -233,10 +29,15 @@ BaseForm(parent)
_definition = new WaterDefinition(NULL); _definition = new WaterDefinition(NULL);
previewCoverage = new PreviewWaterCoverage(this); previewCoverage = new BasePreview(this);
previewColor = new PreviewWaterColor(this); previewCoverageRenderer = new WaterCoveragePreviewRenderer(_definition);
addPreview(previewCoverage, tr("Coverage preview")); addPreview(previewCoverage, tr("Coverage preview"));
previewCoverage->setRenderer(previewCoverageRenderer);
previewColor = new BasePreview(this);
previewColorRenderer = new WaterAspectPreviewRenderer(_definition);
addPreview(previewColor, tr("Rendered preview")); addPreview(previewColor, tr("Rendered preview"));
previewColor->setRenderer(previewColorRenderer);
//addInputDouble(tr("Height"), &_definition->height, -15.0, 15.0, 0.1, 1.0); //addInputDouble(tr("Height"), &_definition->height, -15.0, 15.0, 0.1, 1.0);
addInputMaterial(tr("Surface material"), _definition->material); addInputMaterial(tr("Surface material"), _definition->material);
@ -255,6 +56,17 @@ BaseForm(parent)
revertConfig(); revertConfig();
} }
FormWater::~FormWater()
{
delete _definition;
delete previewCoverageRenderer;
delete previewCoverage;
delete previewColorRenderer;
delete previewCoverage;
}
void FormWater::revertConfig() void FormWater::revertConfig()
{ {
RenderingScenery::getCurrent()->getWater(_definition); RenderingScenery::getCurrent()->getWater(_definition);

View file

@ -12,6 +12,7 @@ class FormWater : public BaseForm
public: public:
explicit FormWater(QWidget *parent = 0); explicit FormWater(QWidget *parent = 0);
virtual ~FormWater();
public slots: public slots:
virtual void revertConfig(); virtual void revertConfig();
@ -23,7 +24,10 @@ protected slots:
private: private:
BasePreview* previewCoverage; BasePreview* previewCoverage;
WaterCoveragePreviewRenderer* previewCoverageRenderer;
BasePreview* previewColor; BasePreview* previewColor;
WaterAspectPreviewRenderer* previewColorRenderer;
}; };
#endif #endif

View file

@ -20,6 +20,10 @@ void Base2dPreviewRenderer::updateEvent()
{ {
} }
void Base2dPreviewRenderer::cameraEvent(double, double, double)
{
}
Color Base2dPreviewRenderer::getColor2D(double, double, double) Color Base2dPreviewRenderer::getColor2D(double, double, double)
{ {
return COLOR_BLACK; return COLOR_BLACK;

View file

@ -20,6 +20,7 @@ public:
virtual void bindEvent(BasePreview* preview); virtual void bindEvent(BasePreview* preview);
virtual void updateEvent(); virtual void updateEvent();
virtual void cameraEvent(double x, double y, double scaling);
virtual Color getColor2D(double x, double y, double scaling); virtual Color getColor2D(double x, double y, double scaling);
}; };

View file

@ -498,6 +498,10 @@ void BasePreview::updateData()
void BasePreview::cameraEvent() void BasePreview::cameraEvent()
{ {
if (_renderer)
{
_renderer->cameraEvent(xoffset, yoffset, scaling);
}
} }
Color BasePreview::getColor(double x, double y) Color BasePreview::getColor(double x, double y)

View file

@ -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;
}

View file

@ -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

View file

@ -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;
}
}

View file

@ -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

View file

@ -19,7 +19,9 @@ SOURCES += Base2dPreviewRenderer.cpp \
DrawingWidget.cpp \ DrawingWidget.cpp \
PreviewOsdItem.cpp \ PreviewOsdItem.cpp \
CloudsCoveragePreviewRenderer.cpp \ CloudsCoveragePreviewRenderer.cpp \
CloudsAspectPreviewRenderer.cpp CloudsAspectPreviewRenderer.cpp \
WaterCoveragePreviewRenderer.cpp \
WaterAspectPreviewRenderer.cpp
HEADERS += Base2dPreviewRenderer.h\ HEADERS += Base2dPreviewRenderer.h\
preview_global.h \ preview_global.h \
@ -29,7 +31,9 @@ HEADERS += Base2dPreviewRenderer.h\
DrawingWidget.h \ DrawingWidget.h \
PreviewOsdItem.h \ PreviewOsdItem.h \
CloudsCoveragePreviewRenderer.h \ CloudsCoveragePreviewRenderer.h \
CloudsAspectPreviewRenderer.h CloudsAspectPreviewRenderer.h \
WaterCoveragePreviewRenderer.h \
WaterAspectPreviewRenderer.h
unix:!symbian { unix:!symbian {
maemo5 { maemo5 {

View file

@ -20,6 +20,8 @@ namespace preview {
class AtmosphereColorPreviewRenderer; class AtmosphereColorPreviewRenderer;
class CloudsCoveragePreviewRenderer; class CloudsCoveragePreviewRenderer;
class CloudsAspectPreviewRenderer; class CloudsAspectPreviewRenderer;
class WaterCoveragePreviewRenderer;
class WaterAspectPreviewRenderer;
} }
} }
using namespace paysages::preview; using namespace paysages::preview;

View file

@ -33,7 +33,6 @@ SOURCES += main.cpp \
tools/cache.cpp \ tools/cache.cpp \
water/wat_render.cpp \ water/wat_render.cpp \
water/wat_raster.cpp \ water/wat_raster.cpp \
water/wat_preview.cpp \
RenderingScenery.cpp RenderingScenery.cpp
HEADERS += \ HEADERS += \

View file

@ -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)
{
}*/