]> git.tdb.fi Git - libs/gl.git/blob - source/lighting.cpp
Better state tracking for bound objects
[libs/gl.git] / source / lighting.cpp
1 #include "light.h"
2 #include "lighting.h"
3 #include "misc.h"
4
5 namespace Msp {
6 namespace GL {
7
8 Lighting::Lighting():
9         ambient(0.2)
10 { }
11
12 void Lighting::set_ambient(const Color &a)
13 {
14         ambient = a;
15 }
16
17 void Lighting::attach(unsigned i, const Light &l)
18 {
19         if(i>=lights.size())
20                 lights.resize(i+1);
21
22         lights[i] = &l;
23         if(current()==this)
24                 l.bind_to(i);
25 }
26
27 void Lighting::detach(unsigned i)
28 {
29         if(i>=lights.size())
30                 return;
31
32         lights[i] = 0;
33         if(current()==this)
34                 Light::unbind_from(i);
35 }
36
37 void Lighting::bind() const
38 {
39         if(!set_current(this))
40                 return;
41
42         enable(GL_LIGHTING);
43         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
44         for(unsigned i=0; i<lights.size(); ++i)
45                 if(lights[i])
46                         lights[i]->bind_to(i);
47 }
48
49 void Lighting::unbind()
50 {
51         const Lighting *old = current();
52         if(!set_current(0))
53                 return;
54
55         for(unsigned i=0; i<old->lights.size(); ++i)
56                 if(old->lights[i])
57                         Light::unbind_from(i);
58
59         disable(GL_LIGHTING);
60 }
61
62 } // namespace GL
63 } // namespace Msp