]> git.tdb.fi Git - libs/gl.git/blob - source/materials/lighting.cpp
Convert Lighting to use shared actions
[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 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
106
107 Lighting::Loader::Loader(Lighting &l):
108         DataFile::ObjectLoader<Lighting>(l)
109 {
110         set_actions(shared_actions);
111 }
112
113 void Lighting::Loader::init_actions()
114 {
115         add("ambient", &Loader::ambient);
116         add("fog_color", &Loader::fog_color);
117         add("fog_density", &Loader::fog_density);
118         add("fog_half_distance", &Loader::fog_half_distance);
119         add("horizon_angle", &Loader::horizon_angle);
120         add("light", &Loader::light);
121         add("sky_color", &Loader::sky_color);
122         add("zenith_direction", &Loader::zenith_direction);
123 }
124
125 void Lighting::Loader::ambient(float r, float g, float b)
126 {
127         obj.ambient = Color(r, g, b);
128 }
129
130 void Lighting::Loader::fog_color(float r, float g, float b)
131 {
132         obj.set_fog_color(Color(r, g, b));
133 }
134
135 void Lighting::Loader::fog_density(float d)
136 {
137         obj.set_fog_density(d);
138 }
139
140 void Lighting::Loader::fog_half_distance(float d)
141 {
142         obj.set_fog_half_distance(d);
143 }
144
145 void Lighting::Loader::horizon_angle(float a)
146 {
147         obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
148 }
149
150 void Lighting::Loader::light(unsigned i)
151 {
152         RefPtr<Light> lgt = new Light;
153         load_sub(*lgt);
154         obj.attach(i, *lgt);
155         obj.owned_data.push_back(lgt.release());
156 }
157
158 void Lighting::Loader::sky_color(float r, float g, float b)
159 {
160         obj.set_sky_color(Color(r, g, b));
161 }
162
163 void Lighting::Loader::zenith_direction(float x, float y, float z)
164 {
165         obj.set_zenith_direction(Vector3(x, y, z));
166 }
167
168 } // namespace GL
169 } // namespace Msp