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