2 #include <msp/strings/format.h>
7 #include "programdata.h"
27 void Light::update_parameter(int mask, int index) const
31 LightUnit *unit = LightUnit::find_unit(this);
35 index = unit->get_index();
38 GLenum l = GL_LIGHT0+index;
40 glLightfv(l, GL_DIFFUSE, &diffuse.r);
42 glLightfv(l, GL_SPECULAR, &specular.r);
44 glLightfv(l, GL_POSITION, &position.x);
46 glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
48 glLightf(l, GL_SPOT_EXPONENT, spot_exp);
50 glLightf(l, GL_SPOT_CUTOFF, spot_cutoff);
53 glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
54 glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
55 glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
59 void Light::set_diffuse(const Color &c)
62 update_parameter(DIFFUSE);
65 void Light::set_specular(const Color &c)
68 update_parameter(SPECULAR);
71 void Light::set_position(const Vector4 &p)
74 update_parameter(POSITION);
77 void Light::set_spot_direction(const Vector3 &d)
80 update_parameter(SPOT_DIR);
83 void Light::set_spot_exponent(float e)
86 update_parameter(SPOT_EXP);
89 void Light::set_spot_cutoff(float c)
92 update_parameter(SPOT_CUTOFF);
95 void Light::set_attenuation(float c, float l, float q)
100 update_parameter(ATTENUATION);
103 void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
105 string base = format("light_sources[%d]", i);
106 shdata.uniform(base+".position", view_matrix*position);
107 shdata.uniform(base+".diffuse", diffuse);
108 shdata.uniform(base+".specular", specular);
111 void Light::bind_to(unsigned i) const
113 LightUnit &unit = LightUnit::get_unit(i);
114 if(unit.set_light(this))
116 enable(GL_LIGHT0+unit.get_index());
117 update_parameter(-1, unit.get_index());
121 const Light *Light::current(unsigned i)
123 return LightUnit::get_unit(i).get_light();
126 void Light::unbind_from(unsigned i)
128 LightUnit &unit = LightUnit::get_unit(i);
129 if(unit.set_light(0))
130 disable(GL_LIGHT0+unit.get_index());