Switched to RGB material colors

...to avoid otherwise unused HSL conversion, which causes problems
for some RGB values (like Color(1, 0, 0))
This commit is contained in:
Michaël Lemaire 2015-08-19 19:14:59 +02:00
parent fde385a51a
commit 0058a0a8e3
15 changed files with 73 additions and 40 deletions

View file

@ -10,6 +10,7 @@ class BASICSSHARED_EXPORT Color
{ {
public: public:
Color(); Color();
Color(const Color &col);
Color(double r, double g, double b, double a=1.0); Color(double r, double g, double b, double a=1.0);
void save(PackStream* stream) const; void save(PackStream* stream) const;

View file

@ -20,7 +20,11 @@ METHSPEC Color::Color():
METHSPEC Color::Color(double r, double g, double b, double a): METHSPEC Color::Color(double r, double g, double b, double a):
r(r), g(g), b(b), a(a) r(r), g(g), b(b), a(a)
{ {
}
METHSPEC Color::Color(const Color &col):
r(col.r), g(col.g), b(col.b), a(col.a)
{
} }
METHSPEC unsigned int Color::to32BitRGBA() const METHSPEC unsigned int Color::to32BitRGBA() const

View file

@ -2,7 +2,7 @@
#include "Color.h" #include "Color.h"
static double _hue2rgb(double p, double q, double t) static inline double _hue2rgb(double p, double q, double t)
{ {
if (t < 0.0) t += 1; if (t < 0.0) t += 1;
if (t > 1.0) t -= 1; if (t > 1.0) t -= 1;
@ -12,7 +12,7 @@ static double _hue2rgb(double p, double q, double t)
return p; return p;
} }
Color colorFromHSL(ColorHSL col) Color colorFromHSL(const ColorHSL &col)
{ {
Color result; Color result;
@ -34,7 +34,7 @@ Color colorFromHSL(ColorHSL col)
return result; return result;
} }
ColorHSL colorToHSL(Color col) ColorHSL colorToHSL(const Color &col)
{ {
ColorHSL result; ColorHSL result;
double min, max; double min, max;

View file

@ -11,8 +11,8 @@ typedef struct
double a; double a;
} ColorHSL; } ColorHSL;
BASICSSHARED_EXPORT Color colorFromHSL(ColorHSL col); BASICSSHARED_EXPORT Color colorFromHSL(const ColorHSL &col);
BASICSSHARED_EXPORT ColorHSL colorToHSL(Color col); BASICSSHARED_EXPORT ColorHSL colorToHSL(const Color &col);
BASICSSHARED_EXPORT ColorHSL colorHSLFromValues(double h, double s, double l, double a); BASICSSHARED_EXPORT ColorHSL colorHSLFromValues(double h, double s, double l, double a);

View file

@ -1,6 +1,7 @@
#include "SurfaceMaterial.h" #include "SurfaceMaterial.h"
#include "PackStream.h" #include "PackStream.h"
#include "Color.h"
SurfaceMaterial::SurfaceMaterial(): SurfaceMaterial::SurfaceMaterial():
SurfaceMaterial(COLOR_BLACK) SurfaceMaterial(COLOR_BLACK)
@ -9,18 +10,29 @@ SurfaceMaterial::SurfaceMaterial():
SurfaceMaterial::SurfaceMaterial(const Color &color) SurfaceMaterial::SurfaceMaterial(const Color &color)
{ {
base = colorToHSL(color); base = new Color(color);
hardness = 0.5; hardness = 0.5;
reflection = 0.0; reflection = 0.0;
shininess = 0.0; shininess = 0.0;
receive_shadows = 1.0; receive_shadows = 1.0;
} }
SurfaceMaterial::~SurfaceMaterial()
{
delete base;
}
void SurfaceMaterial::setColor(double r, double g, double b, double a)
{
base->r = r;
base->g = g;
base->b = b;
base->a = a;
}
void SurfaceMaterial::save(PackStream* stream) const void SurfaceMaterial::save(PackStream* stream) const
{ {
stream->write(&base.h); base->save(stream);
stream->write(&base.l);
stream->write(&base.s);
stream->write(&hardness); stream->write(&hardness);
stream->write(&reflection); stream->write(&reflection);
@ -31,20 +43,15 @@ void SurfaceMaterial::save(PackStream* stream) const
void SurfaceMaterial::load(PackStream* stream) void SurfaceMaterial::load(PackStream* stream)
{ {
stream->read(&base.h); base->load(stream);
stream->read(&base.l);
stream->read(&base.s);
stream->read(&hardness); stream->read(&hardness);
stream->read(&reflection); stream->read(&reflection);
stream->read(&shininess); stream->read(&shininess);
stream->read(&receive_shadows); stream->read(&receive_shadows);
validate();
} }
void SurfaceMaterial::validate() void SurfaceMaterial::validate()
{ {
_rgb = colorFromHSL(base);
} }

View file

@ -3,10 +3,6 @@
#include "definition_global.h" #include "definition_global.h"
// TODO Change to pointers and forward declaration
#include "ColorHSL.h"
#include "Color.h"
namespace paysages { namespace paysages {
namespace definition { namespace definition {
@ -15,6 +11,9 @@ class DEFINITIONSHARED_EXPORT SurfaceMaterial
public: public:
SurfaceMaterial(); SurfaceMaterial();
SurfaceMaterial(const Color& color); SurfaceMaterial(const Color& color);
~SurfaceMaterial();
void setColor(double r, double g, double b, double a);
void save(PackStream* stream) const; void save(PackStream* stream) const;
void load(PackStream* stream); void load(PackStream* stream);
@ -22,15 +21,13 @@ public:
void validate(); void validate();
public: public:
ColorHSL base; Color *base;
double hardness; double hardness;
double reflection; double reflection;
double shininess; double shininess;
double receive_shadows; double receive_shadows;
Color _rgb;
}; };
} }

View file

@ -6,6 +6,7 @@
#include "PackStream.h" #include "PackStream.h"
#include "Scenery.h" #include "Scenery.h"
#include "TerrainDefinition.h" #include "TerrainDefinition.h"
#include "Color.h"
TextureLayerDefinition::TextureLayerDefinition(DefinitionNode* parent): TextureLayerDefinition::TextureLayerDefinition(DefinitionNode* parent):
DefinitionNode(parent, "texture", "texturelayer") DefinitionNode(parent, "texture", "texturelayer")
@ -116,7 +117,7 @@ void TextureLayerDefinition::applyPreset(TextureLayerPreset preset)
displacement_height = 0.02; displacement_height = 0.02;
displacement_scaling = 3.0; displacement_scaling = 3.0;
displacement_offset = 0.0; displacement_offset = 0.0;
material->base = colorToHSL(Color(0.015, 0.014, 0.014, 1.0)); material->setColor(0.015, 0.014, 0.014, 1.0);
material->reflection = 0.003; material->reflection = 0.003;
material->shininess = 4.0; material->shininess = 4.0;
break; break;
@ -125,7 +126,7 @@ void TextureLayerDefinition::applyPreset(TextureLayerPreset preset)
displacement_height = 0.3; displacement_height = 0.3;
displacement_scaling = 2.0; displacement_scaling = 2.0;
displacement_offset = 0.0; displacement_offset = 0.0;
material->base = colorToHSL(Color(0.6, 0.55, 0.57, 1.0)); material->setColor(0.6, 0.55, 0.57, 1.0);
material->reflection = 0.006; material->reflection = 0.006;
material->shininess = 6.0; material->shininess = 6.0;
break; break;
@ -135,7 +136,7 @@ void TextureLayerDefinition::applyPreset(TextureLayerPreset preset)
displacement_height = 0.0; displacement_height = 0.0;
displacement_scaling = 1.0; displacement_scaling = 1.0;
displacement_offset = 0.0; displacement_offset = 0.0;
material->base = colorToHSL(Color(0.12, 0.19, 0.035, 1.0)); material->setColor(0.12, 0.19, 0.035, 1.0);
material->reflection = 0.001; material->reflection = 0.001;
material->shininess = 4.0; material->shininess = 4.0;
break; break;
@ -145,7 +146,7 @@ void TextureLayerDefinition::applyPreset(TextureLayerPreset preset)
displacement_height = 0.05; displacement_height = 0.05;
displacement_scaling = 5.0; displacement_scaling = 5.0;
displacement_offset = 0.0; displacement_offset = 0.0;
material->base = colorToHSL(Color(1.2, 1.1, 0.9, 1.0)); material->setColor(1.2, 1.1, 0.9, 1.0);
material->reflection = 0.008; material->reflection = 0.008;
material->shininess = 1.0; material->shininess = 1.0;
break; break;
@ -155,7 +156,7 @@ void TextureLayerDefinition::applyPreset(TextureLayerPreset preset)
displacement_height = 0.1; displacement_height = 0.1;
displacement_scaling = 1.0; displacement_scaling = 1.0;
displacement_offset = 0.0; displacement_offset = 0.0;
material->base = colorToHSL(Color(5.0, 5.0, 5.0, 1.0)); material->setColor(5.0, 5.0, 5.0, 1.0);
material->reflection = 0.02; material->reflection = 0.02;
material->shininess = 0.6; material->shininess = 0.6;
break; break;

View file

@ -115,7 +115,7 @@ void WaterDefinition::applyPreset(WaterPreset preset)
transparency = 0.5; transparency = 0.5;
reflection = 0.2; reflection = 0.2;
transparency_depth = 4.0; transparency_depth = 4.0;
material->base = colorToHSL(Color(0.08, 0.15, 0.2, 1.0)); material->setColor(0.08, 0.15, 0.2, 1.0);
depth_color->r = 0.0; depth_color->r = 0.0;
depth_color->g = 0.1; depth_color->g = 0.1;
depth_color->b = 0.1; depth_color->b = 0.1;
@ -131,7 +131,7 @@ void WaterDefinition::applyPreset(WaterPreset preset)
transparency = 0.3; transparency = 0.3;
reflection = 0.07; reflection = 0.07;
transparency_depth = 3.0; transparency_depth = 3.0;
material->base = colorToHSL(Color(0.05, 0.18, 0.2, 1.0)); material->setColor(0.05, 0.18, 0.2, 1.0);
depth_color->r = 0.0; depth_color->r = 0.0;
depth_color->g = 0.18; depth_color->g = 0.18;
depth_color->b = 0.15; depth_color->b = 0.15;
@ -144,11 +144,11 @@ void WaterDefinition::applyPreset(WaterPreset preset)
} }
depth_color->a = 1.0; depth_color->a = 1.0;
material->base.a = 1.0; material->base->a = 1.0;
material->reflection = 1.0; material->reflection = 1.0;
material->shininess = 16.0; material->shininess = 16.0;
material->hardness = 0.3; material->hardness = 0.3;
foam_material->base = colorToHSL(Color(0.8, 0.8, 0.8, 1.0)); foam_material->setColor(0.8, 0.8, 0.8, 1.0);
foam_material->reflection = 0.1; foam_material->reflection = 0.1;
foam_material->shininess = 1.5; foam_material->shininess = 1.5;
foam_material->hardness = 0.2; foam_material->hardness = 0.2;

View file

@ -43,7 +43,7 @@ void OpenGLWater::initialize()
void OpenGLWater::update() void OpenGLWater::update()
{ {
Color water_color = renderer->getScenery()->getWater()->material->_rgb; Color water_color = *renderer->getScenery()->getWater()->material->base;
renderer->getSharedState()->set("waterColor", water_color); renderer->getSharedState()->set("waterColor", water_color);
double water_reflection = renderer->getScenery()->getWater()->reflection; double water_reflection = renderer->getScenery()->getWater()->reflection;

View file

@ -176,8 +176,7 @@ Color CloudBasicLayerRenderer::getColor(BaseCloudsModel *model, const Vector3 &e
segment_count = _findSegments(model, parent, start, direction, 20, transparency_depth, max_length, &inside_length, &total_length, segments); segment_count = _findSegments(model, parent, start, direction, 20, transparency_depth, max_length, &inside_length, &total_length, segments);
for (i = segment_count - 1; i >= 0; i--) for (i = segment_count - 1; i >= 0; i--)
{ {
SurfaceMaterial material; SurfaceMaterial material(COLOR_WHITE);
material.base = colorToHSL(COLOR_WHITE);
material.hardness = 0.25; material.hardness = 0.25;
material.reflection = 0.0; material.reflection = 0.0;
material.shininess = 0.0; material.shininess = 0.0;

View file

@ -40,6 +40,6 @@ Color LightStatus::apply(const Vector3 &normal, const SurfaceMaterial &material)
final = final.add(manager->applyFinalComponent(component, eye, location, normal, material)); final = final.add(manager->applyFinalComponent(component, eye, location, normal, material));
} }
final.a = material.base.a; final.a = material.base->a;
return final; return final;
} }

View file

@ -74,9 +74,9 @@ Color LightingManager::applyFinalComponent(const LightComponent &component, cons
diffuse = (diffuse + (1.0 - normal_norm)) / (1.0 + (1.0 - normal_norm)); diffuse = (diffuse + (1.0 - normal_norm)) / (1.0 + (1.0 - normal_norm));
if (diffuse > 0.0) if (diffuse > 0.0)
{ {
result.r += diffuse * material._rgb.r * light_color.r; result.r += diffuse * material.base->r * light_color.r;
result.g += diffuse * material._rgb.g * light_color.g; result.g += diffuse * material.base->g * light_color.g;
result.b += diffuse * material._rgb.b * light_color.b; result.b += diffuse * material.base->b * light_color.b;
} }
/* specular reflection */ /* specular reflection */

View file

@ -236,7 +236,7 @@ WaterRenderer::WaterResult WaterRenderer::getResult(double x, double z)
/* Bring color to the camera */ /* Bring color to the camera */
color = parent->applyMediumTraversal(location, color); color = parent->applyMediumTraversal(location, color);
result.base = definition->material->_rgb; result.base = *definition->material->base;
result.final = color; result.final = color;
return result; return result;

View file

@ -0,0 +1,23 @@
#include "BaseTestCase.h"
#include "Color.h"
#include "ColorHSL.h"
TEST(ColorHSL, colorFromHSL)
{
std::vector<Color> colors;
colors.push_back(Color());
colors.push_back(Color(1.0, 0.0, 0.0, 1.0));
colors.push_back(Color(0.7, 0.5, 0.3, 1.0));
//colors.push_back(Color(2.0, 0.0, 0.0, 1.0)); // FIXME not working yet
colors.push_back(Color(3.0, 3.0, 3.0, 1.0));
for (auto &color: colors)
{
ColorHSL hsl = colorToHSL(color);
Color rgb = colorFromHSL(hsl);
EXPECT_COLOR_RGBA(rgb, color.r, color.g, color.b, color.a);
}
}

View file

@ -25,7 +25,8 @@ SOURCES += main.cpp \
Scenery_Test.cpp \ Scenery_Test.cpp \
DefinitionNode_Test.cpp \ DefinitionNode_Test.cpp \
FloatNode_Test.cpp \ FloatNode_Test.cpp \
DiffManager_Test.cpp DiffManager_Test.cpp \
ColorHSL_Test.cpp
HEADERS += \ HEADERS += \
BaseTestCase.h BaseTestCase.h