]> git.tdb.fi Git - libs/gl.git/blob - source/materials/lighting.cpp
Remove deprecated interfaces from material and lighting code
[libs/gl.git] / source / materials / lighting.cpp
1 #include <stdexcept>
2 #include <cmath>
3 #include <msp/core/algorithm.h>
4 #include <msp/datafile/collection.h>
5 #include <msp/fs/utils.h>
6 #include <msp/strings/format.h>
7 #include "light.h"
8 #include "lighting.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Lighting::Lighting()
16 {
17         set_ambient(0.2f);
18         set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
19         set_fog_density(0.0f);
20 }
21
22 void Lighting::set_ambient(const Color &a)
23 {
24         ambient = a;
25         shdata.uniform("ambient_color", ambient);
26 }
27
28 void Lighting::set_fog_color(const Color &c)
29 {
30         fog_color = c;
31         shdata.uniform("fog_color", fog_color);
32 }
33
34 void Lighting::set_fog_density(float d)
35 {
36         if(d<0)
37                 throw invalid_argument("Lighting::set_fog_density");
38
39         fog_density = d;
40         shdata.uniform("fog_density", fog_density);
41 }
42
43 void Lighting::set_fog_half_distance(float d)
44 {
45         set_fog_density(-log(pow(0.5, 1.0/d)));
46 }
47
48 void Lighting::attach(const Light &l)
49 {
50         if(find_member(lights, &l, &AttachedLight::light)==lights.end())
51                 lights.push_back(&l);
52 }
53
54 void Lighting::detach(const Light &l)
55 {
56         auto i = find_member(lights, &l, &AttachedLight::light);
57         if(i!=lights.end())
58                 lights.erase(i);
59 }
60
61 const ProgramData &Lighting::get_shader_data() const
62 {
63         for(unsigned i=0; i<lights.size(); ++i)
64                 if(lights[i].light->get_generation()!=lights[i].generation)
65                 {
66                         lights[i].light->update_shader_data(shdata, i);
67                         lights[i].generation = lights[i].light->get_generation();
68                 }
69
70         return shdata;
71 }
72
73 void Lighting::set_debug_name(const string &name)
74 {
75 #ifdef DEBUG
76         shdata.set_debug_name(name+" [UBO]");
77 #else
78         (void)name;
79 #endif
80 }
81
82
83 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
84
85 Lighting::Loader::Loader(Lighting &l):
86         CollectionObjectLoader<Lighting>(l, 0)
87 {
88         set_actions(shared_actions);
89 }
90
91 Lighting::Loader::Loader(Lighting &l, Collection &c):
92         CollectionObjectLoader<Lighting>(l, &c)
93 {
94         set_actions(shared_actions);
95 }
96
97 void Lighting::Loader::init_actions()
98 {
99         add("ambient", &Loader::ambient);
100         add("fog_color", &Loader::fog_color);
101         add("fog_density", &Loader::fog_density);
102         add("fog_half_distance", &Loader::fog_half_distance);
103         add("light", &Loader::light);
104         add("light", &Loader::light_inline);
105 }
106
107 void Lighting::Loader::ambient(float r, float g, float b)
108 {
109         obj.set_ambient(Color(r, g, b));
110 }
111
112 void Lighting::Loader::fog_color(float r, float g, float b)
113 {
114         obj.set_fog_color(Color(r, g, b));
115 }
116
117 void Lighting::Loader::fog_density(float d)
118 {
119         obj.set_fog_density(d);
120 }
121
122 void Lighting::Loader::fog_half_distance(float d)
123 {
124         obj.set_fog_half_distance(d);
125 }
126
127 void Lighting::Loader::light(const string &name)
128 {
129         obj.attach(get_collection().get<Light>(name));
130 }
131
132 void Lighting::Loader::light_inline()
133 {
134         RefPtr<Light> lgt = new Light;
135         load_sub(*lgt);
136         get_collection().add(format("%s/%d.light", FS::basename(get_source()), obj.lights.size()), lgt.get());
137         obj.attach(*lgt.release());
138 }
139
140 } // namespace GL
141 } // namespace Msp