]> git.tdb.fi Git - libs/gl.git/blob - source/light.cpp
Convert Light to object model
[libs/gl.git] / source / light.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "except.h"
9 #include "light.h"
10 #include "misc.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Light::Light():
18         ambient(0),
19         diffuse(1),
20         specular(1),
21         x(0), y(0), z(1), w(0),
22         sdx(0), sdy(0), sdz(-1),
23         spot_exp(0),
24         spot_cutoff(180)
25 { }
26
27 void Light::set_ambient(const Color &c)
28 {
29         ambient=c;
30 }
31
32 void Light::set_diffuse(const Color &c)
33 {
34         diffuse=c;
35 }
36
37 void Light::set_specular(const Color &c)
38 {
39         specular=c;
40 }
41
42 void Light::set_position(float x_, float y_, float z_, float w_)
43 {
44         x=x_;
45         y=y_;
46         z=z_;
47         w=w_;
48 }
49
50 void Light::bind() const
51 {
52         if(current_lights[current_unit]!=this)
53         {
54                 GLenum l=GL_LIGHT0+current_unit;
55                 enable(l);
56                 glLightfv(l, GL_AMBIENT, &ambient.r);
57                 glLightfv(l, GL_DIFFUSE, &diffuse.r);
58                 glLightfv(l, GL_SPECULAR, &specular.r);
59                 glLightfv(l, GL_POSITION, &x);
60                 current_lights[current_unit]=this;
61         }
62 }
63
64 void Light::bind_to(unsigned i) const
65 {
66         activate(i);
67         bind();
68 }
69
70 void Light::activate(unsigned i)
71 {
72         static unsigned max_lights=get_i(GL_MAX_LIGHTS);
73
74         if(i>=max_lights)
75                 throw InvalidParameterValue("Light unit index out of range");
76
77         if(i>=current_lights.size())
78                 current_lights.resize(i+1);
79
80         current_unit=i;
81 }
82
83 void Light::unbind()
84 {
85         if(current_lights[current_unit])
86         {
87                 disable(GL_LIGHT0+current_unit);
88                 current_lights[current_unit]=0;
89         }
90 }
91
92 unsigned Light::current_unit=0;
93 vector<const Light *> Light::current_lights(1);
94
95 } // namespace GL
96 } // namespace Msp