2 #include <msp/strings/format.h>
7 #include "programdata.h"
29 while(LightUnit *unit = LightUnit::find_unit(this))
30 unbind_from(unit->get_index());
33 void Light::update_parameter(int mask, int index) const
37 LightUnit *unit = LightUnit::find_unit(this);
41 index = unit->get_index();
44 GLenum l = GL_LIGHT0+index;
46 glLightfv(l, GL_DIFFUSE, &diffuse.r);
48 glLightfv(l, GL_SPECULAR, &specular.r);
50 glLightfv(l, GL_POSITION, &position.x);
52 glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
54 glLightf(l, GL_SPOT_EXPONENT, spot_exp);
56 glLightf(l, GL_SPOT_CUTOFF, spot_cutoff);
59 glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
60 glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
61 glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
65 void Light::set_diffuse(const Color &c)
68 update_parameter(DIFFUSE);
71 void Light::set_specular(const Color &c)
74 update_parameter(SPECULAR);
77 void Light::set_position(const Vector4 &p)
80 update_parameter(POSITION);
83 void Light::set_spot_direction(const Vector3 &d)
86 update_parameter(SPOT_DIR);
89 void Light::set_spot_exponent(float e)
92 update_parameter(SPOT_EXP);
95 void Light::set_spot_cutoff(float c)
98 update_parameter(SPOT_CUTOFF);
101 void Light::set_attenuation(float c, float l, float q)
106 update_parameter(ATTENUATION);
109 void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
111 string base = format("light_sources[%d]", i);
112 shdata.uniform(base+".position", view_matrix*position);
113 shdata.uniform(base+".diffuse", diffuse);
114 shdata.uniform(base+".specular", specular);
117 void Light::bind_to(unsigned i) const
119 LightUnit &unit = LightUnit::get_unit(i);
120 if(unit.set_light(this))
122 enable(GL_LIGHT0+unit.get_index());
123 update_parameter(-1, unit.get_index());
127 const Light *Light::current(unsigned i)
129 return LightUnit::get_unit(i).get_light();
132 void Light::unbind_from(unsigned i)
134 LightUnit &unit = LightUnit::get_unit(i);
135 if(unit.set_light(0))
136 disable(GL_LIGHT0+unit.get_index());