3 #include <msp/core/algorithm.h>
4 #include <msp/fs/utils.h>
17 zenith_direction(0, 0, 1),
18 horizon_angle(Geometry::Angle<float>::zero())
21 set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
22 set_fog_density(0.0f);
25 void Lighting::set_ambient(const Color &a)
28 shdata.uniform("ambient_color", ambient);
31 void Lighting::set_sky_color(const Color &s)
34 shdata.uniform("sky_color", sky_color);
37 void Lighting::set_zenith_direction(const Vector3 &d)
40 shdata.uniform("world_zenith_dir", zenith_direction);
43 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
46 shdata.uniform("horizon_limit", horizon_angle.radians());
49 void Lighting::set_fog_color(const Color &c)
52 shdata.uniform("fog_color", fog_color);
55 void Lighting::set_fog_density(float d)
58 throw invalid_argument("Lighting::set_fog_density");
61 shdata.uniform("fog_density", fog_density);
64 void Lighting::set_fog_half_distance(float d)
66 set_fog_density(-log(pow(0.5, 1.0/d)));
69 void Lighting::attach(const Light &l)
71 if(find_member(lights, &l, &AttachedLight::light)==lights.end())
75 void Lighting::detach(const Light &l)
77 auto i = find_member(lights, &l, &AttachedLight::light);
82 void Lighting::detach(unsigned i)
87 detach(*lights[i].light);
90 const Light *Lighting::get_attached_light(unsigned i) const
92 return i<lights.size() ? lights[i].light : 0;
95 void Lighting::update_shader_data(ProgramData &sd, const Matrix &) const
97 sd.uniform("ambient_color", ambient);
98 sd.uniform("sky_color", sky_color);
99 sd.uniform("world_zenith_dir", zenith_direction);
100 sd.uniform("horizon_limit", horizon_angle.radians());
101 sd.uniform("fog_color", fog_color);
102 sd.uniform("fog_density", fog_density);
104 for(unsigned i=0; i<lights.size(); ++i)
106 lights[i].light->update_shader_data(sd, i);
109 const ProgramData &Lighting::get_shader_data() const
111 for(unsigned i=0; i<lights.size(); ++i)
112 if(lights[i].light->get_generation()!=lights[i].generation)
114 lights[i].light->update_shader_data(shdata, i);
115 lights[i].generation = lights[i].light->get_generation();
121 void Lighting::set_debug_name(const string &name)
124 shdata.set_debug_name(name+" [UBO]");
131 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
133 Lighting::Loader::Loader(Lighting &l):
134 CollectionObjectLoader<Lighting>(l, 0)
136 set_actions(shared_actions);
139 Lighting::Loader::Loader(Lighting &l, Collection &c):
140 CollectionObjectLoader<Lighting>(l, &c)
142 set_actions(shared_actions);
145 void Lighting::Loader::init_actions()
147 add("ambient", &Loader::ambient);
148 add("fog_color", &Loader::fog_color);
149 add("fog_density", &Loader::fog_density);
150 add("fog_half_distance", &Loader::fog_half_distance);
151 add("light", &Loader::light);
152 add("light", &Loader::light_inline);
155 add("horizon_angle", &Loader::horizon_angle);
156 add("light", &Loader::light_inline_index);
157 add("sky_color", &Loader::sky_color);
158 add("zenith_direction", &Loader::zenith_direction);
161 void Lighting::Loader::ambient(float r, float g, float b)
163 obj.set_ambient(Color(r, g, b));
166 void Lighting::Loader::fog_color(float r, float g, float b)
168 obj.set_fog_color(Color(r, g, b));
171 void Lighting::Loader::fog_density(float d)
173 obj.set_fog_density(d);
176 void Lighting::Loader::fog_half_distance(float d)
178 obj.set_fog_half_distance(d);
181 #pragma GCC diagnostic push
182 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
183 void Lighting::Loader::horizon_angle(float a)
185 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
187 #pragma GCC diagnostic pop
189 void Lighting::Loader::light(const string &name)
191 obj.attach(get_collection().get<Light>(name));
194 void Lighting::Loader::light_inline()
196 RefPtr<Light> lgt = new Light;
198 get_collection().add(format("%s/%d.light", FS::basename(get_source()), obj.lights.size()), lgt.get());
199 obj.attach(*lgt.release());
202 void Lighting::Loader::light_inline_index(unsigned)
207 #pragma GCC diagnostic push
208 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
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));
218 #pragma GCC diagnostic pop