From 5be3231ab9fccad76872d0002044c37bddee6cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 13 Nov 2013 20:07:35 +0100 Subject: [PATCH] Refactoring color --- src/basics/Color.cpp | 28 ++ src/basics/Color.h | 126 ++++++++ src/basics/Color.inline.cpp | 179 +++++++++++ src/basics/ColorHSL.cpp | 88 ++++++ src/basics/ColorHSL.h | 19 ++ src/basics/basics.pro | 9 +- src/basics/basics_global.h | 1 + src/definition/BaseDefinition.cpp | 4 +- src/definition/BaseDefinition.h | 10 +- src/definition/Layers.cpp | 2 +- src/definition/Layers.h | 2 +- src/definition/LegacyLayer.cpp | 4 +- src/definition/LegacyLayer.h | 4 +- src/definition/SurfaceMaterial.cpp | 36 +++ src/definition/SurfaceMaterial.h | 34 ++ src/definition/WaterDefinition.cpp | 113 +++++++ src/definition/WaterDefinition.h | 45 +++ src/definition/definition.pro | 8 +- src/definition/definition_global.h | 9 +- src/editing/formwater.cpp | 11 +- src/editing/lighting/DialogMaterialEditor.h | 1 + src/editing/lighting/DialogMaterialEditor.ui | 2 +- src/editing/lighting/SmallPreviewHues.cpp | 1 + src/editing/terrain/mainterrainform.ui | 2 +- src/editing/terrain/widgetheightmap.cpp | 5 +- src/editing/textures/DialogTexturesLayer.ui | 2 +- src/editing/textures/maintexturesform.ui | 2 +- src/render/opengl/WidgetExplorer.cpp | 4 +- .../AtmosphereColorPreviewRenderer.cpp | 1 + src/rendering/Scenery.cpp | 17 +- src/rendering/Scenery.h | 17 +- src/rendering/clouds/public.h | 1 + src/rendering/render.cpp | 4 +- src/rendering/render.h | 2 +- src/rendering/renderer.cpp | 2 +- src/rendering/renderer.h | 1 - src/rendering/rendering_global.h | 12 +- src/rendering/textures/public.h | 1 + src/rendering/tools/color.cpp | 290 ------------------ src/rendering/tools/color.h | 57 +--- src/rendering/tools/lighting.cpp | 34 +- src/rendering/tools/lighting.h | 18 -- src/rendering/water/public.h | 22 -- src/rendering/water/wat_definition.cpp | 100 ------ src/rendering/water/wat_presets.cpp | 34 +- src/rendering/water/wat_render.cpp | 16 +- src/system/PictureFile.cpp | 4 +- src/system/PictureFile.h | 13 +- src/tests/Bruneton_Test.cpp | 4 +- src/tests/Render_Test.cpp | 2 +- 50 files changed, 779 insertions(+), 624 deletions(-) create mode 100644 src/basics/Color.cpp create mode 100644 src/basics/Color.h create mode 100644 src/basics/Color.inline.cpp create mode 100644 src/basics/ColorHSL.cpp create mode 100644 src/basics/ColorHSL.h create mode 100644 src/definition/SurfaceMaterial.cpp create mode 100644 src/definition/SurfaceMaterial.h create mode 100644 src/definition/WaterDefinition.cpp create mode 100644 src/definition/WaterDefinition.h diff --git a/src/basics/Color.cpp b/src/basics/Color.cpp new file mode 100644 index 0000000..4b8b5aa --- /dev/null +++ b/src/basics/Color.cpp @@ -0,0 +1,28 @@ +#include "Color.inline.cpp" + +#include +#include "PackStream.h" + +const Color paysages::basics::COLOR_TRANSPARENT = {0.0, 0.0, 0.0, 0.0}; +const Color paysages::basics::COLOR_BLACK = {0.0, 0.0, 0.0, 1.0}; +const Color paysages::basics::COLOR_RED = {1.0, 0.0, 0.0, 1.0}; +const Color paysages::basics::COLOR_GREEN = {0.0, 1.0, 0.0, 1.0}; +const Color paysages::basics::COLOR_BLUE = {0.0, 0.0, 1.0, 1.0}; +const Color paysages::basics::COLOR_WHITE = {1.0, 1.0, 1.0, 1.0}; +const Color paysages::basics::COLOR_GREY = {0.5, 0.5, 0.5, 1.0}; + +void Color::save(PackStream* stream) const +{ + stream->write(&r); + stream->write(&g); + stream->write(&b); + stream->write(&a); +} + +void Color::load(PackStream* stream) +{ + stream->read(&r); + stream->read(&g); + stream->read(&b); + stream->read(&a); +} diff --git a/src/basics/Color.h b/src/basics/Color.h new file mode 100644 index 0000000..78ed134 --- /dev/null +++ b/src/basics/Color.h @@ -0,0 +1,126 @@ +#ifndef COLOR_H +#define COLOR_H + +#include "basics_global.h" + +namespace paysages { +namespace basics { + +class Color +{ +public: + Color(); + Color(double r, double g, double b, double a); + + void save(PackStream* stream) const; + void load(PackStream* stream); + + unsigned int to32BitRGBA() const; + unsigned int to32BitBGRA() const; + unsigned int to32BitARGB() const; + unsigned int to32BitABGR() const; + + static Color from32BitRGBA(unsigned int col); + static Color from32BitBGRA(unsigned int col); + static Color from32BitARGB(unsigned int col); + static Color from32BitABGR(unsigned int col); + + void mask(const Color& mask); + double normalize(); + double getValue() const; + double getPower() const; + void limitPower(double max_power); + +public: + double r; + double g; + double b; + double a; +}; + +BASICSSHARED_EXPORT extern const Color COLOR_TRANSPARENT; +BASICSSHARED_EXPORT extern const Color COLOR_BLACK; +BASICSSHARED_EXPORT extern const Color COLOR_RED; +BASICSSHARED_EXPORT extern const Color COLOR_GREEN; +BASICSSHARED_EXPORT extern const Color COLOR_BLUE; +BASICSSHARED_EXPORT extern const Color COLOR_WHITE; +BASICSSHARED_EXPORT extern const Color COLOR_GREY; + +} +} + +// Inlining +#if PAYSAGES_USE_INLINING +#ifndef COLOR_INLINE_CPP +#include "Color.inline.cpp" +#endif +#endif + +// Compat API + +static inline void colorSave(PackStream* stream, Color* col) +{ + col->save(stream); +} +static inline void colorLoad(PackStream* stream, Color* col) +{ + col->load(stream); +} +static inline Color colorFromValues(double r, double g, double b, double a) +{ + return Color(r, g, b, a); +} +static inline unsigned int colorTo32BitRGBA(Color* col) +{ + return col->to32BitRGBA(); +} +static inline unsigned int colorTo32BitBGRA(Color* col) +{ + return col->to32BitBGRA(); +} +static inline unsigned int colorTo32BitARGB(Color* col) +{ + return col->to32BitARGB(); +} +static inline unsigned int colorTo32BitABGR(Color* col) +{ + return col->to32BitABGR(); +} +static inline Color colorFrom32BitRGBA(unsigned int col) +{ + return Color::from32BitRGBA(col); +} +static inline Color colorFrom32BitBGRA(unsigned int col) +{ + return Color::from32BitBGRA(col); +} +static inline Color colorFrom32BitARGB(unsigned int col) +{ + return Color::from32BitARGB(col); +} +static inline Color colorFrom32BitABGR(unsigned int col) +{ + return Color::from32BitABGR(col); +} +static inline void colorMask(Color* base, Color* mask) +{ + base->mask(*mask); +} +static inline double colorNormalize(Color* col) +{ + return col->normalize(); +} +static inline double colorGetValue(Color* col) +{ + return col->getValue(); +} +static inline double colorGetPower(Color* col) +{ + return col->getPower(); +} +static inline void colorLimitPower(Color* col, double max_power) +{ + col->limitPower(max_power); +} + +#endif // COLOR_H diff --git a/src/basics/Color.inline.cpp b/src/basics/Color.inline.cpp new file mode 100644 index 0000000..9019f73 --- /dev/null +++ b/src/basics/Color.inline.cpp @@ -0,0 +1,179 @@ +#define COLOR_INLINE_CPP + +#ifdef COLOR_H +# define METHSPEC inline +#else +# include "Color.h" +# define METHSPEC +#endif + +METHSPEC Color::Color(): + r(0.0), g(0.0), b(0.0), a(0.0) +{ +} + +METHSPEC Color::Color(double r, double g, double b, double a): + r(r), g(g), b(b), a(a) +{ + +} + +METHSPEC unsigned int Color::to32BitRGBA() const +{ + return (((unsigned int) (a * 255.0)) << 24) | (((unsigned int) (b * 255.0)) << 16) | (((unsigned int) (g * 255.0)) << 8) | ((unsigned int) (r * 255.0)); +} + +METHSPEC unsigned int Color::to32BitBGRA() const +{ + return (((unsigned int) (a * 255.0)) << 24) | (((unsigned int) (r * 255.0)) << 16) | (((unsigned int) (g * 255.0)) << 8) | ((unsigned int) (b * 255.0)); +} + +METHSPEC unsigned int Color::to32BitARGB() const +{ + return (((unsigned int) (b * 255.0)) << 24) | (((unsigned int) (g * 255.0)) << 16) | (((unsigned int) (r * 255.0)) << 8) | ((unsigned int) (a * 255.0)); +} + +METHSPEC unsigned int Color::to32BitABGR() const +{ + return (((unsigned int) (r * 255.0)) << 24) | (((unsigned int) (g * 255.0)) << 16) | (((unsigned int) (b * 255.0)) << 8) | ((unsigned int) (a * 255.0)); +} + +METHSPEC Color Color::from32BitRGBA(unsigned int col) +{ + return Color( + ((double) (col & 0x000000FF)) / 255.0, + ((double) ((col & 0x0000FF00) >> 8)) / 255.0, + ((double) ((col & 0x00FF0000) >> 16)) / 255.0, + ((double) ((col & 0xFF000000) >> 24)) / 255.0 + ); +} + +METHSPEC Color Color::from32BitBGRA(unsigned int col) +{ + return Color( + ((double) (col & 0x000000FF)) / 255.0, + ((double) ((col & 0x0000FF00) >> 8)) / 255.0, + ((double) ((col & 0x00FF0000) >> 16)) / 255.0, + ((double) ((col & 0xFF000000) >> 24)) / 255.0 + ); +} + +METHSPEC Color Color::from32BitARGB(unsigned int col) +{ + return Color( + ((double) (col & 0x000000FF)) / 255.0, + ((double) ((col & 0x0000FF00) >> 8)) / 255.0, + ((double) ((col & 0x00FF0000) >> 16)) / 255.0, + ((double) ((col & 0xFF000000) >> 24)) / 255.0 + ); +} + +METHSPEC Color Color::from32BitABGR(unsigned int col) +{ + return Color( + ((double) (col & 0x000000FF)) / 255.0, + ((double) ((col & 0x0000FF00) >> 8)) / 255.0, + ((double) ((col & 0x00FF0000) >> 16)) / 255.0, + ((double) ((col & 0xFF000000) >> 24)) / 255.0 + ); +} + +METHSPEC void Color::mask(const Color& mask) +{ + double new_a; + new_a = a + mask.a - (a * mask.a); + r = (mask.r * mask.a + r * a - r * a * mask.a) / new_a; + g = (mask.g * mask.a + g * a - g * a * mask.a) / new_a; + b = (mask.b * mask.a + b * a - b * a * mask.a) / new_a; + a = new_a; + + /*double mask_weight = mask->a; + double base_weight = 1.0 - mask_weight; + + base->r = mask->r * mask_weight + base->r * base_weight; + base->g = mask->g * mask_weight + base->g * base_weight; + base->b = mask->b * mask_weight + base->b * base_weight; + base->a = base->a + mask_weight * (1.0 - base->a);*/ +} + +METHSPEC double Color::normalize() +{ +#ifndef NDEBUG + assert(col->r >= 0.0); + assert(col->g >= 0.0); + assert(col->b >= 0.0); + assert(col->a >= 0.0); +#ifdef isnan + assert(!isnan(col->r)); + assert(!isnan(col->g)); + assert(!isnan(col->b)); + assert(!isnan(col->a)); +#endif +#ifdef isfinite + assert(isfinite(col->r)); + assert(isfinite(col->g)); + assert(isfinite(col->b)); + assert(isfinite(col->a)); +#endif +#endif + + if (r > 1.0) + { + r = 1.0; + } + if (g > 1.0) + { + g = 1.0; + } + if (b > 1.0) + { + b = 1.0; + } + return 1.0; + /*double max = colorGetValue(col); + + assert(max >= 0.0); + + if (max > 1.0) + { + col->r /= max; + col->g /= max; + col->b /= max; + } + return max;*/ +} + +METHSPEC double Color::getValue() const +{ + double max; + + max = r; + if (g > max) + { + max = g; + } + if (b > max) + { + max = b; + } + return max; +} + +METHSPEC double Color::getPower() const +{ + return r + g + b; +} + +METHSPEC void Color::limitPower(double max_power) +{ + double power = r + g + b; + + if (power > max_power) + { + double factor = max_power / power; + + r *= factor; + g *= factor; + b *= factor; + } +} diff --git a/src/basics/ColorHSL.cpp b/src/basics/ColorHSL.cpp new file mode 100644 index 0000000..056381c --- /dev/null +++ b/src/basics/ColorHSL.cpp @@ -0,0 +1,88 @@ +#include "ColorHSL.h" + +#include "Color.h" + +static double _hue2rgb(double p, double q, double t) +{ + if (t < 0.0) t += 1; + if (t > 1.0) t -= 1; + if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t; + if (t < 1.0 / 2.0) return q; + if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0; + return p; +} + +Color colorFromHSL(ColorHSL col) +{ + Color result; + + if (col.s == 0) + { + result.r = result.g = result.b = col.l; + } + else + { + double q = col.l < 0.5 ? col.l * (1.0 + col.s) : col.l + col.s - col.l * col.s; + double p = 2 * col.l - q; + result.r = _hue2rgb(p, q, col.h + 1.0 / 3.0); + result.g = _hue2rgb(p, q, col.h); + result.b = _hue2rgb(p, q, col.h - 1.0 / 3.0); + } + + result.a = col.a; + + return result; +} + +ColorHSL colorToHSL(Color col) +{ + ColorHSL result; + double min, max; + + max = col.r > col.g ? col.r : col.g; + max = col.b > max ? col.b : max; + + min = col.r < col.g ? col.r : col.g; + min = col.b < min ? col.b : min; + + result.h = result.s = result.l = (max + min) / 2.0; + + if (max == min) + { + result.h = result.s = 0.0; + } + else + { + double d = max - min; + result.s = result.l > 0.5 ? d / (2.0 - max - min) : d / (max + min); + if (max == col.r) + { + result.h = (col.g - col.b) / d + (col.g < col.b ? 6.0 : 0); + } + else if (max == col.g) + { + result.h = (col.b - col.r) / d + 2.0; + } + else + { + result.h = (col.r - col.g) / d + 4.0; + } + result.h /= 6.0; + } + + result.a = col.a; + + return result; +} + +ColorHSL colorHSLFromValues(double h, double s, double l, double a) +{ + ColorHSL result; + + result.h = h; + result.s = s; + result.l = l; + result.a = a; + + return result; +} diff --git a/src/basics/ColorHSL.h b/src/basics/ColorHSL.h new file mode 100644 index 0000000..ec185fe --- /dev/null +++ b/src/basics/ColorHSL.h @@ -0,0 +1,19 @@ +#ifndef COLORHSL_H +#define COLORHSL_H + +#include "basics_global.h" + +typedef struct +{ + double h; + double s; + double l; + double a; +} ColorHSL; + +BASICSSHARED_EXPORT Color colorFromHSL(ColorHSL col); +BASICSSHARED_EXPORT ColorHSL colorToHSL(Color col); + +BASICSSHARED_EXPORT ColorHSL colorHSLFromValues(double h, double s, double l, double a); + +#endif // COLORHSL_H diff --git a/src/basics/basics.pro b/src/basics/basics.pro index 8801948..aeb92c6 100644 --- a/src/basics/basics.pro +++ b/src/basics/basics.pro @@ -21,7 +21,10 @@ SOURCES += \ Interpolation.cpp \ Vector3.cpp \ Vector3.inline.cpp \ - SpaceSegment.cpp + SpaceSegment.cpp \ + Color.cpp \ + Color.inline.cpp \ + ColorHSL.cpp HEADERS +=\ basics_global.h \ @@ -31,7 +34,9 @@ HEADERS +=\ NoiseFunctionSimplex.h \ Interpolation.h \ Vector3.h \ - SpaceSegment.h + SpaceSegment.h \ + Color.h \ + ColorHSL.h unix:!symbian { maemo5 { diff --git a/src/basics/basics_global.h b/src/basics/basics_global.h index 0166285..2c6cfc3 100644 --- a/src/basics/basics_global.h +++ b/src/basics/basics_global.h @@ -14,6 +14,7 @@ namespace paysages { namespace basics { class Vector3; class SpaceSegment; + class Color; class NoiseGenerator; } } diff --git a/src/definition/BaseDefinition.cpp b/src/definition/BaseDefinition.cpp index 55176ad..74b7bac 100644 --- a/src/definition/BaseDefinition.cpp +++ b/src/definition/BaseDefinition.cpp @@ -29,7 +29,7 @@ void BaseDefinition::setName(QString name) this->name = name; } -void BaseDefinition::save(PackStream* pack) +void BaseDefinition::save(PackStream* pack) const { pack->write(name); QListIterator it(children); @@ -49,7 +49,7 @@ void BaseDefinition::load(PackStream* pack) } } -void BaseDefinition::copy(BaseDefinition* destination) +void BaseDefinition::copy(BaseDefinition* destination) const { destination->setName(name); // can't copy children as we don't know their types... diff --git a/src/definition/BaseDefinition.h b/src/definition/BaseDefinition.h index 1eca523..5135bc2 100644 --- a/src/definition/BaseDefinition.h +++ b/src/definition/BaseDefinition.h @@ -19,17 +19,17 @@ public: BaseDefinition(BaseDefinition* parent); virtual ~BaseDefinition(); - virtual void save(PackStream* pack); + virtual void save(PackStream* pack) const; virtual void load(PackStream* pack); - virtual void copy(BaseDefinition* destination); + virtual void copy(BaseDefinition* destination) const; virtual void validate(); - inline const QString& getName() {return name;} + inline const QString& getName() const {return name;} virtual void setName(QString name); - inline const BaseDefinition* getParent() {return parent;} - inline const BaseDefinition* getRoot() {return root;} + inline const BaseDefinition* getParent() const {return parent;} + inline const BaseDefinition* getRoot() const {return root;} protected: void addChild(BaseDefinition* child); diff --git a/src/definition/Layers.cpp b/src/definition/Layers.cpp index 0fc8623..6255d37 100644 --- a/src/definition/Layers.cpp +++ b/src/definition/Layers.cpp @@ -17,7 +17,7 @@ Layers::~Layers() delete null_layer; } -void Layers::copy(BaseDefinition* destination_) +void Layers::copy(BaseDefinition* destination_) const { Layers* destination = (Layers*)destination_; diff --git a/src/definition/Layers.h b/src/definition/Layers.h index 85136b3..178e2d4 100644 --- a/src/definition/Layers.h +++ b/src/definition/Layers.h @@ -22,7 +22,7 @@ public: Layers(BaseDefinition* parent, LayerConstructor layer_constructor, LayerType* legacy_type=0); virtual ~Layers(); - virtual void copy(BaseDefinition* destination); + virtual void copy(BaseDefinition* destination) const override; void setMaxLayerCount(int max_layer_count); diff --git a/src/definition/LegacyLayer.cpp b/src/definition/LegacyLayer.cpp index 10a992f..b5253c8 100644 --- a/src/definition/LegacyLayer.cpp +++ b/src/definition/LegacyLayer.cpp @@ -12,7 +12,7 @@ LegacyLayer::~LegacyLayer() type.callback_delete(legacy); } -void LegacyLayer::save(PackStream* pack) +void LegacyLayer::save(PackStream* pack) const { BaseDefinition::save(pack); type.callback_save(pack, legacy); @@ -24,7 +24,7 @@ void LegacyLayer::load(PackStream* pack) type.callback_load(pack, legacy); } -void LegacyLayer::copy(BaseDefinition* destination) +void LegacyLayer::copy(BaseDefinition* destination) const { BaseDefinition::copy(destination); type.callback_copy(legacy, ((LegacyLayer*)destination)->legacy); diff --git a/src/definition/LegacyLayer.h b/src/definition/LegacyLayer.h index f3d38e0..2e8a7d7 100644 --- a/src/definition/LegacyLayer.h +++ b/src/definition/LegacyLayer.h @@ -32,10 +32,10 @@ public: LegacyLayer(BaseDefinition* parent, LayerType* type); virtual ~LegacyLayer(); - virtual void save(PackStream* pack); + virtual void save(PackStream* pack) const; virtual void load(PackStream* pack); - virtual void copy(BaseDefinition* destination); + virtual void copy(BaseDefinition* destination) const; virtual void validate(); virtual void setName(QString name); diff --git a/src/definition/SurfaceMaterial.cpp b/src/definition/SurfaceMaterial.cpp new file mode 100644 index 0000000..e980538 --- /dev/null +++ b/src/definition/SurfaceMaterial.cpp @@ -0,0 +1,36 @@ +#include "SurfaceMaterial.h" + +#include "PackStream.h" + +void materialSave(PackStream* stream, SurfaceMaterial* material) +{ + stream->write(&material->base.h); + stream->write(&material->base.l); + stream->write(&material->base.s); + + stream->write(&material->hardness); + stream->write(&material->reflection); + stream->write(&material->shininess); + + stream->write(&material->receive_shadows); +} + +void materialLoad(PackStream* stream, SurfaceMaterial* material) +{ + stream->read(&material->base.h); + stream->read(&material->base.l); + stream->read(&material->base.s); + + stream->read(&material->hardness); + stream->read(&material->reflection); + stream->read(&material->shininess); + + stream->read(&material->receive_shadows); + + materialValidate(material); +} + +void materialValidate(SurfaceMaterial* material) +{ + material->_rgb = colorFromHSL(material->base); +} diff --git a/src/definition/SurfaceMaterial.h b/src/definition/SurfaceMaterial.h new file mode 100644 index 0000000..71ea441 --- /dev/null +++ b/src/definition/SurfaceMaterial.h @@ -0,0 +1,34 @@ +#ifndef SURFACEMATERIAL_H +#define SURFACEMATERIAL_H + +#include "definition_global.h" + +// TODO Change to pointers and forward declaration +#include "ColorHSL.h" +#include "Color.h" + +namespace paysages { +namespace definition { + +class DEFINITIONSHARED_EXPORT SurfaceMaterial +{ +public: + ColorHSL base; + + double hardness; + double reflection; + double shininess; + + double receive_shadows; + + Color _rgb; +}; + +} +} + +DEFINITIONSHARED_EXPORT void materialSave(PackStream* stream, SurfaceMaterial* material); +DEFINITIONSHARED_EXPORT void materialLoad(PackStream* stream, SurfaceMaterial* material); +DEFINITIONSHARED_EXPORT void materialValidate(SurfaceMaterial* material); + +#endif // SURFACEMATERIAL_H diff --git a/src/definition/WaterDefinition.cpp b/src/definition/WaterDefinition.cpp new file mode 100644 index 0000000..29dacdd --- /dev/null +++ b/src/definition/WaterDefinition.cpp @@ -0,0 +1,113 @@ +#include "WaterDefinition.h" + +#include "PackStream.h" +#include "NoiseGenerator.h" +#include "Color.h" +#include "SurfaceMaterial.h" + +WaterDefinition::WaterDefinition(BaseDefinition* parent): + BaseDefinition(parent) +{ + material = new SurfaceMaterial; + depth_color = new Color; + foam_material = new SurfaceMaterial; + _waves_noise = new NoiseGenerator; + + //waterAutoPreset(this, WATER_PRESET_LAKE); +} + +WaterDefinition::~WaterDefinition() +{ + delete material; + delete depth_color; + delete foam_material; + delete _waves_noise; +} + +void WaterDefinition::save(PackStream* stream) const +{ + BaseDefinition::save(stream); + + materialSave(stream, material); + colorSave(stream, depth_color); + stream->write(&transparency_depth); + stream->write(&transparency); + stream->write(&reflection); + stream->write(&lighting_depth); + + stream->write(&scaling); + stream->write(&waves_height); + stream->write(&detail_height); + stream->write(&turbulence); + + stream->write(&foam_coverage); + materialSave(stream, foam_material); + + _waves_noise->save(stream); +} + +void WaterDefinition::load(PackStream* stream) +{ + BaseDefinition::load(stream); + + materialLoad(stream, material); + colorLoad(stream, depth_color); + stream->read(&transparency_depth); + stream->read(&transparency); + stream->read(&reflection); + stream->read(&lighting_depth); + + stream->read(&scaling); + stream->read(&waves_height); + stream->read(&detail_height); + stream->read(&turbulence); + + stream->read(&foam_coverage); + materialLoad(stream, foam_material); + + _waves_noise->load(stream); + + validate(); +} + +void WaterDefinition::copy(BaseDefinition* _destination) const +{ + BaseDefinition::copy(_destination); + + WaterDefinition* destination = (WaterDefinition*)_destination; + *destination->material = *material; + *destination->depth_color = *depth_color; + destination->transparency_depth = transparency_depth; + destination->transparency = transparency; + destination->reflection = reflection; + destination->lighting_depth = lighting_depth; + destination->scaling = scaling; + destination->waves_height = waves_height; + destination->detail_height = detail_height; + destination->turbulence = turbulence; + destination->foam_coverage = foam_coverage; + *destination->foam_material = *foam_material; + _waves_noise->copy(destination->_waves_noise); +} + +void WaterDefinition::validate() +{ + BaseDefinition::validate(); + + double scaling = this->scaling * 0.3; + + _waves_noise->clearLevels(); + if (waves_height > 0.0) + { + _waves_noise->addLevelsSimple(2, scaling, -waves_height * scaling * 0.015, waves_height * scaling * 0.015, 0.5); + } + if (detail_height > 0.0) + { + _waves_noise->addLevelsSimple(3, scaling * 0.1, -detail_height * scaling * 0.015, detail_height * scaling * 0.015, 0.5); + } + _waves_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -turbulence, 0.0); + _waves_noise->validate(); + + materialValidate(material); + materialValidate(foam_material); +} diff --git a/src/definition/WaterDefinition.h b/src/definition/WaterDefinition.h new file mode 100644 index 0000000..18134b7 --- /dev/null +++ b/src/definition/WaterDefinition.h @@ -0,0 +1,45 @@ +#ifndef WATERDEFINITION_H +#define WATERDEFINITION_H + +#include "definition_global.h" + +#include "BaseDefinition.h" + +namespace paysages { +namespace definition { + +class WaterDefinition: public BaseDefinition +{ +public: + WaterDefinition(BaseDefinition* parent); + virtual ~WaterDefinition(); + + virtual void save(PackStream* stream) const override; + virtual void load(PackStream* stream) override; + + virtual void copy(BaseDefinition* destination) const override; + virtual void validate() override; + +public: + double transparency; + double reflection; + SurfaceMaterial* material; + Color* depth_color; + double transparency_depth; + double lighting_depth; + + double scaling; + double turbulence; + double waves_height; + double detail_height; + + double foam_coverage; + SurfaceMaterial* foam_material; + + NoiseGenerator* _waves_noise; +}; + +} +} + +#endif // WATERDEFINITION_H diff --git a/src/definition/definition.pro b/src/definition/definition.pro index dbf85a5..7a7cf2c 100644 --- a/src/definition/definition.pro +++ b/src/definition/definition.pro @@ -16,13 +16,17 @@ include(../common.pri) SOURCES += \ BaseDefinition.cpp \ Layers.cpp \ - LegacyLayer.cpp + LegacyLayer.cpp \ + WaterDefinition.cpp \ + SurfaceMaterial.cpp HEADERS +=\ definition_global.h \ BaseDefinition.h \ Layers.h \ - LegacyLayer.h + LegacyLayer.h \ + WaterDefinition.h \ + SurfaceMaterial.h unix:!symbian { maemo5 { diff --git a/src/definition/definition_global.h b/src/definition/definition_global.h index e9b7bde..a9ec2a3 100644 --- a/src/definition/definition_global.h +++ b/src/definition/definition_global.h @@ -10,9 +10,12 @@ #include "basics_global.h" -namespace paysages -{ - namespace definition {} +namespace paysages { +namespace definition { + class BaseDefinition; + class SurfaceMaterial; + class WaterDefinition; +} } using namespace paysages::definition; diff --git a/src/editing/formwater.cpp b/src/editing/formwater.cpp index 4701e4f..782296b 100644 --- a/src/editing/formwater.cpp +++ b/src/editing/formwater.cpp @@ -14,6 +14,7 @@ #include "Scenery.h" #include "BasePreview.h" #include "camera.h" +#include "WaterDefinition.h" static WaterDefinition* _definition; @@ -236,7 +237,7 @@ BaseForm(parent) addAutoPreset(tr("Lake surface")); addAutoPreset(tr("Standard sea")); - _definition = (WaterDefinition*) WaterDefinitionClass.create(); + _definition = new WaterDefinition(NULL); previewCoverage = new PreviewWaterCoverage(this); previewColor = new PreviewWaterColor(this); @@ -244,8 +245,8 @@ BaseForm(parent) addPreview(previewColor, tr("Rendered preview")); //addInputDouble(tr("Height"), &_definition->height, -15.0, 15.0, 0.1, 1.0); - addInputMaterial(tr("Surface material"), &_definition->material); - addInputColor(tr("Depth color"), &_definition->depth_color); + addInputMaterial(tr("Surface material"), _definition->material); + addInputColor(tr("Depth color"), _definition->depth_color); addInputDouble(tr("Transparency"), &_definition->transparency, 0.0, 1.0, 0.001, 0.1); addInputDouble(tr("Reflection"), &_definition->reflection, 0.0, 1.0, 0.001, 0.1); addInputDouble(tr("Transparency distance"), &_definition->transparency_depth, 0.0, 20.0, 0.1, 1.0); @@ -255,7 +256,7 @@ BaseForm(parent) addInputDouble(tr("Waves detail"), &_definition->detail_height, 0.0, 0.3, 0.003, 0.03); addInputDouble(tr("Waves turbulence"), &_definition->turbulence, 0.0, 0.5, 0.005, 0.05); addInputDouble(tr("Foam coverage"), &_definition->foam_coverage, 0.0, 1.0, 0.01, 0.1); - addInputMaterial(tr("Foam material"), &_definition->foam_material); + addInputMaterial(tr("Foam material"), _definition->foam_material); revertConfig(); } @@ -274,7 +275,7 @@ void FormWater::applyConfig() void FormWater::configChangeEvent() { - WaterDefinitionClass.validate(_definition); + _definition->validate(); BaseForm::configChangeEvent(); } diff --git a/src/editing/lighting/DialogMaterialEditor.h b/src/editing/lighting/DialogMaterialEditor.h index 92aab4c..0e23104 100644 --- a/src/editing/lighting/DialogMaterialEditor.h +++ b/src/editing/lighting/DialogMaterialEditor.h @@ -7,6 +7,7 @@ #include "tools/lighting.h" #include "renderer.h" +#include "SurfaceMaterial.h" namespace Ui { class DialogMaterialEditor; diff --git a/src/editing/lighting/DialogMaterialEditor.ui b/src/editing/lighting/DialogMaterialEditor.ui index dd0c870..3b0eea4 100644 --- a/src/editing/lighting/DialogMaterialEditor.ui +++ b/src/editing/lighting/DialogMaterialEditor.ui @@ -388,7 +388,7 @@ BasePreview QWidget -
basepreview.h
+
BasePreview.h
1
diff --git a/src/editing/lighting/SmallPreviewHues.cpp b/src/editing/lighting/SmallPreviewHues.cpp index 605d6bc..bd7a2d1 100644 --- a/src/editing/lighting/SmallPreviewHues.cpp +++ b/src/editing/lighting/SmallPreviewHues.cpp @@ -3,6 +3,7 @@ #include #include "tools.h" +#include "ColorHSL.h" SmallPreviewHues::SmallPreviewHues(QWidget* parent) : DrawingWidget(parent) { diff --git a/src/editing/terrain/mainterrainform.ui b/src/editing/terrain/mainterrainform.ui index 91b446a..b6b4958 100644 --- a/src/editing/terrain/mainterrainform.ui +++ b/src/editing/terrain/mainterrainform.ui @@ -516,7 +516,7 @@ BasePreview QWidget -
basepreview.h
+
BasePreview.h
1
diff --git a/src/editing/terrain/widgetheightmap.cpp b/src/editing/terrain/widgetheightmap.cpp index 3961f78..aeb20b7 100644 --- a/src/editing/terrain/widgetheightmap.cpp +++ b/src/editing/terrain/widgetheightmap.cpp @@ -7,7 +7,7 @@ #include #include "tools.h" #include "Scenery.h" -#include "water/public.h" +#include "WaterDefinition.h" #define HEIGHTMAP_RESOLUTION 256 @@ -29,10 +29,7 @@ QGLWidget(parent) _water = true; _wireframe = true; _painted_area = true; - WaterDefinition* water_definition = (WaterDefinition*)WaterDefinitionClass.create(); - sceneryGetWater(water_definition); _water_height = 0.0; - WaterDefinitionClass.destroy(water_definition); _average_frame_time = 0.0; diff --git a/src/editing/textures/DialogTexturesLayer.ui b/src/editing/textures/DialogTexturesLayer.ui index c190240..599eb8d 100644 --- a/src/editing/textures/DialogTexturesLayer.ui +++ b/src/editing/textures/DialogTexturesLayer.ui @@ -283,7 +283,7 @@ BasePreview QWidget -
basepreview.h
+
BasePreview.h
1
diff --git a/src/editing/textures/maintexturesform.ui b/src/editing/textures/maintexturesform.ui index 8449c3b..e703b30 100644 --- a/src/editing/textures/maintexturesform.ui +++ b/src/editing/textures/maintexturesform.ui @@ -440,7 +440,7 @@ BasePreview QWidget -
basepreview.h
+
BasePreview.h
1
diff --git a/src/render/opengl/WidgetExplorer.cpp b/src/render/opengl/WidgetExplorer.cpp index e3bada2..a84c5fe 100644 --- a/src/render/opengl/WidgetExplorer.cpp +++ b/src/render/opengl/WidgetExplorer.cpp @@ -9,6 +9,8 @@ #include "Scenery.h" #include "SoftwareRenderer.h" #include "OpenGLRenderer.h" +#include "WaterDefinition.h" +#include "SurfaceMaterial.h" #include "rendering/tools/euclid.h" #include "rendering/renderer.h" #include "rendering/camera.h" @@ -422,7 +424,7 @@ void WidgetExplorer::paintGL() // Render water double water_height = _renderer->terrain->getWaterHeight(_renderer); glDisable(GL_TEXTURE_2D); - glColor3f(water->material._rgb.r, water->material._rgb.g, water->material._rgb.b); + glColor3f(water->material->_rgb.r, water->material->_rgb.g, water->material->_rgb.b); glBegin(GL_QUADS); glVertex3f(camera_location.x - 500.0, water_height, camera_location.z - 500.0); glVertex3f(camera_location.x - 500.0, water_height, camera_location.z + 500.0); diff --git a/src/render/preview/AtmosphereColorPreviewRenderer.cpp b/src/render/preview/AtmosphereColorPreviewRenderer.cpp index 3c5d361..a71ee75 100644 --- a/src/render/preview/AtmosphereColorPreviewRenderer.cpp +++ b/src/render/preview/AtmosphereColorPreviewRenderer.cpp @@ -4,6 +4,7 @@ #include "AtmosphereRenderer.h" #include "camera.h" #include "tools/lighting.h" +#include "SurfaceMaterial.h" /* * Atmosphere previews. diff --git a/src/rendering/Scenery.cpp b/src/rendering/Scenery.cpp index 2972046..b8d738e 100644 --- a/src/rendering/Scenery.cpp +++ b/src/rendering/Scenery.cpp @@ -12,6 +12,7 @@ #include "water/public.h" #include "renderer.h" #include "terrain/ter_raster.h" +#include "WaterDefinition.h" static Scenery* _main_scenery; static SceneryCustomDataCallback _custom_save = NULL; @@ -26,17 +27,21 @@ Scenery::Scenery(): clouds = (CloudsDefinition*)CloudsDefinitionClass.create(); terrain = (TerrainDefinition*)TerrainDefinitionClass.create(); textures = (TexturesDefinition*)TexturesDefinitionClass.create(); - water = (WaterDefinition*)WaterDefinitionClass.create(); + water = new WaterDefinition(this); + + addChild(water); } Scenery::~Scenery() { + removeChild(water); + AtmosphereDefinitionClass.destroy(atmosphere); cameraDeleteDefinition(camera); CloudsDefinitionClass.destroy(clouds); TerrainDefinitionClass.destroy(terrain); TexturesDefinitionClass.destroy(textures); - WaterDefinitionClass.destroy(water); + delete water; } Scenery* Scenery::getCurrent() @@ -44,7 +49,7 @@ Scenery* Scenery::getCurrent() return _main_scenery; } -void Scenery::save(PackStream* stream) +void Scenery::save(PackStream* stream) const { BaseDefinition::save(stream); @@ -53,7 +58,6 @@ void Scenery::save(PackStream* stream) CloudsDefinitionClass.save(stream, clouds); TerrainDefinitionClass.save(stream, terrain); TexturesDefinitionClass.save(stream, textures); - WaterDefinitionClass.save(stream, water); } void Scenery::load(PackStream* stream) @@ -65,7 +69,6 @@ void Scenery::load(PackStream* stream) CloudsDefinitionClass.load(stream, clouds); TerrainDefinitionClass.load(stream, terrain); TexturesDefinitionClass.load(stream, textures); - WaterDefinitionClass.load(stream, water); } void Scenery::autoPreset(int seed) @@ -139,12 +142,12 @@ void Scenery::getTextures(TexturesDefinition* textures) void Scenery::setWater(WaterDefinition* water) { - WaterDefinitionClass.copy(water, this->water); + water->copy(this->water); } void Scenery::getWater(WaterDefinition* water) { - WaterDefinitionClass.copy(this->water, water); + this->water->copy(water); } void Scenery::bindToRenderer(Renderer* renderer) diff --git a/src/rendering/Scenery.h b/src/rendering/Scenery.h index 5cdad94..f30d95c 100644 --- a/src/rendering/Scenery.h +++ b/src/rendering/Scenery.h @@ -10,7 +10,6 @@ class CameraDefinition; class CloudsDefinition; class TerrainDefinition; class TexturesDefinition; -class WaterDefinition; class Renderer; namespace paysages { @@ -32,33 +31,33 @@ public: static Scenery* getCurrent(); - virtual void save(PackStream* stream); - virtual void load(PackStream* stream); + virtual void save(PackStream* stream) const override; + virtual void load(PackStream* stream) override; void autoPreset(int seed); void setAtmosphere(AtmosphereDefinition* atmosphere); - inline AtmosphereDefinition* getAtmosphere() {return atmosphere;} + inline AtmosphereDefinition* getAtmosphere() const {return atmosphere;} void getAtmosphere(AtmosphereDefinition* atmosphere); void setCamera(CameraDefinition* camera); - inline CameraDefinition* getCamera() {return camera;} + inline CameraDefinition* getCamera() const {return camera;} void getCamera(CameraDefinition* camera); void setClouds(CloudsDefinition* clouds); - inline CloudsDefinition* getClouds() {return clouds;} + inline CloudsDefinition* getClouds() const {return clouds;} void getClouds(CloudsDefinition* clouds); void setTerrain(TerrainDefinition* terrain); - inline TerrainDefinition* getTerrain() {return terrain;} + inline TerrainDefinition* getTerrain() const {return terrain;} void getTerrain(TerrainDefinition* terrain); void setTextures(TexturesDefinition* textures); - inline TexturesDefinition* getTextures() {return textures;} + inline TexturesDefinition* getTextures() const {return textures;} void getTextures(TexturesDefinition* textures); void setWater(WaterDefinition* water); - inline WaterDefinition* getWater() {return water;} + inline WaterDefinition* getWater() const {return water;} void getWater(WaterDefinition* water); void bindToRenderer(Renderer* renderer); diff --git a/src/rendering/clouds/public.h b/src/rendering/clouds/public.h index dc4b1eb..16b5bb7 100644 --- a/src/rendering/clouds/public.h +++ b/src/rendering/clouds/public.h @@ -7,6 +7,7 @@ #include "../tools/curve.h" #include "../tools/euclid.h" #include "Layers.h" +#include "SurfaceMaterial.h" namespace paysages { namespace basics { diff --git a/src/rendering/render.cpp b/src/rendering/render.cpp index 1ad7316..d0f5090 100644 --- a/src/rendering/render.cpp +++ b/src/rendering/render.cpp @@ -178,9 +178,9 @@ void renderSetToneMapping(RenderArea* area, ToneMappingOperator tonemapper, doub renderUpdate(area); } -void renderSetBackgroundColor(RenderArea* area, Color* col) +void renderSetBackgroundColor(RenderArea* area, const Color &col) { - area->background_color = *col; + area->background_color = col; } void renderClear(RenderArea* area) diff --git a/src/rendering/render.h b/src/rendering/render.h index fe6b85e..0961453 100644 --- a/src/rendering/render.h +++ b/src/rendering/render.h @@ -30,7 +30,7 @@ RENDERINGSHARED_EXPORT void renderDeleteArea(RenderArea* area); RENDERINGSHARED_EXPORT void renderSetParams(RenderArea* area, RenderParams params); RENDERINGSHARED_EXPORT void renderSetToneMapping(RenderArea* area, ToneMappingOperator tonemapper, double exposure); -RENDERINGSHARED_EXPORT void renderSetBackgroundColor(RenderArea* area, Color* col); +RENDERINGSHARED_EXPORT void renderSetBackgroundColor(RenderArea* area, const Color& col); RENDERINGSHARED_EXPORT void renderClear(RenderArea* area); RENDERINGSHARED_EXPORT void renderUpdate(RenderArea* area); diff --git a/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index 28c0752..ae6d91f 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -225,7 +225,7 @@ void rendererStart(Renderer* renderer, RenderParams params) cameraSetRenderSize(renderer->render_camera, renderer->render_width, renderer->render_height); - renderSetBackgroundColor(renderer->render_area, &COLOR_BLACK); + renderSetBackgroundColor(renderer->render_area, COLOR_BLACK); renderSetParams(renderer->render_area, params); renderClear(renderer->render_area); diff --git a/src/rendering/renderer.h b/src/rendering/renderer.h index f9df721..48012c2 100644 --- a/src/rendering/renderer.h +++ b/src/rendering/renderer.h @@ -7,7 +7,6 @@ class CameraDefinition; class LightingManager; -class SurfaceMaterial; class AtmosphereRenderer; class TerrainRenderer; class TexturesRenderer; diff --git a/src/rendering/rendering_global.h b/src/rendering/rendering_global.h index d9bbe60..3225823 100644 --- a/src/rendering/rendering_global.h +++ b/src/rendering/rendering_global.h @@ -1,7 +1,6 @@ #ifndef RENDERING_GLOBAL_H #define RENDERING_GLOBAL_H -/* Shared object helpers */ #include #if defined(RENDERING_LIBRARY) # define RENDERINGSHARED_EXPORT Q_DECL_EXPORT @@ -9,15 +8,6 @@ # define RENDERINGSHARED_EXPORT Q_DECL_IMPORT #endif -/* Namespace using */ -namespace paysages -{ - namespace system {} - namespace basics {} - namespace definition {} -} -using namespace paysages::system; -using namespace paysages::basics; -using namespace paysages::definition; +#include "definition_global.h" #endif // RENDERING_GLOBAL_H diff --git a/src/rendering/textures/public.h b/src/rendering/textures/public.h index 8eb6094..d5dad59 100644 --- a/src/rendering/textures/public.h +++ b/src/rendering/textures/public.h @@ -6,6 +6,7 @@ #include "tools/zone.h" #include "tools/lighting.h" #include "terrain/public.h" +#include "SurfaceMaterial.h" #define TEXTURES_MAX_LAYERS 50 diff --git a/src/rendering/tools/color.cpp b/src/rendering/tools/color.cpp index eaa2cca..3be661f 100644 --- a/src/rendering/tools/color.cpp +++ b/src/rendering/tools/color.cpp @@ -7,210 +7,6 @@ #include "../tools.h" #include "PackStream.h" -/******************************** Color ********************************/ - -Color COLOR_TRANSPARENT = {0.0, 0.0, 0.0, 0.0}; -Color COLOR_BLACK = {0.0, 0.0, 0.0, 1.0}; -Color COLOR_RED = {1.0, 0.0, 0.0, 1.0}; -Color COLOR_GREEN = {0.0, 1.0, 0.0, 1.0}; -Color COLOR_BLUE = {0.0, 0.0, 1.0, 1.0}; -Color COLOR_WHITE = {1.0, 1.0, 1.0, 1.0}; -Color COLOR_GREY = {0.5, 0.5, 0.5, 1.0}; - -void colorSave(PackStream* stream, Color* col) -{ - stream->write(&col->r); - stream->write(&col->g); - stream->write(&col->b); - stream->write(&col->a); -} - -void colorLoad(PackStream* stream, Color* col) -{ - stream->read(&col->r); - stream->read(&col->g); - stream->read(&col->b); - stream->read(&col->a); -} - -Color colorFromValues(double r, double g, double b, double a) -{ - Color result; - - result.r = r; - result.g = g; - result.b = b; - result.a = a; - - return result; -} - -unsigned int colorTo32BitRGBA(Color* col) -{ - return (((unsigned int) (col->a * 255.0)) << 24) | (((unsigned int) (col->b * 255.0)) << 16) | (((unsigned int) (col->g * 255.0)) << 8) | ((unsigned int) (col->r * 255.0)); -} - -unsigned int colorTo32BitBGRA(Color* col) -{ - return (((unsigned int) (col->a * 255.0)) << 24) | (((unsigned int) (col->r * 255.0)) << 16) | (((unsigned int) (col->g * 255.0)) << 8) | ((unsigned int) (col->b * 255.0)); -} - -unsigned int colorTo32BitARGB(Color* col) -{ - return (((unsigned int) (col->b * 255.0)) << 24) | (((unsigned int) (col->g * 255.0)) << 16) | (((unsigned int) (col->r * 255.0)) << 8) | ((unsigned int) (col->a * 255.0)); -} - -unsigned int colorTo32BitABGR(Color* col) -{ - return (((unsigned int) (col->r * 255.0)) << 24) | (((unsigned int) (col->g * 255.0)) << 16) | (((unsigned int) (col->b * 255.0)) << 8) | ((unsigned int) (col->a * 255.0)); -} - -Color colorFrom32BitRGBA(unsigned int col) -{ - Color result; - - result.r = ((double) (col & 0x000000FF)) / 255.0; - result.g = ((double) ((col & 0x0000FF00) >> 8)) / 255.0; - result.b = ((double) ((col & 0x00FF0000) >> 16)) / 255.0; - result.a = ((double) ((col & 0xFF000000) >> 24)) / 255.0; - - return result; -} - -Color colorFrom32BitBGRA(unsigned int col) -{ - Color result; - - result.b = ((double) (col & 0x000000FF)) / 255.0; - result.g = ((double) ((col & 0x0000FF00) >> 8)) / 255.0; - result.r = ((double) ((col & 0x00FF0000) >> 16)) / 255.0; - result.a = ((double) ((col & 0xFF000000) >> 24)) / 255.0; - - return result; -} - -Color colorFrom32BitARGB(unsigned int col) -{ - Color result; - - result.a = ((double) (col & 0x000000FF)) / 255.0; - result.r = ((double) ((col & 0x0000FF00) >> 8)) / 255.0; - result.g = ((double) ((col & 0x00FF0000) >> 16)) / 255.0; - result.b = ((double) ((col & 0xFF000000) >> 24)) / 255.0; - - return result; -} - -Color colorFrom32BitABGR(unsigned int col) -{ - Color result; - - result.a = ((double) (col & 0x000000FF)) / 255.0; - result.b = ((double) ((col & 0x0000FF00) >> 8)) / 255.0; - result.g = ((double) ((col & 0x00FF0000) >> 16)) / 255.0; - result.r = ((double) ((col & 0xFF000000) >> 24)) / 255.0; - - return result; -} - -void colorMask(Color* base, Color* mask) -{ - double new_a; - new_a = base->a + mask->a - (base->a * mask->a); - base->r = (mask->r * mask->a + base->r * base->a - base->r * base->a * mask->a) / new_a; - base->g = (mask->g * mask->a + base->g * base->a - base->g * base->a * mask->a) / new_a; - base->b = (mask->b * mask->a + base->b * base->a - base->b * base->a * mask->a) / new_a; - base->a = new_a; - - /*double mask_weight = mask->a; - double base_weight = 1.0 - mask_weight; - - base->r = mask->r * mask_weight + base->r * base_weight; - base->g = mask->g * mask_weight + base->g * base_weight; - base->b = mask->b * mask_weight + base->b * base_weight; - base->a = base->a + mask_weight * (1.0 - base->a);*/ -} - -double colorNormalize(Color* col) -{ - assert(col->r >= 0.0); - assert(col->g >= 0.0); - assert(col->b >= 0.0); - assert(col->a >= 0.0); -#ifdef isnan - assert(!isnan(col->r)); - assert(!isnan(col->g)); - assert(!isnan(col->b)); - assert(!isnan(col->a)); -#endif -#ifdef isfinite - assert(isfinite(col->r)); - assert(isfinite(col->g)); - assert(isfinite(col->b)); - assert(isfinite(col->a)); -#endif - - if (col->r > 1.0) - { - col->r = 1.0; - } - if (col->g > 1.0) - { - col->g = 1.0; - } - if (col->b > 1.0) - { - col->b = 1.0; - } - return 1.0; - /*double max = colorGetValue(col); - - assert(max >= 0.0); - - if (max > 1.0) - { - col->r /= max; - col->g /= max; - col->b /= max; - } - return max;*/ -} - -double colorGetValue(Color* col) -{ - double max; - - max = col->r; - if (col->g > max) - { - max = col->g; - } - if (col->b > max) - { - max = col->b; - } - return max; -} - -double colorGetPower(Color* col) -{ - return col->r + col->g + col->b; -} - -void colorLimitPower(Color* col, double max_power) -{ - double power = colorGetPower(col); - - if (power > max_power) - { - double factor = max_power / power; - - col->r *= factor; - col->g *= factor; - col->b *= factor; - } -} - /******************************** ColorProfile ********************************/ class ColorProfile { @@ -454,89 +250,3 @@ Color colorGradationGet(ColorGradation* gradation, double value) return result; } - -/******************************** HSL Color Space ********************************/ -static double _hue2rgb(double p, double q, double t) -{ - if (t < 0.0) t += 1; - if (t > 1.0) t -= 1; - if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t; - if (t < 1.0 / 2.0) return q; - if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0; - return p; -} - -Color colorFromHSL(ColorHSL col) -{ - Color result; - - if (col.s == 0) - { - result.r = result.g = result.b = col.l; - } - else - { - double q = col.l < 0.5 ? col.l * (1.0 + col.s) : col.l + col.s - col.l * col.s; - double p = 2 * col.l - q; - result.r = _hue2rgb(p, q, col.h + 1.0 / 3.0); - result.g = _hue2rgb(p, q, col.h); - result.b = _hue2rgb(p, q, col.h - 1.0 / 3.0); - } - - result.a = col.a; - - return result; -} - -ColorHSL colorToHSL(Color col) -{ - ColorHSL result; - double min, max; - - max = col.r > col.g ? col.r : col.g; - max = col.b > max ? col.b : max; - - min = col.r < col.g ? col.r : col.g; - min = col.b < min ? col.b : min; - - result.h = result.s = result.l = (max + min) / 2.0; - - if (max == min) - { - result.h = result.s = 0.0; - } - else - { - double d = max - min; - result.s = result.l > 0.5 ? d / (2.0 - max - min) : d / (max + min); - if (max == col.r) - { - result.h = (col.g - col.b) / d + (col.g < col.b ? 6.0 : 0); - } - else if (max == col.g) - { - result.h = (col.b - col.r) / d + 2.0; - } - else - { - result.h = (col.r - col.g) / d + 4.0; - } - result.h /= 6.0; - } - - result.a = col.a; - - return result; -} - -ColorHSL colorHSLFromValues(double h, double s, double l, double a) -{ - ColorHSL result; - - result.h = h; - result.s = s; - result.l = l; - result.a = a; - - return result; -} diff --git a/src/rendering/tools/color.h b/src/rendering/tools/color.h index ed4b15f..7819497 100644 --- a/src/rendering/tools/color.h +++ b/src/rendering/tools/color.h @@ -4,49 +4,6 @@ #include "../rendering_global.h" #include "curve.h" -namespace paysages { -namespace system {class PackStream;} -} - -#define TYPEDEF_COLOR -typedef struct -{ - double r; - double g; - double b; - double a; -} Color; - -RENDERINGSHARED_EXPORT extern Color COLOR_TRANSPARENT; -RENDERINGSHARED_EXPORT extern Color COLOR_BLACK; -RENDERINGSHARED_EXPORT extern Color COLOR_RED; -RENDERINGSHARED_EXPORT extern Color COLOR_GREEN; -RENDERINGSHARED_EXPORT extern Color COLOR_BLUE; -RENDERINGSHARED_EXPORT extern Color COLOR_WHITE; -RENDERINGSHARED_EXPORT extern Color COLOR_GREY; - -/* Color */ -RENDERINGSHARED_EXPORT void colorSave(PackStream* stream, Color* col); -RENDERINGSHARED_EXPORT void colorLoad(PackStream* stream, Color* col); -RENDERINGSHARED_EXPORT Color colorFromValues(double r, double g, double b, double a); - -RENDERINGSHARED_EXPORT unsigned int colorTo32BitRGBA(Color* col); -RENDERINGSHARED_EXPORT unsigned int colorTo32BitBGRA(Color* col); -RENDERINGSHARED_EXPORT unsigned int colorTo32BitARGB(Color* col); -RENDERINGSHARED_EXPORT unsigned int colorTo32BitABGR(Color* col); - -RENDERINGSHARED_EXPORT Color colorFrom32BitRGBA(unsigned int col); -RENDERINGSHARED_EXPORT Color colorFrom32BitBGRA(unsigned int col); -RENDERINGSHARED_EXPORT Color colorFrom32BitARGB(unsigned int col); -RENDERINGSHARED_EXPORT Color colorFrom32BitABGR(unsigned int col); - -RENDERINGSHARED_EXPORT void colorMask(Color* base, Color* mask); -RENDERINGSHARED_EXPORT double colorNormalize(Color* col); -RENDERINGSHARED_EXPORT double colorGetValue(Color* col); - -RENDERINGSHARED_EXPORT double colorGetPower(Color* col); -RENDERINGSHARED_EXPORT void colorLimitPower(Color* col, double max_power); - /* HDR profile for tone-mapping */ class ColorProfile; typedef enum @@ -92,18 +49,8 @@ RENDERINGSHARED_EXPORT void colorGradationQuickAddRgb(ColorGradation* gradation, RENDERINGSHARED_EXPORT Color colorGradationGet(ColorGradation* gradation, double value); -/* HSL color space */ -typedef struct -{ - double h; - double s; - double l; - double a; -} ColorHSL; -RENDERINGSHARED_EXPORT Color colorFromHSL(ColorHSL col); -RENDERINGSHARED_EXPORT ColorHSL colorToHSL(Color col); - -RENDERINGSHARED_EXPORT ColorHSL colorHSLFromValues(double h, double s, double l, double a); +// TEMP +#include "Color.h" #endif diff --git a/src/rendering/tools/lighting.cpp b/src/rendering/tools/lighting.cpp index 4ecc5dd..ca9db29 100644 --- a/src/rendering/tools/lighting.cpp +++ b/src/rendering/tools/lighting.cpp @@ -5,6 +5,7 @@ #include #include #include "PackStream.h" +#include "SurfaceMaterial.h" #define MAX_CALLBACK_COUNT 10 #define MAX_LIGHT_COUNT 30 @@ -214,36 +215,3 @@ Vector3 lightingGetStatusLocation(LightStatus* status) { return status->location; } - -void materialSave(PackStream* stream, SurfaceMaterial* material) -{ - stream->write(&material->base.h); - stream->write(&material->base.l); - stream->write(&material->base.s); - - stream->write(&material->hardness); - stream->write(&material->reflection); - stream->write(&material->shininess); - - stream->write(&material->receive_shadows); -} - -void materialLoad(PackStream* stream, SurfaceMaterial* material) -{ - stream->read(&material->base.h); - stream->read(&material->base.l); - stream->read(&material->base.s); - - stream->read(&material->hardness); - stream->read(&material->reflection); - stream->read(&material->shininess); - - stream->read(&material->receive_shadows); - - materialValidate(material); -} - -void materialValidate(SurfaceMaterial* material) -{ - material->_rgb = colorFromHSL(material->base); -} diff --git a/src/rendering/tools/lighting.h b/src/rendering/tools/lighting.h index 06a6657..f797147 100644 --- a/src/rendering/tools/lighting.h +++ b/src/rendering/tools/lighting.h @@ -9,20 +9,6 @@ namespace paysages { namespace system {class PackStream;} } -class SurfaceMaterial -{ -public: - ColorHSL base; - - double hardness; - double reflection; - double shininess; - - double receive_shadows; - - Color _rgb; -}; - typedef struct { Vector3 direction; /* Global direction of the light */ @@ -49,8 +35,4 @@ RENDERINGSHARED_EXPORT Vector3 lightingGetStatusLocation(LightStatus* status); RENDERINGSHARED_EXPORT Color lightingApplyOneLight(LightDefinition* light, Vector3 eye, Vector3 location, Vector3 normal, SurfaceMaterial* material); -RENDERINGSHARED_EXPORT void materialSave(PackStream* stream, SurfaceMaterial* material); -RENDERINGSHARED_EXPORT void materialLoad(PackStream* stream, SurfaceMaterial* material); -RENDERINGSHARED_EXPORT void materialValidate(SurfaceMaterial* material); - #endif diff --git a/src/rendering/water/public.h b/src/rendering/water/public.h index 1921b0e..16ed74f 100644 --- a/src/rendering/water/public.h +++ b/src/rendering/water/public.h @@ -19,27 +19,6 @@ typedef enum WATER_PRESET_SEA } WaterPreset; -class WaterDefinition -{ -public: - double transparency; - double reflection; - SurfaceMaterial material; - Color depth_color; - double transparency_depth; - double lighting_depth; - - double scaling; - double turbulence; - double waves_height; - double detail_height; - - double foam_coverage; - SurfaceMaterial foam_material; - - NoiseGenerator* _waves_noise; -}; - typedef struct { Vector3 location; @@ -65,7 +44,6 @@ public: }; -RENDERINGSHARED_EXPORT extern StandardDefinition WaterDefinitionClass; RENDERINGSHARED_EXPORT extern StandardRenderer WaterRendererClass; RENDERINGSHARED_EXPORT void waterRenderSurface(Renderer* renderer); diff --git a/src/rendering/water/wat_definition.cpp b/src/rendering/water/wat_definition.cpp index f055640..4036a20 100644 --- a/src/rendering/water/wat_definition.cpp +++ b/src/rendering/water/wat_definition.cpp @@ -1,101 +1 @@ #include "private.h" - -#include "PackStream.h" -#include "NoiseGenerator.h" - -static void _validateDefinition(WaterDefinition* definition) -{ - double scaling = definition->scaling * 0.3; - definition->_waves_noise->clearLevels(); - if (definition->waves_height > 0.0) - { - definition->_waves_noise->addLevelsSimple(2, scaling, -definition->waves_height * scaling * 0.015, definition->waves_height * scaling * 0.015, 0.5); - } - if (definition->detail_height > 0.0) - { - definition->_waves_noise->addLevelsSimple(3, scaling * 0.1, -definition->detail_height * scaling * 0.015, definition->detail_height * scaling * 0.015, 0.5); - } - definition->_waves_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -definition->turbulence, 0.0); - definition->_waves_noise->validate(); - - materialValidate(&definition->material); - materialValidate(&definition->foam_material); -} - -static WaterDefinition* _createDefinition() -{ - WaterDefinition* definition = new WaterDefinition; - - definition->_waves_noise = new NoiseGenerator(); - - waterAutoPreset(definition, WATER_PRESET_LAKE); - - return definition; -} - -static void _deleteDefinition(WaterDefinition* definition) -{ - delete definition->_waves_noise; - delete definition; -} - -static void _copyDefinition(WaterDefinition* source, WaterDefinition* destination) -{ - NoiseGenerator* noise; - - noise = destination->_waves_noise; - *destination = *source; - destination->_waves_noise = noise; - source->_waves_noise->copy(destination->_waves_noise); -} - -static void _saveDefinition(PackStream* stream, WaterDefinition* definition) -{ - materialSave(stream, &definition->material); - colorSave(stream, &definition->depth_color); - stream->write(&definition->transparency_depth); - stream->write(&definition->transparency); - stream->write(&definition->reflection); - stream->write(&definition->lighting_depth); - - stream->write(&definition->scaling); - stream->write(&definition->waves_height); - stream->write(&definition->detail_height); - stream->write(&definition->turbulence); - - stream->write(&definition->foam_coverage); - materialSave(stream, &definition->foam_material); - - definition->_waves_noise->save(stream); -} - -static void _loadDefinition(PackStream* stream, WaterDefinition* definition) -{ - materialLoad(stream, &definition->material); - colorLoad(stream, &definition->depth_color); - stream->read(&definition->transparency_depth); - stream->read(&definition->transparency); - stream->read(&definition->reflection); - stream->read(&definition->lighting_depth); - - stream->read(&definition->scaling); - stream->read(&definition->waves_height); - stream->read(&definition->detail_height); - stream->read(&definition->turbulence); - - stream->read(&definition->foam_coverage); - materialLoad(stream, &definition->foam_material); - - definition->_waves_noise->load(stream); - - _validateDefinition(definition); -} - -StandardDefinition WaterDefinitionClass = { - (FuncObjectCreate)_createDefinition, - (FuncObjectDelete)_deleteDefinition, - (FuncObjectCopy)_copyDefinition, - (FuncObjectValidate)_validateDefinition, - (FuncObjectSave)_saveDefinition, - (FuncObjectLoad)_loadDefinition -}; diff --git a/src/rendering/water/wat_presets.cpp b/src/rendering/water/wat_presets.cpp index 4565dbb..96edf79 100644 --- a/src/rendering/water/wat_presets.cpp +++ b/src/rendering/water/wat_presets.cpp @@ -1,5 +1,7 @@ #include "private.h" +#include "WaterDefinition.h" +#include "SurfaceMaterial.h" #include "NoiseGenerator.h" void waterAutoPreset(WaterDefinition* definition, WaterPreset preset) @@ -11,10 +13,10 @@ void waterAutoPreset(WaterDefinition* definition, WaterPreset preset) definition->transparency = 0.5; definition->reflection = 0.4; definition->transparency_depth = 4.0; - definition->material.base = colorToHSL(colorFromValues(0.08, 0.15, 0.2, 1.0)); - definition->depth_color.r = 0.0; - definition->depth_color.g = 0.1; - definition->depth_color.b = 0.1; + definition->material->base = colorToHSL(colorFromValues(0.08, 0.15, 0.2, 1.0)); + definition->depth_color->r = 0.0; + definition->depth_color->g = 0.1; + definition->depth_color->b = 0.1; definition->lighting_depth = 6.0; definition->scaling = 1.0; definition->waves_height = 0.8; @@ -27,10 +29,10 @@ void waterAutoPreset(WaterDefinition* definition, WaterPreset preset) definition->transparency = 0.4; definition->reflection = 0.35; definition->transparency_depth = 3.0; - definition->material.base = colorToHSL(colorFromValues(0.05, 0.18, 0.2, 1.0)); - definition->depth_color.r = 0.0; - definition->depth_color.g = 0.18; - definition->depth_color.b = 0.15; + definition->material->base = colorToHSL(colorFromValues(0.05, 0.18, 0.2, 1.0)); + definition->depth_color->r = 0.0; + definition->depth_color->g = 0.18; + definition->depth_color->b = 0.15; definition->lighting_depth = 4.0; definition->scaling = 1.5; definition->waves_height = 1.0; @@ -39,13 +41,13 @@ void waterAutoPreset(WaterDefinition* definition, WaterPreset preset) definition->foam_coverage = 0.4; } - definition->depth_color.a = 1.0; - definition->material.base.a = 1.0; - definition->material.reflection = 1.0; - definition->material.shininess = 16.0; - definition->foam_material.base = colorToHSL(colorFromValues(0.8, 0.8, 0.8, 1.0)); - definition->foam_material.reflection = 0.1; - definition->foam_material.shininess = 1.5; + definition->depth_color->a = 1.0; + definition->material->base.a = 1.0; + definition->material->reflection = 1.0; + definition->material->shininess = 16.0; + definition->foam_material->base = colorToHSL(colorFromValues(0.8, 0.8, 0.8, 1.0)); + definition->foam_material->reflection = 0.1; + definition->foam_material->shininess = 1.5; - WaterDefinitionClass.validate(definition); + definition->validate(); } diff --git a/src/rendering/water/wat_render.cpp b/src/rendering/water/wat_render.cpp index dce4c96..f86260a 100644 --- a/src/rendering/water/wat_render.cpp +++ b/src/rendering/water/wat_render.cpp @@ -3,6 +3,8 @@ #include #include "../tools.h" #include "../renderer.h" +#include "WaterDefinition.h" +#include "SurfaceMaterial.h" #include "NoiseGenerator.h" #include "terrain/public.h" @@ -143,7 +145,7 @@ static inline Color _getFoamMask(Renderer* renderer, WaterDefinition* definition foam_factor = (foam_factor - (1.0 - definition->foam_coverage)) * definition->foam_coverage; /* TODO Re-use base lighting status */ - result = renderer->applyLightingToSurface(renderer, location, normal, &definition->foam_material); + result = renderer->applyLightingToSurface(renderer, location, normal, definition->foam_material); result.r *= 2.0; result.g *= 2.0; result.b *= 2.0; @@ -260,7 +262,7 @@ static WaterResult _realGetResult(Renderer* renderer, double x, double z) } else { - Color depth_color = definition->depth_color; + Color depth_color = *definition->depth_color; refracted = renderer->rayWalking(renderer, location, _refractRay(look_direction, normal), 1, 0, 1, 1); depth = v3Norm(v3Sub(location, refracted.hit_location)); colorLimitPower(&depth_color, colorGetPower(&refracted.hit_color)); @@ -279,7 +281,7 @@ static WaterResult _realGetResult(Renderer* renderer, double x, double z) } /* Lighting from environment */ - color = renderer->applyLightingToSurface(renderer, location, normal, &definition->material); + color = renderer->applyLightingToSurface(renderer, location, normal, definition->material); color.r += result.reflected.r * definition->reflection + result.refracted.r * definition->transparency; color.g += result.reflected.g * definition->reflection + result.refracted.g * definition->transparency; @@ -292,7 +294,7 @@ static WaterResult _realGetResult(Renderer* renderer, double x, double z) /* Bring color to the camera */ color = renderer->applyMediumTraversal(location, color); - result.base = definition->material._rgb; + result.base = definition->material->_rgb; result.final = color; return result; @@ -304,7 +306,7 @@ static WaterRenderer* _createRenderer() WaterRenderer* result; result = new WaterRenderer; - result->definition = (WaterDefinition*)WaterDefinitionClass.create(); + result->definition = new WaterDefinition(NULL); result->getHeightInfo = _fakeGetHeightInfo; result->getHeight = _fakeGetHeight; @@ -315,13 +317,13 @@ static WaterRenderer* _createRenderer() static void _deleteRenderer(WaterRenderer* renderer) { - WaterDefinitionClass.destroy(renderer->definition); + delete renderer->definition; delete renderer; } static void _bindRenderer(Renderer* renderer, WaterDefinition* definition) { - WaterDefinitionClass.copy(definition, renderer->water->definition); + definition->copy(renderer->water->definition); renderer->water->getHeightInfo = _realGetHeightInfo; renderer->water->getHeight = _realGetHeight; diff --git a/src/system/PictureFile.cpp b/src/system/PictureFile.cpp index 4db268d..322b566 100644 --- a/src/system/PictureFile.cpp +++ b/src/system/PictureFile.cpp @@ -17,9 +17,7 @@ int systemSavePictureFile(const char* filepath, PictureCallbackSavePixel callbac { for (int x = 0; x < width; x++) { - Color pixel = callback_pixel(data, x, y); - QColor pixelconv = QColor::fromRgbF(pixel.r, pixel.g, pixel.b, pixel.a); - result.setPixel(x, height - 1 - y, pixelconv.rgba()); + result.setPixel(x, height - 1 - y, callback_pixel(data, x, y)); } } diff --git a/src/system/PictureFile.h b/src/system/PictureFile.h index 86dee6f..367d906 100644 --- a/src/system/PictureFile.h +++ b/src/system/PictureFile.h @@ -19,17 +19,8 @@ public: // Transitional C-API -#ifndef TYPEDEF_COLOR -typedef struct -{ - double r; - double g; - double b; - double a; -} Color; -#endif - -typedef Color (*PictureCallbackSavePixel)(void* data, int x, int y); +// Les pixels doivent ĂȘtre fournis en RGBA +typedef unsigned int (*PictureCallbackSavePixel)(void* data, int x, int y); SYSTEMSHARED_EXPORT int systemSavePictureFile(const char* filepath, PictureCallbackSavePixel callback_pixel, void* data, int width, int height); #endif // PICTUREFILE_H diff --git a/src/tests/Bruneton_Test.cpp b/src/tests/Bruneton_Test.cpp index afd4c81..4cd16e4 100644 --- a/src/tests/Bruneton_Test.cpp +++ b/src/tests/Bruneton_Test.cpp @@ -28,7 +28,7 @@ TEST(Bruneton, AerialPerspective1) RenderParams params = {renderer->render_width, renderer->render_height, 1, 1}; renderSetParams(renderer->render_area, params); - renderSetBackgroundColor(renderer->render_area, &COLOR_BLACK); + renderSetBackgroundColor(renderer->render_area, COLOR_BLACK); renderClear(renderer->render_area); renderer->pushQuad(renderer, v3(50.0, -10.0, -50.0), v3(1.0, -10.0, -50.0), v3(1.0, -10.0, 50.0), v3(50.0, -10.0, 50.0), _postProcessFragment, NULL); @@ -63,7 +63,7 @@ TEST(Bruneton, AerialPerspective2) RenderParams params = {renderer->render_width, renderer->render_height, 1, 1}; renderSetParams(renderer->render_area, params); - renderSetBackgroundColor(renderer->render_area, &COLOR_BLACK); + renderSetBackgroundColor(renderer->render_area, COLOR_BLACK); renderClear(renderer->render_area); renderer->pushQuad(renderer, v3(50.0, -10.0, -50.0), v3(1.0, -10.0, -50.0), v3(1.0, -10.0, 50.0), v3(50.0, -10.0, 50.0), _postProcessFragment, NULL); diff --git a/src/tests/Render_Test.cpp b/src/tests/Render_Test.cpp index 17c005a..ac3e8ce 100644 --- a/src/tests/Render_Test.cpp +++ b/src/tests/Render_Test.cpp @@ -45,7 +45,7 @@ TEST(Render, quad) RenderParams params = {renderer->render_width, renderer->render_height, 1, 1}; renderSetParams(renderer->render_area, params); - renderSetBackgroundColor(renderer->render_area, &COLOR_BLUE); + renderSetBackgroundColor(renderer->render_area, COLOR_BLUE); renderClear(renderer->render_area); renderer->pushQuad(renderer, v3(-1.0, 0.0, 1.0), v3(-1.0, 0.0, -1.0), v3(1.0, 0.0, -1.0), v3(1.0, 0.0, 1.0), _postProcessFragment, NULL);