2 #include <msp/strings/format.h>
6 #include "programdata.h"
18 spot_cutoff(Geometry::Angle<float>::straight())
25 void Light::update_matrix()
28 if(20*abs(direction.z)>abs(direction.x)+abs(direction.y))
32 Vector3 right_dir = normalize(cross(direction, up_dir));
35 columns[0] = compose(right_dir, 0.0f);
36 columns[1] = compose(cross(right_dir, direction), 0.0f);
37 columns[2] = compose(-direction, 0.0f);
38 columns[3] = position;
39 matrix = Matrix::from_columns(columns);
42 void Light::set_color(const Color &c)
47 void Light::set_matrix(const Matrix &m)
49 Placeable::set_matrix(m);
50 position = matrix.column(3);
51 spot_dir = normalize(-matrix.column(2).slice<3>(0));
52 direction = (position.w ? spot_dir : normalize(-position.slice<3>(0)));
56 void Light::set_position(const Vector4 &p)
60 direction = normalize(-position.slice<3>(0));
64 void Light::set_spot_direction(const Vector3 &d)
66 spot_dir = normalize(d);
72 void Light::set_spot_exponent(float e)
75 throw invalid_argument("Light::set_spot_exponent");
80 void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
82 if(c<Geometry::Angle<float>::zero() || (c>Geometry::Angle<float>::right() && c!=Geometry::Angle<float>::straight()))
83 throw invalid_argument("Light::set_spot_cutoff");
88 void Light::disable_spot_cutoff()
90 set_spot_cutoff(Geometry::Angle<float>::straight());
93 void Light::set_attenuation(float c, float l, float q)
100 void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
102 string base = format("light_sources[%d]", i);
103 shdata.uniform(base+".position", view_matrix*position);
104 shdata.uniform(base+".color", color.r, color.g, color.b);
108 Light::Loader::Loader(Light &l):
109 DataFile::ObjectLoader<Light>(l)
111 add("attenuation", &Loader::attenuation);
112 add("color", &Loader::color);
113 add("position", &Loader::position);
114 add("spot_direction", &Loader::spot_direction);
115 add("spot_exponent", &Loader::spot_exponent);
116 add("spot_cutoff", &Loader::spot_cutoff);
119 add("diffuse", &Loader::color);
123 void Light::Loader::attenuation(float c, float l, float q)
125 obj.set_attenuation(c, l, q);
128 void Light::Loader::color(float r, float g, float b)
130 obj.set_color(Color(r, g, b));
133 void Light::Loader::position(float x, float y, float z, float w)
135 obj.set_position(Vector4(x, y, z, w));
138 void Light::Loader::spot_direction(float x, float y, float z)
140 obj.set_spot_direction(Vector3(x, y, z));
143 void Light::Loader::spot_exponent(float e)
145 obj.set_spot_exponent(e);
148 void Light::Loader::spot_cutoff(float c)
150 obj.set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));