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