]> git.tdb.fi Git - libs/gl.git/blob - source/light.cpp
Rewrite light binding to match textures
[libs/gl.git] / source / light.cpp
1 #include <stdexcept>
2 #include "light.h"
3 #include "lightunit.h"
4 #include "misc.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Light::Light():
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_diffuse(const Color &c)
25 {
26         diffuse = c;
27 }
28
29 void Light::set_specular(const Color &c)
30 {
31         specular = c;
32 }
33
34 void Light::set_position(const Vector4 &p)
35 {
36         position = p;
37 }
38
39 void Light::set_spot_direction(const Vector3 &d)
40 {
41         spot_dir = d;
42 }
43
44 void Light::set_spot_exponent(float e)
45 {
46         spot_exp = e;
47 }
48
49 void Light::set_spot_cutoff(float c)
50 {
51         spot_cutoff = c;
52 }
53
54 void Light::set_attenuation(float c, float l, float q)
55 {
56         attenuation[0] = c;
57         attenuation[1] = l;
58         attenuation[2] = q;
59 }
60
61 void Light::bind_to(unsigned i) const
62 {
63         LightUnit &unit = LightUnit::get_unit(i);
64         if(unit.set_light(this))
65         {
66                 GLenum l = GL_LIGHT0+unit.get_index();
67                 enable(l);
68                 glLightfv(l, GL_DIFFUSE, &diffuse.r);
69                 glLightfv(l, GL_SPECULAR, &specular.r);
70                 glLightfv(l, GL_POSITION, &position.x);
71                 glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
72                 glLightf(l, GL_SPOT_EXPONENT, spot_exp);
73                 glLightf(l, GL_SPOT_CUTOFF, spot_cutoff);
74                 glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
75                 glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
76                 glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
77         }
78 }
79
80 const Light *Light::current(unsigned i)
81 {
82         return LightUnit::get_unit(i).get_light();
83 }
84
85 void Light::unbind_from(unsigned i)
86 {
87         LightUnit &unit = LightUnit::get_unit(i);
88         if(unit.set_light(0))
89                 disable(GL_LIGHT0+unit.get_index());
90 }
91
92 } // namespace GL
93 } // namespace Msp