From d5d9ae219fe428453b7d7202f8e60931b26a7a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Thu, 26 Jan 2012 18:20:19 +0000 Subject: [PATCH] paysages: Fixed specular lighting and clouds reflection on water. git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@238 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- gui_qt/dialogrender.cpp | 2 +- gui_qt/formterrain.cpp | 3 +-- lib_paysages/auto.c | 1 + lib_paysages/clouds.c | 2 +- lib_paysages/lighting.c | 25 ++++++++++++++----------- lib_paysages/lighting.h | 1 + lib_paysages/scenery.c | 6 ++++-- lib_paysages/sky.c | 2 ++ lib_paysages/textures.c | 2 +- lib_paysages/water.c | 4 ++-- 10 files changed, 28 insertions(+), 20 deletions(-) diff --git a/gui_qt/dialogrender.cpp b/gui_qt/dialogrender.cpp index 32aa823..64427ab 100644 --- a/gui_qt/dialogrender.cpp +++ b/gui_qt/dialogrender.cpp @@ -72,7 +72,7 @@ public: }; DialogRender::DialogRender(QWidget *parent): - QDialog(parent) + QDialog(parent, Qt::WindowTitleHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint) { pixbuf = new QImage(1, 1, QImage::Format_ARGB32); _current_dialog = this; diff --git a/gui_qt/formterrain.cpp b/gui_qt/formterrain.cpp index ae6be33..e370fe2 100644 --- a/gui_qt/formterrain.cpp +++ b/gui_qt/formterrain.cpp @@ -41,8 +41,7 @@ class PreviewTerrainColor:public Preview public: PreviewTerrainColor(QWidget* parent):Preview(parent) { - // TODO Use custom renderer - _renderer = sceneryGetStandardRenderer(3); + _renderer = rendererGetFake(); _preview_definition = terrainCreateDefinition(); } protected: diff --git a/lib_paysages/auto.c b/lib_paysages/auto.c index 64b21c1..e9d23f8 100644 --- a/lib_paysages/auto.c +++ b/lib_paysages/auto.c @@ -107,6 +107,7 @@ void autoGenRealisticLandscape(int seed) noiseAddLevelSimple(cloud->noise, 50.0 / 800.0, 0.001); noiseAddLevelSimple(cloud->noise, 50.0 / 1000.0, 0.0005); scenerySetClouds(&clouds); + cloudsDeleteDefinition(&clouds); /* Water */ water = waterCreateDefinition(); diff --git a/lib_paysages/clouds.c b/lib_paysages/clouds.c index 9a38e04..9e8e017 100644 --- a/lib_paysages/clouds.c +++ b/lib_paysages/clouds.c @@ -422,7 +422,7 @@ static Color _applyLayerLighting(CloudsLayerDefinition* definition, Renderer* re material.base = base; material.reflection = 0.3; - material.shininess = 0.1; + material.shininess = 2.0; return renderer->applyLightingToSurface(renderer, position, normal, material); } diff --git a/lib_paysages/lighting.c b/lib_paysages/lighting.c index 640378a..b718a51 100644 --- a/lib_paysages/lighting.c +++ b/lib_paysages/lighting.c @@ -23,6 +23,7 @@ void lightingInit() _LIGHT_NULL.direction.y = 1.0; _LIGHT_NULL.direction.z = 0.0; _LIGHT_NULL.color = COLOR_BLACK; + _LIGHT_NULL.reflection = 0.0; _LIGHT_NULL.filtered = 0; _LIGHT_NULL.masked = 0; _LIGHT_NULL.amplitude = 0.0; @@ -38,6 +39,7 @@ void lightingSave(FILE* f, LightingDefinition* definition) { v3Save(definition->lights[i].direction, f); colorSave(definition->lights[i].color, f); + toolsSaveDouble(f, definition->lights[i].reflection); toolsSaveInt(f, definition->lights[i].filtered); toolsSaveInt(f, definition->lights[i].masked); toolsSaveDouble(f, definition->lights[i].amplitude); @@ -54,6 +56,7 @@ void lightingLoad(FILE* f, LightingDefinition* definition) { definition->lights[i].direction = v3Load(f); definition->lights[i].color = colorLoad(f); + definition->lights[i].reflection = toolsLoadDouble(f); definition->lights[i].filtered = toolsLoadInt(f); definition->lights[i].masked = toolsLoadInt(f); definition->lights[i].amplitude = toolsLoadDouble(f); @@ -166,20 +169,20 @@ static Color _applyLightCustom(LightDefinition* definition, Renderer* renderer, } normal = v3Normalize(normal); - view = v3Normalize(v3Sub(location, renderer->camera_location)); - reflect = v3Sub(v3Scale(normal, 2.0 * v3Dot(direction_inv, normal)), direction_inv); - diffuse = v3Dot(direction_inv, normal); - diffuse = pow(diffuse * 0.5 + 0.5, 2.0); - //diffuse = (diffuse * 0.5 + 0.5); + //diffuse = pow(diffuse * 0.5 + 0.5, 2.0); + diffuse = diffuse * 0.5 + 0.5; if (diffuse > 0.0) { - if (material.shininess > 0.0) + if (material.shininess > 0.0 && definition->reflection > 0.0) { + view = v3Normalize(v3Sub(location, renderer->camera_location)); + reflect = v3Sub(direction_inv, v3Scale(normal, 2.0 * v3Dot(direction_inv, normal))); + specular = v3Dot(reflect, view) * material.reflection; if (specular > 0.0) { - specular = pow(specular, material.shininess * 10.0 + 1.0); + specular = pow(specular, material.shininess); } else { @@ -197,12 +200,12 @@ static Color _applyLightCustom(LightDefinition* definition, Renderer* renderer, specular = 0.0; } - specular *= normal_norm; + specular *= normal_norm * definition->reflection; diffuse = 1.0 - normal_norm + diffuse * normal_norm; - result.r = material.base.r * diffuse * light.r + material.base.r * specular * light.r; - result.g = material.base.g * diffuse * light.g + material.base.g * specular * light.g; - result.b = material.base.b * diffuse * light.b + material.base.b * specular * light.b; + result.r = material.base.r * diffuse * light.r + specular * light.r; + result.g = material.base.g * diffuse * light.g + specular * light.g; + result.b = material.base.b * diffuse * light.b + specular * light.b; result.a = material.base.a; return result; diff --git a/lib_paysages/lighting.h b/lib_paysages/lighting.h index 90c0459..f973fec 100644 --- a/lib_paysages/lighting.h +++ b/lib_paysages/lighting.h @@ -15,6 +15,7 @@ typedef struct { Vector3 direction; /* Global direction of the light */ Color color; /* Main color of the light */ + double reflection; /* Reflected factor of the light (for specular lighting) */ int filtered; /* Should the light be filtered (by atmosphere, water...) */ int masked; /* Should the light be masked (cast shadows..) */ double amplitude; /* Angle amplitude of the light source (for multi-sampling, pi / 2.0 for skydome) */ diff --git a/lib_paysages/scenery.c b/lib_paysages/scenery.c index c839ce3..c010109 100644 --- a/lib_paysages/scenery.c +++ b/lib_paysages/scenery.c @@ -232,11 +232,13 @@ static Color _applyLightingToSurface(Renderer* renderer, Vector3 location, Vecto static RayCastingResult _rayWalking(Renderer* renderer, Vector3 location, Vector3 direction, int terrain, int water, int sky, int clouds) { RayCastingResult result; + Color sky_color; if (!terrainProjectRay(&_terrain, renderer, location, direction, &result.hit_location, &result.hit_color)) { - result.hit_color = skyGetColor(&_sky, renderer, location, direction); - /* TODO hit_location */ + sky_color = skyGetColor(&_sky, renderer, location, direction); + result.hit_location = v3Add(location, v3Scale(direction, 1000.0)); + result.hit_color = renderer->applyClouds(renderer, sky_color, location, result.hit_location); } result.hit = 1; diff --git a/lib_paysages/sky.c b/lib_paysages/sky.c index f5d6527..b5cfc5a 100644 --- a/lib_paysages/sky.c +++ b/lib_paysages/sky.c @@ -99,6 +99,7 @@ int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights) /* Light from the sun */ lights[0].direction = v3Scale(sun_direction, -1.0); lights[0].color = colorGradationGet(&sky->sun_color, sky->daytime); + lights[0].reflection = 1.0; lights[0].filtered = 1; lights[0].masked = 1; lights[0].amplitude = 0.0; @@ -113,6 +114,7 @@ int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights) lights[1].color.r *= 0.4; lights[1].color.g *= 0.4; lights[1].color.b *= 0.4; + lights[1].reflection = 0.0; lights[1].filtered = 1; lights[1].masked = 0; lights[1].amplitude = M_PI / 2.0; diff --git a/lib_paysages/textures.c b/lib_paysages/textures.c index ec17fa5..d4e2517 100644 --- a/lib_paysages/textures.c +++ b/lib_paysages/textures.c @@ -221,7 +221,7 @@ Color texturesGetLayerColor(TextureLayerDefinition* definition, Renderer* render { material.base = definition->color; material.reflection = 0.1; - material.shininess = 0.1; + material.shininess = 4.0; result = renderer->applyLightingToSurface(renderer, location, normal, material); result.a = coverage; diff --git a/lib_paysages/water.c b/lib_paysages/water.c index a32a985..a4f7070 100644 --- a/lib_paysages/water.c +++ b/lib_paysages/water.c @@ -199,8 +199,8 @@ WaterResult waterGetColorDetail(WaterDefinition* definition, Renderer* renderer, color.a = 1.0; material.base = color; - material.reflection = 0.8; - material.shininess = 0.6; + material.reflection = 1.0; + material.shininess = 12.0; color = renderer->applyLightingToSurface(renderer, location, normal, material); color = renderer->applyAtmosphere(renderer, location, color);