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" #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); base = colorToHSL(color);
stream->read(&material->base.l); hardness = 0.5;
stream->read(&material->base.s); reflection = 0.0;
shininess = 0.0;
stream->read(&material->hardness); receive_shadows = 1.0;
stream->read(&material->reflection);
stream->read(&material->shininess);
stream->read(&material->receive_shadows);
materialValidate(material);
} }
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 class DEFINITIONSHARED_EXPORT SurfaceMaterial
{ {
public:
SurfaceMaterial();
SurfaceMaterial(const Color& color);
void save(PackStream* stream) const;
void load(PackStream* stream);
void validate();
public: public:
ColorHSL base; 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 #endif // SURFACEMATERIAL_H

View file

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

View file

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

View file

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

View file

@ -55,9 +55,7 @@ static Mount MOUNTS[MOUNTS_COUNT] = {
}, },
}; };
static SurfaceMaterial MOUNT_MATERIAL = { static SurfaceMaterial MOUNT_MATERIAL(Color(0.4, 0.4, 0.4, 1.0));
{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 inline int _rayIntersectsTriangle(Vector3 p, Vector3 d, Vector3 v0, Vector3 v1, Vector3 v2, Vector3* hit) 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.hardness = 0.25;
material.reflection = 0.0; material.reflection = 0.0;
material.shininess = 0.0; material.shininess = 0.0;
materialValidate(&material); material.validate();
col = parent->applyLightingToSurface(segments[i].start, parent->getAtmosphereRenderer()->getSunDirection(), material); 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 */ /* diffused light */
double diffuse = direction_inv.dotProduct(normal.normalize()); 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)); diffuse = (diffuse + (1.0 - normal_norm)) / (1.0 + (1.0 - normal_norm));
if (diffuse > 0.0) if (diffuse > 0.0)
{ {