]> git.tdb.fi Git - libs/gl.git/blob - source/materials/light.cpp
Some more cleanup of includes and forward declarations
[libs/gl.git] / source / materials / light.cpp
1 #include <stdexcept>
2 #include <msp/strings/format.h>
3 #include "light.h"
4 #include "programdata.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Light::Light():
12         color(1),
13         transmittance(1),
14         position(0, 0, 1, 0),
15         spot_dir(0, 0, -1),
16         spot_exp(0),
17         spot_cutoff(Geometry::Angle<float>::straight()),
18         generation(0)
19 {
20         attenuation[0] = 1;
21         attenuation[1] = 0;
22         attenuation[2] = 0;
23 }
24
25 void Light::update_matrix()
26 {
27         Vector3 up_dir;
28         if(20*abs(direction.z)>abs(direction.x)+abs(direction.y))
29                 up_dir.y = 1;
30         else
31                 up_dir.z = 1;
32         Vector3 right_dir = normalize(cross(direction, up_dir));
33
34         Vector4 columns[4];
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);
40 }
41
42 void Light::set_color(const Color &c)
43 {
44         color = c;
45         ++generation;
46 }
47
48 void Light::set_transmittance(const Color &t)
49 {
50         transmittance = t;
51         ++generation;
52 }
53
54 void Light::set_matrix(const Matrix &m)
55 {
56         Placeable::set_matrix(m);
57         position = matrix.column(3);
58         spot_dir = normalize(-matrix.column(2).slice<3>(0));
59         direction = (position.w ? spot_dir : normalize(-position.slice<3>(0)));
60         update_matrix();
61         ++generation;
62 }
63
64 void Light::set_position(const Vector4 &p)
65 {
66         position = p;
67         if(!position.w)
68                 direction = normalize(-position.slice<3>(0));
69         update_matrix();
70         ++generation;
71 }
72
73 void Light::set_spot_direction(const Vector3 &d)
74 {
75         spot_dir = normalize(d);
76         if(position.w)
77                 direction = spot_dir;
78         update_matrix();
79         ++generation;
80 }
81
82 void Light::set_spot_exponent(float e)
83 {
84         if(e<0)
85                 throw invalid_argument("Light::set_spot_exponent");
86
87         spot_exp = e;
88         ++generation;
89 }
90
91 void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
92 {
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");
95
96         spot_cutoff = c;
97         ++generation;
98 }
99
100 void Light::disable_spot_cutoff()
101 {
102         set_spot_cutoff(Geometry::Angle<float>::straight());
103         ++generation;
104 }
105
106 void Light::set_attenuation(float c, float l, float q)
107 {
108         attenuation[0] = c;
109         attenuation[1] = l;
110         attenuation[2] = q;
111         ++generation;
112 }
113
114 void Light::update_shader_data(ProgramData &shdata, unsigned i) const
115 {
116         string base = format("light_sources[%d]", i);
117         shdata.uniform(base+".position", position);
118         shdata.uniform(base+".color", color.r*transmittance.r, color.g*transmittance.g, color.b*transmittance.b);
119         shdata.uniform(base+".enabled", 1);
120 }
121
122
123 Light::Loader::Loader(Light &l):
124         DataFile::ObjectLoader<Light>(l)
125 {
126         add("attenuation", &Loader::attenuation);
127         add("color", &Loader::color);
128         add("position", &Loader::position);
129         add("spot_direction", &Loader::spot_direction);
130         add("spot_exponent", &Loader::spot_exponent);
131         add("spot_cutoff", &Loader::spot_cutoff);
132
133         // Deprecated
134         add("diffuse", &Loader::color);
135         add("specular");
136 }
137
138 void Light::Loader::attenuation(float c, float l, float q)
139 {
140         obj.set_attenuation(c, l, q);
141 }
142
143 void Light::Loader::color(float r, float g, float b)
144 {
145         obj.set_color(Color(r, g, b));
146 }
147
148 void Light::Loader::position(float x, float y, float z, float w)
149 {
150         obj.set_position(Vector4(x, y, z, w));
151 }
152
153 void Light::Loader::spot_direction(float x, float y, float z)
154 {
155         obj.set_spot_direction(Vector3(x, y, z));
156 }
157
158 void Light::Loader::spot_exponent(float e)
159 {
160         obj.set_spot_exponent(e);
161 }
162
163 void Light::Loader::spot_cutoff(float c)
164 {
165         obj.set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));
166 }
167
168 } // namespace GL
169 } // namespace Msp