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),
26 for(vector<Light *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
30 void Lighting::set_ambient(const Color &a)
35 void Lighting::set_sky_color(const Color &s)
40 void Lighting::set_zenith_direction(const Vector3 &d)
45 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
50 void Lighting::set_fog_color(const Color &c)
55 void Lighting::set_fog_density(float d)
58 throw invalid_argument("Lighting::set_fog_density");
63 void Lighting::set_fog_half_distance(float d)
65 set_fog_density(-log(pow(0.5, 1.0/d)));
68 void Lighting::attach(unsigned i, const Light &l)
78 void Lighting::detach(unsigned i)
85 Light::unbind_from(i);
88 const Light *Lighting::get_attached_light(unsigned i) const
90 return i<lights.size() ? lights[i] : 0;
93 void Lighting::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
95 shdata.uniform("ambient_color", ambient);
96 shdata.uniform("sky_color", sky_color);
97 shdata.uniform("eye_zenith_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
98 shdata.uniform("horizon_limit", horizon_angle.radians());
99 shdata.uniform("fog_color", fog_color);
100 shdata.uniform("fog_density", fog_density);
102 // For backwards compatibility
103 shdata.uniform("eye_sky_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
105 for(unsigned i=0; i<lights.size(); ++i)
107 lights[i]->update_shader_data(shdata, view_matrix, i);
110 void Lighting::bind() const
112 static Require _req(MSP_legacy_features);
113 if(lights.size()>LightUnit::get_n_units())
114 throw invalid_operation("Lighting::bind");
116 const Lighting *old = current();
117 if(!set_current(this))
121 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
122 for(unsigned i=0; i<lights.size(); ++i)
125 lights[i]->bind_to(i);
127 Light::unbind_from(i);
132 for(unsigned i=lights.size(); i<old->lights.size(); ++i)
133 Light::unbind_from(i);
139 glFogi(GL_FOG_MODE, GL_EXP);
140 glFogf(GL_FOG_DENSITY, fog_density);
141 glFogfv(GL_FOG_COLOR, &fog_color.r);
145 void Lighting::unbind()
147 const Lighting *old = current();
151 for(unsigned i=0; i<old->lights.size(); ++i)
153 Light::unbind_from(i);
155 disable(GL_LIGHTING);
161 Lighting::Loader::Loader(Lighting &l):
162 DataFile::ObjectLoader<Lighting>(l)
164 add("ambient", &Loader::ambient);
165 add("fog_color", &Loader::fog_color);
166 add("fog_density", &Loader::fog_density);
167 add("fog_half_distance", &Loader::fog_half_distance);
168 add("horizon_angle", &Loader::horizon_angle);
169 add("light", &Loader::light);
170 add("sky_color", &Loader::sky_color);
171 add("zenith_direction", &Loader::zenith_direction);
174 void Lighting::Loader::ambient(float r, float g, float b)
176 obj.ambient = Color(r, g, b);
179 void Lighting::Loader::fog_color(float r, float g, float b)
181 obj.set_fog_color(Color(r, g, b));
184 void Lighting::Loader::fog_density(float d)
186 obj.set_fog_density(d);
189 void Lighting::Loader::fog_half_distance(float d)
191 obj.set_fog_half_distance(d);
194 void Lighting::Loader::horizon_angle(float a)
196 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
199 void Lighting::Loader::light(unsigned i)
201 RefPtr<Light> lgt = new Light;
204 obj.owned_data.push_back(lgt.release());
207 void Lighting::Loader::sky_color(float r, float g, float b)
209 obj.set_sky_color(Color(r, g, b));
212 void Lighting::Loader::zenith_direction(float x, float y, float z)
214 obj.set_sky_direction(Vector3(x, y, z));