2 #include <msp/strings/format.h>
6 #include "programdata.h"
19 spot_cutoff(Geometry::Angle<float>::straight())
26 void Light::update_matrix()
29 if(20*abs(direction.z)>abs(direction.x)+abs(direction.y))
33 Vector3 right_dir = normalize(cross(direction, up_dir));
36 columns[0] = compose(right_dir, 0.0f);
37 columns[1] = compose(cross(right_dir, direction), 0.0f);
38 columns[2] = compose(-direction, 0.0f);
39 columns[3] = position;
40 matrix = Matrix::from_columns(columns);
43 void Light::set_diffuse(const Color &c)
48 void Light::set_specular(const Color &c)
53 void Light::set_matrix(const Matrix &m)
55 Placeable::set_matrix(m);
56 position = matrix.column(3);
57 spot_dir = normalize(-matrix.column(2).slice<3>(0));
58 direction = (position.w ? spot_dir : normalize(-position.slice<3>(0)));
62 void Light::set_position(const Vector4 &p)
66 direction = normalize(-position.slice<3>(0));
70 void Light::set_spot_direction(const Vector3 &d)
72 spot_dir = normalize(d);
78 void Light::set_spot_exponent(float e)
81 throw invalid_argument("Light::set_spot_exponent");
86 void Light::set_spot_cutoff(float c)
88 set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));
91 void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
93 if(c<Geometry::Angle<float>::zero() || (c>Geometry::Angle<float>::right() && c!=Geometry::Angle<float>::straight()))
94 throw invalid_argument("Light::set_spot_cutoff");
99 void Light::disable_spot_cutoff()
101 set_spot_cutoff(Geometry::Angle<float>::straight());
104 void Light::set_attenuation(float c, float l, float q)
111 void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
113 string base = format("light_sources[%d]", i);
114 shdata.uniform(base+".position", view_matrix*position);
115 shdata.uniform(base+".diffuse", diffuse);
116 shdata.uniform(base+".specular", specular);
120 Light::Loader::Loader(Light &l):
121 DataFile::ObjectLoader<Light>(l)
123 add("attenuation", &Loader::attenuation);
124 add("diffuse", &Loader::diffuse);
125 add("position", &Loader::position);
126 add("specular", &Loader::specular);
127 add("spot_direction", &Loader::spot_direction);
128 add("spot_exponent", &Loader::spot_exponent);
129 add("spot_cutoff", &Loader::spot_cutoff);
132 void Light::Loader::attenuation(float c, float l, float q)
134 obj.set_attenuation(c, l, q);
137 void Light::Loader::diffuse(float r, float g, float b)
139 obj.set_diffuse(Color(r, g, b));
142 void Light::Loader::position(float x, float y, float z, float w)
144 obj.set_position(Vector4(x, y, z, w));
147 void Light::Loader::specular(float r, float g, float b)
149 obj.set_specular(Color(r, g, b));
152 void Light::Loader::spot_direction(float x, float y, float z)
154 obj.set_spot_direction(Vector3(x, y, z));
157 void Light::Loader::spot_exponent(float e)
159 obj.set_spot_exponent(e);
162 void Light::Loader::spot_cutoff(float c)
164 obj.set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));