3 #include <msp/core/algorithm.h>
16 zenith_direction(0, 0, 1),
17 horizon_angle(Geometry::Angle<float>::zero())
20 set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
21 set_fog_density(0.0f);
26 for(vector<Light *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
30 void Lighting::set_ambient(const Color &a)
33 shdata.uniform("ambient_color", ambient);
36 void Lighting::set_sky_color(const Color &s)
39 shdata.uniform("sky_color", sky_color);
42 void Lighting::set_zenith_direction(const Vector3 &d)
45 shdata.uniform("world_zenith_dir", zenith_direction);
48 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
51 shdata.uniform("horizon_limit", horizon_angle.radians());
54 void Lighting::set_fog_color(const Color &c)
57 shdata.uniform("fog_color", fog_color);
60 void Lighting::set_fog_density(float d)
63 throw invalid_argument("Lighting::set_fog_density");
66 shdata.uniform("fog_density", fog_density);
69 void Lighting::set_fog_half_distance(float d)
71 set_fog_density(-log(pow(0.5, 1.0/d)));
74 void Lighting::attach(const Light &l)
76 if(find_member(lights, &l, &AttachedLight::light)==lights.end())
80 void Lighting::detach(const Light &l)
82 vector<AttachedLight>::iterator i = find_member(lights, &l, &AttachedLight::light);
87 void Lighting::detach(unsigned i)
92 detach(*lights[i].light);
95 const Light *Lighting::get_attached_light(unsigned i) const
97 return i<lights.size() ? lights[i].light : 0;
100 void Lighting::update_shader_data(ProgramData &sd, const Matrix &) const
102 sd.uniform("ambient_color", ambient);
103 sd.uniform("sky_color", sky_color);
104 sd.uniform("world_zenith_dir", zenith_direction);
105 sd.uniform("horizon_limit", horizon_angle.radians());
106 sd.uniform("fog_color", fog_color);
107 sd.uniform("fog_density", fog_density);
109 for(unsigned i=0; i<lights.size(); ++i)
111 lights[i].light->update_shader_data(sd, i);
114 const ProgramData &Lighting::get_shader_data() const
116 for(unsigned i=0; i<lights.size(); ++i)
117 if(lights[i].light->get_generation()!=lights[i].generation)
119 lights[i].light->update_shader_data(shdata, i);
120 lights[i].generation = lights[i].light->get_generation();
126 void Lighting::set_debug_name(const string &name)
129 shdata.set_debug_name(name+" [UBO]");
136 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
138 Lighting::Loader::Loader(Lighting &l):
139 CollectionObjectLoader<Lighting>(l, 0)
141 set_actions(shared_actions);
144 Lighting::Loader::Loader(Lighting &l, Collection &c):
145 CollectionObjectLoader<Lighting>(l, &c)
147 set_actions(shared_actions);
150 void Lighting::Loader::init_actions()
152 add("ambient", &Loader::ambient);
153 add("fog_color", &Loader::fog_color);
154 add("fog_density", &Loader::fog_density);
155 add("fog_half_distance", &Loader::fog_half_distance);
156 add("horizon_angle", &Loader::horizon_angle);
157 add("light", &Loader::light);
158 add("light", &Loader::light_inline);
159 add("sky_color", &Loader::sky_color);
160 add("zenith_direction", &Loader::zenith_direction);
163 add("light", &Loader::light_inline_index);
166 void Lighting::Loader::ambient(float r, float g, float b)
168 obj.set_ambient(Color(r, g, b));
171 void Lighting::Loader::fog_color(float r, float g, float b)
173 obj.set_fog_color(Color(r, g, b));
176 void Lighting::Loader::fog_density(float d)
178 obj.set_fog_density(d);
181 void Lighting::Loader::fog_half_distance(float d)
183 obj.set_fog_half_distance(d);
186 void Lighting::Loader::horizon_angle(float a)
188 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
191 void Lighting::Loader::light(const string &name)
193 obj.attach(get_collection().get<Light>(name));
196 void Lighting::Loader::light_inline()
198 RefPtr<Light> lgt = new Light;
201 obj.owned_data.push_back(lgt.release());
204 void Lighting::Loader::light_inline_index(unsigned)
209 void Lighting::Loader::sky_color(float r, float g, float b)
211 obj.set_sky_color(Color(r, g, b));
214 void Lighting::Loader::zenith_direction(float x, float y, float z)
216 obj.set_zenith_direction(Vector3(x, y, z));