X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Flighting.cpp;h=ce522ac06a11e77151a5c7fbe0967347674f2aa3;hp=2ba1edc5560e00889ecd31fa01592f121a68a993;hb=4fe225bf15048fcb7a678370f87d09f2de37031a;hpb=70663ae97d5b70a6b468e07dd7e73475a175dc9b diff --git a/source/lighting.cpp b/source/lighting.cpp index 2ba1edc5..ce522ac0 100644 --- a/source/lighting.cpp +++ b/source/lighting.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include "error.h" #include "light.h" #include "lighting.h" #include "lightunit.h" @@ -13,7 +16,9 @@ namespace GL { Lighting::Lighting(): ambient(0.2), sky_direction(0, 0, 1), - horizon_angle(Geometry::Angle::zero()) + horizon_angle(Geometry::Angle::zero()), + fog_color(0.0f, 0.0f, 0.0f, 0.0f), + fog_density(0.0f) { } void Lighting::set_ambient(const Color &a) @@ -36,11 +41,26 @@ void Lighting::set_horizon_angle(const Geometry::Angle &a) horizon_angle = a; } -void Lighting::attach(unsigned i, const Light &l) +void Lighting::set_fog_color(const Color &c) +{ + fog_color = c; +} + +void Lighting::set_fog_density(float d) +{ + if(d<0) + throw invalid_argument("Lighting::set_fog_density"); + + fog_density = d; +} + +void Lighting::set_fog_half_distance(float d) { - if(i>=LightUnit::get_n_units()) - throw out_of_range("Lighting::attach"); + set_fog_density(-log(pow(0.5, 1.0/d))); +} +void Lighting::attach(unsigned i, const Light &l) +{ if(i>=lights.size()) lights.resize(i+1); @@ -59,12 +79,19 @@ void Lighting::detach(unsigned i) Light::unbind_from(i); } +const Light *Lighting::get_attached_light(unsigned i) const +{ + return i(0, 0)*sky_direction); shdata.uniform("horizon_limit", horizon_angle.radians()); + shdata.uniform("fog_color", fog_color); + shdata.uniform("fog_density", fog_density); for(unsigned i=0; iLightUnit::get_n_units()) + throw invalid_operation("Lighting::bind"); + + const Lighting *old = current(); if(!set_current(this)) return; enable(GL_LIGHTING); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r); for(unsigned i=0; ibind_to(i); + else + Light::unbind_from(i); + } + + if(old) + { + for(unsigned i=lights.size(); ilights.size(); ++i) + Light::unbind_from(i); + } + + if(fog_density) + { + enable(GL_FOG); + glFogi(GL_FOG_MODE, GL_EXP); + glFogf(GL_FOG_DENSITY, fog_density); + glFogfv(GL_FOG_COLOR, &fog_color.r); + } } void Lighting::unbind() @@ -94,6 +144,8 @@ void Lighting::unbind() Light::unbind_from(i); disable(GL_LIGHTING); + if(old->fog_density) + disable(GL_FOG); } } // namespace GL