]> git.tdb.fi Git - libs/gl.git/blob - source/lighting.cpp
Make desertpillars compatible with the modernization changes
[libs/gl.git] / source / lighting.cpp
1 #include <stdexcept>
2 #include "light.h"
3 #include "lighting.h"
4 #include "lightunit.h"
5 #include "misc.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 Lighting::Lighting():
13         ambient(0.2)
14 { }
15
16 void Lighting::set_ambient(const Color &a)
17 {
18         ambient = a;
19 }
20
21 void Lighting::attach(unsigned i, const Light &l)
22 {
23         if(i>=LightUnit::get_n_units())
24                 throw out_of_range("Lighting::attach");
25
26         if(i>=lights.size())
27                 lights.resize(i+1);
28
29         lights[i] = &l;
30         if(current()==this)
31                 l.bind_to(i);
32 }
33
34 void Lighting::detach(unsigned i)
35 {
36         if(i>=lights.size())
37                 return;
38
39         lights[i] = 0;
40         if(current()==this)
41                 Light::unbind_from(i);
42 }
43
44 void Lighting::update_shader_data(const Matrix &view_matrix)
45 {
46         shdata.uniform("ambient_color", ambient);
47         for(unsigned i=0; i<lights.size(); ++i)
48                 if(lights[i])
49                         lights[i]->update_shader_data(shdata, view_matrix, i);
50 }
51
52 void Lighting::bind() const
53 {
54         if(!set_current(this))
55                 return;
56
57         enable(GL_LIGHTING);
58         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
59         for(unsigned i=0; i<lights.size(); ++i)
60                 if(lights[i])
61                         lights[i]->bind_to(i);
62 }
63
64 void Lighting::unbind()
65 {
66         const Lighting *old = current();
67         if(!set_current(0))
68                 return;
69
70         for(unsigned i=0; i<old->lights.size(); ++i)
71                 if(old->lights[i])
72                         Light::unbind_from(i);
73
74         disable(GL_LIGHTING);
75 }
76
77 } // namespace GL
78 } // namespace Msp