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