Fixed material dialog validation and made material hardness to work

This commit is contained in:
Michaël Lemaire 2013-12-25 19:45:56 +01:00
parent 8896127ecf
commit a9a6134633
8 changed files with 76 additions and 43 deletions

View file

@ -2,35 +2,49 @@
#include "PackStream.h"
void materialSave(PackStream* stream, SurfaceMaterial* material)
SurfaceMaterial::SurfaceMaterial():
SurfaceMaterial(COLOR_BLACK)
{
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)
SurfaceMaterial::SurfaceMaterial(const Color &color)
{
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);
base = colorToHSL(color);
hardness = 0.5;
reflection = 0.0;
shininess = 0.0;
receive_shadows = 1.0;
}
void materialValidate(SurfaceMaterial* material)
void SurfaceMaterial::save(PackStream* stream) const
{
material->_rgb = colorFromHSL(material->base);
stream->write(&base.h);
stream->write(&base.l);
stream->write(&base.s);
stream->write(&hardness);
stream->write(&reflection);
stream->write(&shininess);
stream->write(&receive_shadows);
}
void SurfaceMaterial::load(PackStream* stream)
{
stream->read(&base.h);
stream->read(&base.l);
stream->read(&base.s);
stream->read(&hardness);
stream->read(&reflection);
stream->read(&shininess);
stream->read(&receive_shadows);
validate();
}
void SurfaceMaterial::validate()
{
_rgb = colorFromHSL(base);
}

View file

@ -12,6 +12,15 @@ namespace definition {
class DEFINITIONSHARED_EXPORT SurfaceMaterial
{
public:
SurfaceMaterial();
SurfaceMaterial(const Color& color);
void save(PackStream* stream) const;
void load(PackStream* stream);
void validate();
public:
ColorHSL base;
@ -27,8 +36,4 @@ public:
}
}
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

View file

@ -43,7 +43,7 @@ void TextureLayerDefinition::validate()
_detail_noise->normalizeAmplitude(-0.008, 0.008, 0);
_detail_noise->validate();
materialValidate(material);
material->validate();
/* Update zone height range */
Scenery* scenery = getScenery();
@ -80,7 +80,7 @@ void TextureLayerDefinition::save(PackStream* stream) const
stream->write(&displacement_scaling);
stream->write(&displacement_height);
stream->write(&displacement_offset);
materialSave(stream, material);
material->save(stream);
_displacement_noise->save(stream);
_detail_noise->save(stream);
@ -94,7 +94,7 @@ void TextureLayerDefinition::load(PackStream* stream)
stream->read(&displacement_scaling);
stream->read(&displacement_height);
stream->read(&displacement_offset);
materialLoad(stream, material);
material->load(stream);
_displacement_noise->load(stream);
_detail_noise->load(stream);

View file

@ -36,7 +36,7 @@ void WaterDefinition::save(PackStream* stream) const
{
BaseDefinition::save(stream);
materialSave(stream, material);
material->save(stream);
depth_color->save(stream);
stream->write(&transparency_depth);
stream->write(&transparency);
@ -49,7 +49,7 @@ void WaterDefinition::save(PackStream* stream) const
stream->write(&turbulence);
stream->write(&foam_coverage);
materialSave(stream, foam_material);
foam_material->save(stream);
_waves_noise->save(stream);
}
@ -58,7 +58,7 @@ void WaterDefinition::load(PackStream* stream)
{
BaseDefinition::load(stream);
materialLoad(stream, material);
material->load(stream);
depth_color->load(stream);
stream->read(&transparency_depth);
stream->read(&transparency);
@ -71,7 +71,7 @@ void WaterDefinition::load(PackStream* stream)
stream->read(&turbulence);
stream->read(&foam_coverage);
materialLoad(stream, foam_material);
foam_material->load(stream);
_waves_noise->load(stream);
@ -116,8 +116,8 @@ void WaterDefinition::validate()
_waves_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -turbulence, 0.0);
_waves_noise->validate();
materialValidate(material);
materialValidate(foam_material);
material->validate();
foam_material->validate();
}
void WaterDefinition::applyPreset(WaterPreset preset)

View file

@ -46,12 +46,17 @@ DialogMaterialEditor::~DialogMaterialEditor()
bool DialogMaterialEditor::getMaterial(QWidget* parent, SurfaceMaterial* material)
{
DialogMaterialEditor dialog(parent, material);
return dialog.exec() != 0;
bool validated = dialog.exec() != 0;
if (validated)
{
*material = dialog.edited;
}
return validated;
}
void DialogMaterialEditor::refreshFromLocalData()
{
materialValidate(&edited);
edited.validate();
ui->preview_color->update();
}

View file

@ -55,9 +55,7 @@ static Mount MOUNTS[MOUNTS_COUNT] = {
},
};
static SurfaceMaterial MOUNT_MATERIAL = {
{0.4, 0.4, 0.4, 1.0}, 0.0, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0, 0.0}
};
static SurfaceMaterial MOUNT_MATERIAL(Color(0.4, 0.4, 0.4, 1.0));
static inline int _rayIntersectsTriangle(Vector3 p, Vector3 d, Vector3 v0, Vector3 v1, Vector3 v2, Vector3* hit)
{

View file

@ -181,7 +181,7 @@ Color CloudBasicLayerRenderer::getColor(BaseCloudsModel *model, const Vector3 &e
material.hardness = 0.25;
material.reflection = 0.0;
material.shininess = 0.0;
materialValidate(&material);
material.validate();
col = parent->applyLightingToSurface(segments[i].start, parent->getAtmosphereRenderer()->getSunDirection(), material);

View file

@ -60,6 +60,17 @@ Color LightingManager::applyFinalComponent(const LightComponent &component, cons
/* diffused light */
double diffuse = direction_inv.dotProduct(normal.normalize());
double sign = (diffuse < 0.0) ? -1.0 : 1.0;
if (material.hardness <= 0.5)
{
double hardness = material.hardness * 2.0;
diffuse = (1.0 - hardness) * (diffuse * diffuse) * sign + hardness * diffuse;
}
else if (diffuse != 0.0)
{
double hardness = (material.hardness - 0.5) * 2.0;
diffuse = (1.0 - hardness) * diffuse + hardness * sign * sqrt(fabs(diffuse));
}
diffuse = (diffuse + (1.0 - normal_norm)) / (1.0 + (1.0 - normal_norm));
if (diffuse > 0.0)
{