]> git.tdb.fi Git - libs/gl.git/blob - source/materials/light.cpp
Rearrange soucre files into subdirectories
[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         diffuse(1),
15         specular(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 {
21         attenuation[0] = 1;
22         attenuation[1] = 0;
23         attenuation[2] = 0;
24 }
25
26 void Light::update_matrix()
27 {
28         Vector3 up_dir;
29         if(20*abs(direction.z)>abs(direction.x)+abs(direction.y))
30                 up_dir.y = 1;
31         else
32                 up_dir.z = 1;
33         Vector3 right_dir = normalize(cross(direction, up_dir));
34
35         Vector4 columns[4];
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);
41 }
42
43 void Light::set_diffuse(const Color &c)
44 {
45         diffuse = c;
46 }
47
48 void Light::set_specular(const Color &c)
49 {
50         specular = c;
51 }
52
53 void Light::set_matrix(const Matrix &m)
54 {
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)));
59         update_matrix();
60 }
61
62 void Light::set_position(const Vector4 &p)
63 {
64         position = p;
65         if(!position.w)
66                 direction = normalize(-position.slice<3>(0));
67         update_matrix();
68 }
69
70 void Light::set_spot_direction(const Vector3 &d)
71 {
72         spot_dir = normalize(d);
73         if(position.w)
74                 direction = spot_dir;
75         update_matrix();
76 }
77
78 void Light::set_spot_exponent(float e)
79 {
80         if(e<0)
81                 throw invalid_argument("Light::set_spot_exponent");
82
83         spot_exp = e;
84 }
85
86 void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
87 {
88         if(c<Geometry::Angle<float>::zero() || (c>Geometry::Angle<float>::right() && c!=Geometry::Angle<float>::straight()))
89                 throw invalid_argument("Light::set_spot_cutoff");
90
91         spot_cutoff = c;
92 }
93
94 void Light::disable_spot_cutoff()
95 {
96         set_spot_cutoff(Geometry::Angle<float>::straight());
97 }
98
99 void Light::set_attenuation(float c, float l, float q)
100 {
101         attenuation[0] = c;
102         attenuation[1] = l;
103         attenuation[2] = q;
104 }
105
106 void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
107 {
108         string base = format("light_sources[%d]", i);
109         shdata.uniform(base+".position", view_matrix*position);
110         shdata.uniform(base+".diffuse", diffuse);
111         shdata.uniform(base+".specular", specular);
112 }
113
114
115 Light::Loader::Loader(Light &l):
116         DataFile::ObjectLoader<Light>(l)
117 {
118         add("attenuation", &Loader::attenuation);
119         add("diffuse", &Loader::diffuse);
120         add("position", &Loader::position);
121         add("specular", &Loader::specular);
122         add("spot_direction", &Loader::spot_direction);
123         add("spot_exponent", &Loader::spot_exponent);
124         add("spot_cutoff", &Loader::spot_cutoff);
125 }
126
127 void Light::Loader::attenuation(float c, float l, float q)
128 {
129         obj.set_attenuation(c, l, q);
130 }
131
132 void Light::Loader::diffuse(float r, float g, float b)
133 {
134         obj.set_diffuse(Color(r, g, b));
135 }
136
137 void Light::Loader::position(float x, float y, float z, float w)
138 {
139         obj.set_position(Vector4(x, y, z, w));
140 }
141
142 void Light::Loader::specular(float r, float g, float b)
143 {
144         obj.set_specular(Color(r, g, b));
145 }
146
147 void Light::Loader::spot_direction(float x, float y, float z)
148 {
149         obj.set_spot_direction(Vector3(x, y, z));
150 }
151
152 void Light::Loader::spot_exponent(float e)
153 {
154         obj.set_spot_exponent(e);
155 }
156
157 void Light::Loader::spot_cutoff(float c)
158 {
159         obj.set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));
160 }
161
162 } // namespace GL
163 } // namespace Msp