3 #include <msp/gl/extensions/msp_legacy_features.h>
18 zenith_direction(0, 0, 1),
19 horizon_angle(Geometry::Angle<float>::zero()),
20 fog_color(0.0f, 0.0f, 0.0f, 0.0f),
24 void Lighting::set_ambient(const Color &a)
29 void Lighting::set_sky_color(const Color &s)
34 void Lighting::set_zenith_direction(const Vector3 &d)
39 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
44 void Lighting::set_fog_color(const Color &c)
49 void Lighting::set_fog_density(float d)
52 throw invalid_argument("Lighting::set_fog_density");
57 void Lighting::set_fog_half_distance(float d)
59 set_fog_density(-log(pow(0.5, 1.0/d)));
62 void Lighting::attach(unsigned i, const Light &l)
72 void Lighting::detach(unsigned i)
79 Light::unbind_from(i);
82 const Light *Lighting::get_attached_light(unsigned i) const
84 return i<lights.size() ? lights[i] : 0;
87 void Lighting::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
89 shdata.uniform("ambient_color", ambient);
90 shdata.uniform("sky_color", sky_color);
91 shdata.uniform("eye_zenith_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
92 shdata.uniform("horizon_limit", horizon_angle.radians());
93 shdata.uniform("fog_color", fog_color);
94 shdata.uniform("fog_density", fog_density);
96 // For backwards compatibility
97 shdata.uniform("eye_sky_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
99 for(unsigned i=0; i<lights.size(); ++i)
101 lights[i]->update_shader_data(shdata, view_matrix, i);
104 void Lighting::bind() const
106 static Require _req(MSP_legacy_features);
107 if(lights.size()>LightUnit::get_n_units())
108 throw invalid_operation("Lighting::bind");
110 const Lighting *old = current();
111 if(!set_current(this))
115 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
116 for(unsigned i=0; i<lights.size(); ++i)
119 lights[i]->bind_to(i);
121 Light::unbind_from(i);
126 for(unsigned i=lights.size(); i<old->lights.size(); ++i)
127 Light::unbind_from(i);
133 glFogi(GL_FOG_MODE, GL_EXP);
134 glFogf(GL_FOG_DENSITY, fog_density);
135 glFogfv(GL_FOG_COLOR, &fog_color.r);
139 void Lighting::unbind()
141 const Lighting *old = current();
145 for(unsigned i=0; i<old->lights.size(); ++i)
147 Light::unbind_from(i);
149 disable(GL_LIGHTING);