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