]> git.tdb.fi Git - libs/gl.git/blob - source/light.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / light.cpp
1 #include "except.h"
2 #include "light.h"
3 #include "misc.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Light::Light():
11         ambient(0),
12         diffuse(1),
13         specular(1),
14         position(0, 0, 1, 0),
15         spot_dir(0, 0, -1),
16         spot_exp(0),
17         spot_cutoff(180)
18 {
19         attenuation[0] = 1;
20         attenuation[1] = 0;
21         attenuation[2] = 0;
22 }
23
24 void Light::set_ambient(const Color &c)
25 {
26         ambient = c;
27 }
28
29 void Light::set_diffuse(const Color &c)
30 {
31         diffuse = c;
32 }
33
34 void Light::set_specular(const Color &c)
35 {
36         specular = c;
37 }
38
39 void Light::set_position(const Vector4 &p)
40 {
41         position = p;
42 }
43
44 void Light::set_spot_direction(const Vector3 &d)
45 {
46         spot_dir = d;
47 }
48
49 void Light::set_spot_exponent(float e)
50 {
51         spot_exp = e;
52 }
53
54 void Light::set_spot_cutoff(float c)
55 {
56         spot_cutoff = c;
57 }
58
59 void Light::set_attenuation(float c, float l, float q)
60 {
61         attenuation[0] = c;
62         attenuation[1] = l;
63         attenuation[2] = q;
64 }
65
66 void Light::bind() const
67 {
68         if(current_lights[current_unit]!=this)
69         {
70                 GLenum l = GL_LIGHT0+current_unit;
71                 enable(l);
72                 glLightfv(l, GL_AMBIENT, &ambient.r);
73                 glLightfv(l, GL_DIFFUSE, &diffuse.r);
74                 glLightfv(l, GL_SPECULAR, &specular.r);
75                 glLightfv(l, GL_POSITION, &position.x);
76                 glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
77                 glLightf(l, GL_SPOT_EXPONENT, spot_exp);
78                 glLightf(l, GL_SPOT_CUTOFF, spot_cutoff);
79                 glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
80                 glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
81                 glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
82                 current_lights[current_unit] = this;
83         }
84 }
85
86 void Light::bind_to(unsigned i) const
87 {
88         activate(i);
89         bind();
90 }
91
92 void Light::activate(unsigned i)
93 {
94         static unsigned max_lights = get_i(GL_MAX_LIGHTS);
95
96         if(i>=max_lights)
97                 throw InvalidParameterValue("Light unit index out of range");
98
99         if(i>=current_lights.size())
100                 current_lights.resize(i+1);
101
102         current_unit = i;
103 }
104
105 void Light::unbind()
106 {
107         if(current_lights[current_unit])
108         {
109                 disable(GL_LIGHT0+current_unit);
110                 current_lights[current_unit] = 0;
111         }
112 }
113
114 void Light::unbind_from(unsigned i)
115 {
116         activate(i);
117         unbind();
118 }
119
120 unsigned Light::current_unit = 0;
121 vector<const Light *> Light::current_lights(1);
122
123 } // namespace GL
124 } // namespace Msp