3 #include <msp/core/algorithm.h>
17 zenith_direction(0, 0, 1),
18 horizon_angle(Geometry::Angle<float>::zero()),
19 fog_color(0.0f, 0.0f, 0.0f, 0.0f),
25 for(vector<Light *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
29 void Lighting::set_ambient(const Color &a)
34 void Lighting::set_sky_color(const Color &s)
39 void Lighting::set_zenith_direction(const Vector3 &d)
44 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
49 void Lighting::set_fog_color(const Color &c)
54 void Lighting::set_fog_density(float d)
57 throw invalid_argument("Lighting::set_fog_density");
62 void Lighting::set_fog_half_distance(float d)
64 set_fog_density(-log(pow(0.5, 1.0/d)));
67 void Lighting::attach(const Light &l)
69 if(find(lights, &l)==lights.end())
73 void Lighting::detach(const Light &l)
75 vector<const Light *>::iterator i = find(lights, &l);
80 void Lighting::detach(unsigned 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);
111 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
113 Lighting::Loader::Loader(Lighting &l):
114 CollectionObjectLoader<Lighting>(l, 0)
116 set_actions(shared_actions);
119 Lighting::Loader::Loader(Lighting &l, Collection &c):
120 CollectionObjectLoader<Lighting>(l, &c)
122 set_actions(shared_actions);
125 void Lighting::Loader::init_actions()
127 add("ambient", &Loader::ambient);
128 add("fog_color", &Loader::fog_color);
129 add("fog_density", &Loader::fog_density);
130 add("fog_half_distance", &Loader::fog_half_distance);
131 add("horizon_angle", &Loader::horizon_angle);
132 add("light", &Loader::light);
133 add("light", &Loader::light_inline);
134 add("sky_color", &Loader::sky_color);
135 add("zenith_direction", &Loader::zenith_direction);
138 add("light", &Loader::light_inline_index);
141 void Lighting::Loader::ambient(float r, float g, float b)
143 obj.ambient = Color(r, g, b);
146 void Lighting::Loader::fog_color(float r, float g, float b)
148 obj.set_fog_color(Color(r, g, b));
151 void Lighting::Loader::fog_density(float d)
153 obj.set_fog_density(d);
156 void Lighting::Loader::fog_half_distance(float d)
158 obj.set_fog_half_distance(d);
161 void Lighting::Loader::horizon_angle(float a)
163 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
166 void Lighting::Loader::light(const string &name)
168 obj.attach(get_collection().get<Light>(name));
171 void Lighting::Loader::light_inline()
173 RefPtr<Light> lgt = new Light;
176 obj.owned_data.push_back(lgt.release());
179 void Lighting::Loader::light_inline_index(unsigned)
184 void Lighting::Loader::sky_color(float r, float g, float b)
186 obj.set_sky_color(Color(r, g, b));
189 void Lighting::Loader::zenith_direction(float x, float y, float z)
191 obj.set_zenith_direction(Vector3(x, y, z));