3 #include <msp/core/algorithm.h>
4 #include <msp/datafile/collection.h>
5 #include <msp/fs/utils.h>
6 #include <msp/strings/format.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);
24 void Lighting::set_ambient(const Color &a)
27 shdata.uniform("ambient_color", ambient);
30 void Lighting::set_sky_color(const Color &s)
33 shdata.uniform("sky_color", sky_color);
36 void Lighting::set_zenith_direction(const Vector3 &d)
39 shdata.uniform("world_zenith_dir", zenith_direction);
42 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
45 shdata.uniform("horizon_limit", horizon_angle.radians());
48 void Lighting::set_fog_color(const Color &c)
51 shdata.uniform("fog_color", fog_color);
54 void Lighting::set_fog_density(float d)
57 throw invalid_argument("Lighting::set_fog_density");
60 shdata.uniform("fog_density", 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(const Light &l)
70 if(find_member(lights, &l, &AttachedLight::light)==lights.end())
74 void Lighting::detach(const Light &l)
76 auto i = find_member(lights, &l, &AttachedLight::light);
81 void Lighting::detach(unsigned i)
86 detach(*lights[i].light);
89 const Light *Lighting::get_attached_light(unsigned i) const
91 return i<lights.size() ? lights[i].light : 0;
94 void Lighting::update_shader_data(ProgramData &sd, const Matrix &) const
96 sd.uniform("ambient_color", ambient);
97 sd.uniform("sky_color", sky_color);
98 sd.uniform("world_zenith_dir", zenith_direction);
99 sd.uniform("horizon_limit", horizon_angle.radians());
100 sd.uniform("fog_color", fog_color);
101 sd.uniform("fog_density", fog_density);
103 for(unsigned i=0; i<lights.size(); ++i)
105 lights[i].light->update_shader_data(sd, i);
108 const ProgramData &Lighting::get_shader_data() const
110 for(unsigned i=0; i<lights.size(); ++i)
111 if(lights[i].light->get_generation()!=lights[i].generation)
113 lights[i].light->update_shader_data(shdata, i);
114 lights[i].generation = lights[i].light->get_generation();
120 void Lighting::set_debug_name(const string &name)
123 shdata.set_debug_name(name+" [UBO]");
130 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
132 Lighting::Loader::Loader(Lighting &l):
133 CollectionObjectLoader<Lighting>(l, 0)
135 set_actions(shared_actions);
138 Lighting::Loader::Loader(Lighting &l, Collection &c):
139 CollectionObjectLoader<Lighting>(l, &c)
141 set_actions(shared_actions);
144 void Lighting::Loader::init_actions()
146 add("ambient", &Loader::ambient);
147 add("fog_color", &Loader::fog_color);
148 add("fog_density", &Loader::fog_density);
149 add("fog_half_distance", &Loader::fog_half_distance);
150 add("light", &Loader::light);
151 add("light", &Loader::light_inline);
154 add("horizon_angle", &Loader::horizon_angle);
155 add("light", &Loader::light_inline_index);
156 add("sky_color", &Loader::sky_color);
157 add("zenith_direction", &Loader::zenith_direction);
160 void Lighting::Loader::ambient(float r, float g, float b)
162 obj.set_ambient(Color(r, g, b));
165 void Lighting::Loader::fog_color(float r, float g, float b)
167 obj.set_fog_color(Color(r, g, b));
170 void Lighting::Loader::fog_density(float d)
172 obj.set_fog_density(d);
175 void Lighting::Loader::fog_half_distance(float d)
177 obj.set_fog_half_distance(d);
180 #pragma GCC diagnostic push
181 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
182 void Lighting::Loader::horizon_angle(float a)
184 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
186 #pragma GCC diagnostic pop
188 void Lighting::Loader::light(const string &name)
190 obj.attach(get_collection().get<Light>(name));
193 void Lighting::Loader::light_inline()
195 RefPtr<Light> lgt = new Light;
197 get_collection().add(format("%s/%d.light", FS::basename(get_source()), obj.lights.size()), lgt.get());
198 obj.attach(*lgt.release());
201 void Lighting::Loader::light_inline_index(unsigned)
206 #pragma GCC diagnostic push
207 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
208 void Lighting::Loader::sky_color(float r, float g, float b)
210 obj.set_sky_color(Color(r, g, b));
213 void Lighting::Loader::zenith_direction(float x, float y, float z)
215 obj.set_zenith_direction(Vector3(x, y, z));
217 #pragma GCC diagnostic pop