]> git.tdb.fi Git - libs/gl.git/blob - source/light.cpp
Rearrange things in ZSortedScene a bit
[libs/gl.git] / source / light.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/msp_legacy_features.h>
3 #include <msp/strings/format.h>
4 #include "light.h"
5 #include "lightunit.h"
6 #include "matrix.h"
7 #include "misc.h"
8 #include "programdata.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Light::Light():
16         diffuse(1),
17         specular(1),
18         position(0, 0, 1, 0),
19         spot_dir(0, 0, -1),
20         spot_exp(0),
21         spot_cutoff(180)
22 {
23         attenuation[0] = 1;
24         attenuation[1] = 0;
25         attenuation[2] = 0;
26 }
27
28 Light::~Light()
29 {
30         while(LightUnit *unit = LightUnit::find_unit(this))
31                 unbind_from(unit->get_index());
32 }
33
34 void Light::update_parameter(int mask, int index) const
35 {
36         if(index<0)
37         {
38                 LightUnit *unit = LightUnit::find_unit(this);
39                 if(!unit)
40                         return;
41
42                 index = unit->get_index();
43         }
44
45         GLenum l = GL_LIGHT0+index;
46         if(mask&DIFFUSE)
47                 glLightfv(l, GL_DIFFUSE, &diffuse.r);
48         if(mask&SPECULAR)
49                 glLightfv(l, GL_SPECULAR, &specular.r);
50         if(mask&POSITION)
51                 glLightfv(l, GL_POSITION, &position.x);
52         if(mask&SPOT_DIR)
53                 glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
54         if(mask&SPOT_EXP)
55                 glLightf(l, GL_SPOT_EXPONENT, spot_exp);
56         if(mask&SPOT_CUTOFF)
57                 glLightf(l, GL_SPOT_CUTOFF, spot_cutoff);
58         if(mask&ATTENUATION)
59         {
60                 glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
61                 glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
62                 glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
63         }
64 }
65
66 void Light::set_diffuse(const Color &c)
67 {
68         diffuse = c;
69         update_parameter(DIFFUSE);
70 }
71
72 void Light::set_specular(const Color &c)
73 {
74         specular = c;
75         update_parameter(SPECULAR);
76 }
77
78 void Light::set_position(const Vector4 &p)
79 {
80         position = p;
81         update_parameter(POSITION);
82 }
83
84 void Light::set_spot_direction(const Vector3 &d)
85 {
86         spot_dir = d;
87         update_parameter(SPOT_DIR);
88 }
89
90 void Light::set_spot_exponent(float e)
91 {
92         spot_exp = e;
93         update_parameter(SPOT_EXP);
94 }
95
96 void Light::set_spot_cutoff(float c)
97 {
98         spot_cutoff = c;
99         update_parameter(SPOT_CUTOFF);
100 }
101
102 void Light::set_attenuation(float c, float l, float q)
103 {
104         attenuation[0] = c;
105         attenuation[1] = l;
106         attenuation[2] = q;
107         update_parameter(ATTENUATION);
108 }
109
110 void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
111 {
112         string base = format("light_sources[%d]", i);
113         shdata.uniform(base+".position", view_matrix*position);
114         shdata.uniform(base+".diffuse", diffuse);
115         shdata.uniform(base+".specular", specular);
116 }
117
118 void Light::bind_to(unsigned i) const
119 {
120         static Require _req(MSP_legacy_features);
121
122         LightUnit &unit = LightUnit::get_unit(i);
123         if(unit.set_light(this))
124         {
125                 enable(GL_LIGHT0+unit.get_index());
126                 update_parameter(-1, unit.get_index());
127         }
128 }
129
130 const Light *Light::current(unsigned i)
131 {
132         return LightUnit::get_unit(i).get_light();
133 }
134
135 void Light::unbind_from(unsigned i)
136 {
137         LightUnit &unit = LightUnit::get_unit(i);
138         if(unit.set_light(0))
139                 disable(GL_LIGHT0+unit.get_index());
140 }
141
142 } // namespace GL
143 } // namespace Msp