3 #include <msp/core/algorithm.h>
4 #include <msp/datafile/collection.h>
5 #include <msp/fs/utils.h>
6 #include <msp/strings/format.h>
18 zenith_direction(0, 0, 1),
19 horizon_angle(Geometry::Angle<float>::zero())
22 set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
23 set_fog_density(0.0f);
26 void Lighting::set_ambient(const Color &a)
29 shdata.uniform("ambient_color", ambient);
32 void Lighting::set_sky_color(const Color &s)
35 shdata.uniform("sky_color", sky_color);
38 void Lighting::set_zenith_direction(const Vector3 &d)
41 shdata.uniform("world_zenith_dir", zenith_direction);
44 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
47 shdata.uniform("horizon_limit", horizon_angle.radians());
50 void Lighting::set_fog_color(const Color &c)
53 shdata.uniform("fog_color", fog_color);
56 void Lighting::set_fog_density(float d)
59 throw invalid_argument("Lighting::set_fog_density");
62 shdata.uniform("fog_density", fog_density);
65 void Lighting::set_fog_half_distance(float d)
67 set_fog_density(-log(pow(0.5, 1.0/d)));
70 void Lighting::attach(const Light &l)
72 if(find_member(lights, &l, &AttachedLight::light)==lights.end())
76 void Lighting::detach(const Light &l)
78 auto i = find_member(lights, &l, &AttachedLight::light);
83 void Lighting::detach(unsigned i)
88 detach(*lights[i].light);
91 const Light *Lighting::get_attached_light(unsigned i) const
93 return i<lights.size() ? lights[i].light : 0;
96 void Lighting::update_shader_data(ProgramData &sd, const Matrix &) const
98 sd.uniform("ambient_color", ambient);
99 sd.uniform("sky_color", sky_color);
100 sd.uniform("world_zenith_dir", zenith_direction);
101 sd.uniform("horizon_limit", horizon_angle.radians());
102 sd.uniform("fog_color", fog_color);
103 sd.uniform("fog_density", fog_density);
105 for(unsigned i=0; i<lights.size(); ++i)
107 lights[i].light->update_shader_data(sd, i);
110 const ProgramData &Lighting::get_shader_data() const
112 for(unsigned i=0; i<lights.size(); ++i)
113 if(lights[i].light->get_generation()!=lights[i].generation)
115 lights[i].light->update_shader_data(shdata, i);
116 lights[i].generation = lights[i].light->get_generation();
122 void Lighting::set_debug_name(const string &name)
125 shdata.set_debug_name(name+" [UBO]");
132 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
134 Lighting::Loader::Loader(Lighting &l):
135 CollectionObjectLoader<Lighting>(l, 0)
137 set_actions(shared_actions);
140 Lighting::Loader::Loader(Lighting &l, Collection &c):
141 CollectionObjectLoader<Lighting>(l, &c)
143 set_actions(shared_actions);
146 void Lighting::Loader::init_actions()
148 add("ambient", &Loader::ambient);
149 add("fog_color", &Loader::fog_color);
150 add("fog_density", &Loader::fog_density);
151 add("fog_half_distance", &Loader::fog_half_distance);
152 add("light", &Loader::light);
153 add("light", &Loader::light_inline);
156 add("horizon_angle", &Loader::horizon_angle);
157 add("light", &Loader::light_inline_index);
158 add("sky_color", &Loader::sky_color);
159 add("zenith_direction", &Loader::zenith_direction);
162 void Lighting::Loader::ambient(float r, float g, float b)
164 obj.set_ambient(Color(r, g, b));
167 void Lighting::Loader::fog_color(float r, float g, float b)
169 obj.set_fog_color(Color(r, g, b));
172 void Lighting::Loader::fog_density(float d)
174 obj.set_fog_density(d);
177 void Lighting::Loader::fog_half_distance(float d)
179 obj.set_fog_half_distance(d);
182 #pragma GCC diagnostic push
183 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
184 void Lighting::Loader::horizon_angle(float a)
186 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
188 #pragma GCC diagnostic pop
190 void Lighting::Loader::light(const string &name)
192 obj.attach(get_collection().get<Light>(name));
195 void Lighting::Loader::light_inline()
197 RefPtr<Light> lgt = new Light;
199 get_collection().add(format("%s/%d.light", FS::basename(get_source()), obj.lights.size()), lgt.get());
200 obj.attach(*lgt.release());
203 void Lighting::Loader::light_inline_index(unsigned)
208 #pragma GCC diagnostic push
209 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
210 void Lighting::Loader::sky_color(float r, float g, float b)
212 obj.set_sky_color(Color(r, g, b));
215 void Lighting::Loader::zenith_direction(float x, float y, float z)
217 obj.set_zenith_direction(Vector3(x, y, z));
219 #pragma GCC diagnostic pop