Fixed specular lighting on water not being consistent between opengl and software

This commit is contained in:
Michaël Lemaire 2015-09-10 23:41:15 +02:00
parent 3c351bbe5c
commit 2b5b35e3b4
4 changed files with 25 additions and 16 deletions

View file

@ -10,6 +10,7 @@
#include "NoiseFunctionSimplex.h" #include "NoiseFunctionSimplex.h"
#include "FloatNode.h" #include "FloatNode.h"
#include "FloatDiff.h" #include "FloatDiff.h"
#include "IntNode.h"
OpenGLWater::OpenGLWater(OpenGLRenderer *renderer): OpenGLWater::OpenGLWater(OpenGLRenderer *renderer):
OpenGLPart(renderer) OpenGLPart(renderer)
@ -42,12 +43,16 @@ void OpenGLWater::initialize()
// Watch for definition changes // Watch for definition changes
renderer->getScenery()->getTerrain()->propWaterHeight()->addWatcher(this, true); renderer->getScenery()->getTerrain()->propWaterHeight()->addWatcher(this, true);
renderer->getScenery()->getWater()->propReflection()->addWatcher(this, true); renderer->getScenery()->getWater()->propReflection()->addWatcher(this, true);
renderer->getScenery()->getWater()->propModel()->addWatcher(this, false);
} }
void OpenGLWater::update() void OpenGLWater::update()
{ {
Color water_color = *renderer->getScenery()->getWater()->material->base; WaterDefinition *water = renderer->getScenery()->getWater();
renderer->getSharedState()->set("waterColor", water_color); renderer->getSharedState()->set("waterMaterialColor", *water->material->base);
renderer->getSharedState()->set("waterMaterialReflection", water->material->reflection);
renderer->getSharedState()->set("waterMaterialShininess", water->material->shininess);
renderer->getSharedState()->set("waterMaterialHardness", water->material->hardness);
renderer->getSharedState()->set("simplexSampler", NoiseFunctionSimplex::getNormalTexture(), true, true); renderer->getSharedState()->set("simplexSampler", NoiseFunctionSimplex::getNormalTexture(), true, true);
} }
@ -77,6 +82,10 @@ void OpenGLWater::nodeChanged(const DefinitionNode *node, const DefinitionDiff *
{ {
renderer->getSharedState()->set("waterReflection", renderer->getScenery()->getWater()->propReflection()->getValue()); renderer->getSharedState()->set("waterReflection", renderer->getScenery()->getWater()->propReflection()->getValue());
} }
else if (node->getPath() == "/water/model")
{
update();
}
} }
void OpenGLWater::setEnabled(bool enabled) void OpenGLWater::setEnabled(bool enabled)

View file

@ -285,11 +285,8 @@ vec4 getSkyColor(vec3 location, vec3 direction)
return vec4(applyWeatherEffects(SPHERE_SIZE, nightsky + sunTransmittance.rgb, vec3(1), inscattering), 1.0); return vec4(applyWeatherEffects(SPHERE_SIZE, nightsky + sunTransmittance.rgb, vec3(1), inscattering), 1.0);
} }
vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess) vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float reflection, float shininess, float hardness)
{ {
float material_hardness = 0.3;
float material_reflection = 1.0;
float r0 = Rg + WORKAROUND_OFFSET + location.y * WORLD_SCALING; float r0 = Rg + WORKAROUND_OFFSET + location.y * WORLD_SCALING;
vec3 sun_position = sunDirection * SUN_DISTANCE; vec3 sun_position = sunDirection * SUN_DISTANCE;
float muS = dot(vec3(0.0, 1.0, 0.0), normalize(sun_position - vec3(0.0, r0, 0.0))); float muS = dot(vec3(0.0, 1.0, 0.0), normalize(sun_position - vec3(0.0, r0, 0.0)));
@ -301,15 +298,15 @@ vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess)
/* diffused light */ /* diffused light */
float diffuse = dot(sunDirection, normal); float diffuse = dot(sunDirection, normal);
float sign = (diffuse < 0.0) ? -1.0 : 1.0; float sign = (diffuse < 0.0) ? -1.0 : 1.0;
if (material_hardness <= 0.5) if (hardness <= 0.5)
{ {
float hardness = material_hardness * 2.0; float hardness_factor = hardness * 2.0;
diffuse = (1.0 - hardness) * (diffuse * diffuse) * sign + hardness * diffuse; diffuse = (1.0 - hardness_factor) * (diffuse * diffuse) * sign + hardness_factor * diffuse;
} }
else if (diffuse != 0.0) else if (diffuse != 0.0)
{ {
float hardness = (material_hardness - 0.5) * 2.0; float hardness_factor = (hardness - 0.5) * 2.0;
diffuse = (1.0 - hardness) * diffuse + hardness * sign * sqrt(abs(diffuse)); diffuse = (1.0 - hardness) * diffuse + hardness_factor * sign * sqrt(abs(diffuse));
} }
if (diffuse > 0.0) if (diffuse > 0.0)
{ {
@ -317,14 +314,14 @@ vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess)
} }
/* specular reflection */ /* specular reflection */
if (shininess > 0.0 && material_reflection > 0.0) if (shininess > 0.0 && reflection > 0.0)
{ {
vec3 view = normalize(location - cameraLocation); vec3 view = normalize(location - cameraLocation);
vec3 reflect = sunDirection - normal * 2.0 * dot(sunDirection, normal); vec3 reflect = sunDirection - normal * 2.0 * dot(sunDirection, normal);
float specular = dot(reflect, view); float specular = dot(reflect, view);
if (specular > 0.0) if (specular > 0.0)
{ {
specular = pow(specular, shininess) * material_reflection; specular = pow(specular, shininess) * reflection * ISun;
if (specular > 0.0) if (specular > 0.0)
{ {
result += specular * light_color; result += specular * light_color;

View file

@ -1,11 +1,14 @@
uniform vec4 waterColor; uniform vec4 waterMaterialColor;
uniform float waterMaterialReflection;
uniform float waterMaterialShininess;
uniform float waterMaterialHardness;
uniform float waterReflection; uniform float waterReflection;
void main(void) void main(void)
{ {
vec3 normal = noiseNormal2d(unprojected.xz, 0.001); vec3 normal = noiseNormal2d(unprojected.xz, 0.001);
gl_FragColor = applyLighting(unprojected, normal, waterColor, 16.0); gl_FragColor = applyLighting(unprojected, normal, waterMaterialColor, waterMaterialReflection, waterMaterialShininess, waterMaterialHardness);
vec3 reflected = reflect(unprojected - cameraLocation, normal); vec3 reflected = reflect(unprojected - cameraLocation, normal);
reflected.y = max(reflected.y, 0.0); reflected.y = max(reflected.y, 0.0);

View file

@ -80,7 +80,7 @@ Color LightingManager::applyFinalComponent(const LightComponent &component, cons
} }
/* specular reflection */ /* specular reflection */
if (material.reflection > 0.0 && component.reflection > 0.0) if (material.shininess > 0.0 && material.reflection > 0.0 && component.reflection > 0.0)
{ {
Vector3 view = location.sub(eye).normalize(); Vector3 view = location.sub(eye).normalize();
Vector3 reflect = direction_inv.sub(normal.scale(2.0 * direction_inv.dotProduct(normal))); Vector3 reflect = direction_inv.sub(normal.scale(2.0 * direction_inv.dotProduct(normal)));