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 set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
19 set_fog_density(0.0f);
21 for(unsigned i=0; i<7; ++i)
23 string base = format("light_sources[%d]", i);
24 shdata.uniform(base+".position", Vector4(0, 0, 1, 0));
25 shdata.uniform(base+".color", 0.0f, 0.0f, 0.0f);
26 shdata.uniform(base+".type", 0);
27 shdata.uniform(base+".attenuation", 1.0f, 0.0f, 0.0f);
31 void Lighting::set_ambient(const Color &a)
34 shdata.uniform("ambient_color", ambient);
37 void Lighting::set_fog_color(const Color &c)
40 shdata.uniform("fog_color", fog_color);
43 void Lighting::set_fog_density(float d)
46 throw invalid_argument("Lighting::set_fog_density");
49 shdata.uniform("fog_density", fog_density);
52 void Lighting::set_fog_half_distance(float d)
54 set_fog_density(-log(pow(0.5, 1.0/d)));
57 void Lighting::attach(const Light &l)
59 if(find_member(lights, &l, &AttachedLight::light)==lights.end())
63 void Lighting::detach(const Light &l)
65 auto i = find_member(lights, &l, &AttachedLight::light);
69 shdata.uniform(format("light_sources[%d].enabled", lights.size()), 0);
73 int Lighting::find_light_index(const Light &l) const
75 auto i = find_member(lights, &l, &AttachedLight::light);
76 return (i!=lights.end() ? i-lights.begin() : -1);
79 const ProgramData &Lighting::get_shader_data() const
81 for(unsigned i=0; i<lights.size(); ++i)
82 if(lights[i].light->get_generation()!=lights[i].generation)
84 lights[i].light->update_shader_data(shdata, i);
85 lights[i].generation = lights[i].light->get_generation();
91 void Lighting::set_debug_name(const string &name)
94 shdata.set_debug_name(name+" [UBO]");
101 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
103 Lighting::Loader::Loader(Lighting &l, Collection &c):
104 CollectionObjectLoader<Lighting>(l, &c)
106 set_actions(shared_actions);
109 void Lighting::Loader::init_actions()
111 add("ambient", &Loader::ambient);
112 add("fog_color", &Loader::fog_color);
113 add("fog_density", &Loader::fog_density);
114 add("fog_half_distance", &Loader::fog_half_distance);
115 add("light", &Loader::light);
116 add("light", &Loader::light_inline);
119 void Lighting::Loader::ambient(float r, float g, float b)
121 obj.set_ambient(Color(r, g, b));
124 void Lighting::Loader::fog_color(float r, float g, float b)
126 obj.set_fog_color(Color(r, g, b));
129 void Lighting::Loader::fog_density(float d)
131 obj.set_fog_density(d);
134 void Lighting::Loader::fog_half_distance(float d)
136 obj.set_fog_half_distance(d);
139 void Lighting::Loader::light(const string &name)
141 obj.attach(get_collection().get<Light>(name));
144 void Lighting::Loader::light_inline()
146 Light::GenericLoader ldr(get_collection());
148 Light *lgt = ldr.store_object(get_collection(), format("%s/%d.light", FS::basename(get_source()), obj.lights.size()));