]> git.tdb.fi Git - libs/gl.git/blob - source/lighting.cpp
Use renderer for lighting in the shaders demo
[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::bind() const
45 {
46         if(!set_current(this))
47                 return;
48
49         enable(GL_LIGHTING);
50         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
51         for(unsigned i=0; i<lights.size(); ++i)
52                 if(lights[i])
53                         lights[i]->bind_to(i);
54 }
55
56 void Lighting::unbind()
57 {
58         const Lighting *old = current();
59         if(!set_current(0))
60                 return;
61
62         for(unsigned i=0; i<old->lights.size(); ++i)
63                 if(old->lights[i])
64                         Light::unbind_from(i);
65
66         disable(GL_LIGHTING);
67 }
68
69 } // namespace GL
70 } // namespace Msp