3 #include <msp/core/algorithm.h>
17 zenith_direction(0, 0, 1),
18 horizon_angle(Geometry::Angle<float>::zero()),
19 fog_color(0.0f, 0.0f, 0.0f, 0.0f),
25 for(vector<Light *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
29 void Lighting::set_ambient(const Color &a)
34 void Lighting::set_sky_color(const Color &s)
39 void Lighting::set_zenith_direction(const Vector3 &d)
44 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
49 void Lighting::set_fog_color(const Color &c)
54 void Lighting::set_fog_density(float d)
57 throw invalid_argument("Lighting::set_fog_density");
62 void Lighting::set_fog_half_distance(float d)
64 set_fog_density(-log(pow(0.5, 1.0/d)));
67 void Lighting::attach(const Light &l)
69 if(find(lights, &l)==lights.end())
73 void Lighting::detach(const Light &l)
75 vector<const Light *>::iterator i = find(lights, &l);
80 void Lighting::detach(unsigned i)
88 const Light *Lighting::get_attached_light(unsigned i) const
90 return i<lights.size() ? lights[i] : 0;
93 void Lighting::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
95 shdata.uniform("ambient_color", ambient);
96 shdata.uniform("sky_color", sky_color);
97 shdata.uniform("eye_zenith_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
98 shdata.uniform("horizon_limit", horizon_angle.radians());
99 shdata.uniform("fog_color", fog_color);
100 shdata.uniform("fog_density", fog_density);
102 // For backwards compatibility
103 shdata.uniform("eye_sky_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
105 for(unsigned i=0; i<lights.size(); ++i)
107 lights[i]->update_shader_data(shdata, view_matrix, i);
111 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
113 Lighting::Loader::Loader(Lighting &l):
114 DataFile::ObjectLoader<Lighting>(l)
116 set_actions(shared_actions);
119 void Lighting::Loader::init_actions()
121 add("ambient", &Loader::ambient);
122 add("fog_color", &Loader::fog_color);
123 add("fog_density", &Loader::fog_density);
124 add("fog_half_distance", &Loader::fog_half_distance);
125 add("horizon_angle", &Loader::horizon_angle);
126 add("light", &Loader::light);
127 add("sky_color", &Loader::sky_color);
128 add("zenith_direction", &Loader::zenith_direction);
131 add("light", &Loader::light_index);
134 void Lighting::Loader::ambient(float r, float g, float b)
136 obj.ambient = Color(r, g, b);
139 void Lighting::Loader::fog_color(float r, float g, float b)
141 obj.set_fog_color(Color(r, g, b));
144 void Lighting::Loader::fog_density(float d)
146 obj.set_fog_density(d);
149 void Lighting::Loader::fog_half_distance(float d)
151 obj.set_fog_half_distance(d);
154 void Lighting::Loader::horizon_angle(float a)
156 obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
159 void Lighting::Loader::light()
161 RefPtr<Light> lgt = new Light;
164 obj.owned_data.push_back(lgt.release());
167 void Lighting::Loader::light_index(unsigned)
172 void Lighting::Loader::sky_color(float r, float g, float b)
174 obj.set_sky_color(Color(r, g, b));
177 void Lighting::Loader::zenith_direction(float x, float y, float z)
179 obj.set_zenith_direction(Vector3(x, y, z));