]> git.tdb.fi Git - libs/gl.git/blob - source/lighting.cpp
Check and report the use of legacy built-in variables in a program
[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 }
24
25 void Lighting::detach(unsigned i)
26 {
27         if(i>=lights.size())
28                 return;
29
30         lights[i] = 0;
31 }
32
33 void Lighting::bind() const
34 {
35         if(!set_current(this))
36                 return;
37
38         enable(GL_LIGHTING);
39         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
40         for(unsigned i=0; i<lights.size(); ++i)
41                 if(lights[i])
42                         lights[i]->bind_to(i);
43 }
44
45 void Lighting::unbind()
46 {
47         const Lighting *old = current();
48         if(!set_current(0))
49                 return;
50
51         for(unsigned i=0; i<old->lights.size(); ++i)
52                 if(old->lights[i])
53                         Light::unbind_from(i);
54
55         disable(GL_LIGHTING);
56 }
57
58 } // namespace GL
59 } // namespace Msp