Optimization for moon lighting

This commit is contained in:
Michaël Lemaire 2013-12-26 19:03:19 +01:00
parent 0e837f00c6
commit 938242a7de
6 changed files with 37 additions and 13 deletions

View file

@ -21,7 +21,7 @@ OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
shared_state = new OpenGLSharedState(); shared_state = new OpenGLSharedState();
shared_state->set("viewDistance", 300.0); shared_state->set("viewDistance", 300.0);
shared_state->set("exposure", 1.6); shared_state->set("exposure", 1.2);
skybox = new OpenGLSkybox(this); skybox = new OpenGLSkybox(this);
water = new OpenGLWater(this); water = new OpenGLWater(this);

View file

@ -15,5 +15,7 @@ void main(void)
vec3 attenuation; vec3 attenuation;
vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation); vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation);
gl_FragColor = applyToneMapping(sunTransmittance + vec4(inscattering, 0.0)); gl_FragColor = vec4(0.01, 0.012, 0.03, 1.0); // night sky
gl_FragColor += sunTransmittance + vec4(inscattering, 0.0);
gl_FragColor = applyToneMapping(gl_FragColor);
} }

View file

@ -12,21 +12,23 @@ float _uncharted2Tonemap(float x)
return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F; return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
} }
vec4 applyToneMapping(vec4 color)
{
return vec4(((color * exposure) / (1.0 + color * exposure)).rgb, 1.0);
}
/*vec4 applyToneMapping(vec4 color) /*vec4 applyToneMapping(vec4 color)
{ {
float e = mix(exposure, exposure * 3.0, clamp(-sunDirection.y * 10.0, 0.0, 1.0));
return vec4(((color * e) / (1.0 + color * e)).rgb, 1.0);
}*/
vec4 applyToneMapping(vec4 color)
{
float e = mix(exposure, exposure * 3.0, clamp(-sunDirection.y * 10.0, 0.0, 1.0));
float W = 11.2; float W = 11.2;
float white_scale = 1.0 / _uncharted2Tonemap(W); float white_scale = 1.0 / _uncharted2Tonemap(W);
vec4 result; vec4 result;
result.r = pow(_uncharted2Tonemap(color.r * exposure) * white_scale, 1.0 / 2.2); result.r = pow(_uncharted2Tonemap(color.r * e) * white_scale, 1.0 / 2.2);
result.g = pow(_uncharted2Tonemap(color.g * exposure) * white_scale, 1.0 / 2.2); result.g = pow(_uncharted2Tonemap(color.g * e) * white_scale, 1.0 / 2.2);
result.b = pow(_uncharted2Tonemap(color.b * exposure) * white_scale, 1.0 / 2.2); result.b = pow(_uncharted2Tonemap(color.b * e) * white_scale, 1.0 / 2.2);
result.a = 1.0; result.a = 1.0;
return result; return result;
}*/ }

View file

@ -6,6 +6,7 @@
LightStatus::LightStatus(LightingManager *manager, const Vector3 &location, const Vector3 &eye) LightStatus::LightStatus(LightingManager *manager, const Vector3 &location, const Vector3 &eye)
{ {
this->max_power = 0.0;
this->manager = manager; this->manager = manager;
this->location = location; this->location = location;
this->eye = eye; this->eye = eye;
@ -13,8 +14,19 @@ LightStatus::LightStatus(LightingManager *manager, const Vector3 &location, cons
void LightStatus::pushComponent(LightComponent component) void LightStatus::pushComponent(LightComponent component)
{ {
double power = component.color.getPower();
if (component.altered && (power < max_power * 0.05 || power < 0.001))
{
// Exclude filtered lights that are owerpowered by a previous one
return;
}
if (manager->alterLight(component, location)) if (manager->alterLight(component, location))
{ {
if (power > max_power)
{
max_power = power;
}
components.push_back(component); components.push_back(component);
} }
} }

View file

@ -25,6 +25,7 @@ public:
Color apply(const Vector3 &normal, const SurfaceMaterial &material); Color apply(const Vector3 &normal, const SurfaceMaterial &material);
private: private:
double max_power;
LightingManager* manager; LightingManager* manager;
Vector3 location; Vector3 location;
Vector3 eye; Vector3 eye;

View file

@ -72,7 +72,7 @@ const Color NightSky::getColor(double altitude, const Vector3 &direction)
double dist = hit2.sub(hit1).getNorm() / moon_radius; // distance between intersection points (relative to radius) double dist = hit2.sub(hit1).getNorm() / moon_radius; // distance between intersection points (relative to radius)
Vector3 nearest = (hit1.sub(location).getNorm() > hit2.sub(location).getNorm()) ? hit2 : hit1; Vector3 nearest = (hit1.sub(location).getNorm() > hit2.sub(location).getNorm()) ? hit2 : hit1;
SurfaceMaterial moon_material(Color(3.0, 3.0, 3.0, 1.0)); SurfaceMaterial moon_material(Color(3.0, 3.0, 3.0));
moon_material.validate(); moon_material.validate();
Color moon_color = renderer->applyLightingToSurface(nearest, nearest.sub(moon_position).normalize(), moon_material); Color moon_color = renderer->applyLightingToSurface(nearest, nearest.sub(moon_position).normalize(), moon_material);
@ -91,7 +91,7 @@ const Color NightSky::getColor(double altitude, const Vector3 &direction)
void NightSky::fillLightingStatus(LightStatus *status, const Vector3 &, int) void NightSky::fillLightingStatus(LightStatus *status, const Vector3 &, int)
{ {
LightComponent moon; LightComponent moon, sky;
AtmosphereDefinition* atmosphere = renderer->getScenery()->getAtmosphere(); AtmosphereDefinition* atmosphere = renderer->getScenery()->getAtmosphere();
VectorSpherical moon_location_s = {MOON_DISTANCE_SCALED, atmosphere->moon_theta, -atmosphere->moon_phi}; VectorSpherical moon_location_s = {MOON_DISTANCE_SCALED, atmosphere->moon_theta, -atmosphere->moon_phi};
@ -102,4 +102,11 @@ void NightSky::fillLightingStatus(LightStatus *status, const Vector3 &, int)
moon.altered = 1; moon.altered = 1;
status->pushComponent(moon); status->pushComponent(moon);
sky.color = Color(0.01, 0.012, 0.03);
sky.direction = VECTOR_DOWN;
sky.reflection = 0.0;
sky.altered = 0;
status->pushComponent(sky);
} }