]> git.tdb.fi Git - libs/gl.git/blob - source/materials/lighting.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / materials / lighting.cpp
1 #include <stdexcept>
2 #include <cmath>
3 #include "error.h"
4 #include "light.h"
5 #include "lighting.h"
6 #include "matrix.h"
7 #include "misc.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Lighting::Lighting():
15         ambient(0.2),
16         zenith_direction(0, 0, 1),
17         horizon_angle(Geometry::Angle<float>::zero()),
18         fog_color(0.0f, 0.0f, 0.0f, 0.0f),
19         fog_density(0.0f)
20 { }
21
22 Lighting::~Lighting()
23 {
24         for(vector<Light *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
25                 delete *i;
26 }
27
28 void Lighting::set_ambient(const Color &a)
29 {
30         ambient = a;
31 }
32
33 void Lighting::set_sky_color(const Color &s)
34 {
35         sky_color = s;
36 }
37
38 void Lighting::set_zenith_direction(const Vector3 &d)
39 {
40         zenith_direction = d;
41 }
42
43 void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
44 {
45         horizon_angle = a;
46 }
47
48 void Lighting::set_fog_color(const Color &c)
49 {
50         fog_color = c;
51 }
52
53 void Lighting::set_fog_density(float d)
54 {
55         if(d<0)
56                 throw invalid_argument("Lighting::set_fog_density");
57
58         fog_density = d;
59 }
60
61 void Lighting::set_fog_half_distance(float d)
62 {
63         set_fog_density(-log(pow(0.5, 1.0/d)));
64 }
65
66 void Lighting::attach(unsigned i, const Light &l)
67 {
68         if(i>=lights.size())
69                 lights.resize(i+1);
70
71         lights[i] = &l;
72 }
73
74 void Lighting::detach(unsigned i)
75 {
76         if(i>=lights.size())
77                 return;
78
79         lights[i] = 0;
80 }
81
82 const Light *Lighting::get_attached_light(unsigned i) const
83 {
84         return i<lights.size() ? lights[i] : 0;
85 }
86
87 void Lighting::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
88 {
89         shdata.uniform("ambient_color", ambient);
90         shdata.uniform("sky_color", sky_color);
91         shdata.uniform("eye_zenith_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
92         shdata.uniform("horizon_limit", horizon_angle.radians());
93         shdata.uniform("fog_color", fog_color);
94         shdata.uniform("fog_density", fog_density);
95
96         // For backwards compatibility
97         shdata.uniform("eye_sky_dir", view_matrix.block<3, 3>(0, 0)*zenith_direction);
98
99         for(unsigned i=0; i<lights.size(); ++i)
100                 if(lights[i])
101                         lights[i]->update_shader_data(shdata, view_matrix, i);
102 }
103
104
105 Lighting::Loader::Loader(Lighting &l):
106         DataFile::ObjectLoader<Lighting>(l)
107 {
108         add("ambient", &Loader::ambient);
109         add("fog_color", &Loader::fog_color);
110         add("fog_density", &Loader::fog_density);
111         add("fog_half_distance", &Loader::fog_half_distance);
112         add("horizon_angle", &Loader::horizon_angle);
113         add("light", &Loader::light);
114         add("sky_color", &Loader::sky_color);
115         add("zenith_direction", &Loader::zenith_direction);
116 }
117
118 void Lighting::Loader::ambient(float r, float g, float b)
119 {
120         obj.ambient = Color(r, g, b);
121 }
122
123 void Lighting::Loader::fog_color(float r, float g, float b)
124 {
125         obj.set_fog_color(Color(r, g, b));
126 }
127
128 void Lighting::Loader::fog_density(float d)
129 {
130         obj.set_fog_density(d);
131 }
132
133 void Lighting::Loader::fog_half_distance(float d)
134 {
135         obj.set_fog_half_distance(d);
136 }
137
138 void Lighting::Loader::horizon_angle(float a)
139 {
140         obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
141 }
142
143 void Lighting::Loader::light(unsigned i)
144 {
145         RefPtr<Light> lgt = new Light;
146         load_sub(*lgt);
147         obj.attach(i, *lgt);
148         obj.owned_data.push_back(lgt.release());
149 }
150
151 void Lighting::Loader::sky_color(float r, float g, float b)
152 {
153         obj.set_sky_color(Color(r, g, b));
154 }
155
156 void Lighting::Loader::zenith_direction(float x, float y, float z)
157 {
158         obj.set_zenith_direction(Vector3(x, y, z));
159 }
160
161 } // namespace GL
162 } // namespace Msp