#include <stdexcept>
#include "light.h"
+#include "lightunit.h"
#include "misc.h"
using namespace std;
namespace Msp {
namespace GL {
-unsigned Light::current_unit = 0;
-vector<const Light *> Light::current_lights(1);
-
Light::Light():
diffuse(1),
specular(1),
attenuation[2] = q;
}
-void Light::bind() const
+void Light::bind_to(unsigned i) const
{
- if(current_lights[current_unit]!=this)
+ LightUnit &unit = LightUnit::get_unit(i);
+ if(unit.set_light(this))
{
- GLenum l = GL_LIGHT0+current_unit;
+ GLenum l = GL_LIGHT0+unit.get_index();
enable(l);
glLightfv(l, GL_DIFFUSE, &diffuse.r);
glLightfv(l, GL_SPECULAR, &specular.r);
glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
- current_lights[current_unit] = this;
}
}
-void Light::bind_to(unsigned i) const
-{
- activate(i);
- bind();
-}
-
-void Light::activate(unsigned i)
-{
- static unsigned max_lights = get_i(GL_MAX_LIGHTS);
-
- if(i>=max_lights)
- throw out_of_range("Light::activate");
-
- if(i>=current_lights.size())
- current_lights.resize(i+1);
-
- current_unit = i;
-}
-
-void Light::unbind()
+const Light *Light::current(unsigned i)
{
- if(current_lights[current_unit])
- {
- disable(GL_LIGHT0+current_unit);
- current_lights[current_unit] = 0;
- }
+ return LightUnit::get_unit(i).get_light();
}
void Light::unbind_from(unsigned i)
{
- activate(i);
- unbind();
+ LightUnit &unit = LightUnit::get_unit(i);
+ if(unit.set_light(0))
+ disable(GL_LIGHT0+unit.get_index());
}
} // namespace GL
float spot_cutoff;
float attenuation[3];
- static unsigned current_unit;
- static std::vector<const Light *> current_lights;
-
public:
Light();
void set_attenuation(float, float, float);
const float *get_attenuation() const { return attenuation; }
- void bind() const;
+ void bind() const { return bind_to(0); }
void bind_to(unsigned) const;
- static void activate(unsigned);
- static void unbind();
+ static const Light *current(unsigned = 0);
+ static void unbind() { return unbind_from(0); }
static void unbind_from(unsigned);
};
--- /dev/null
+#include <stdexcept>
+#include "gl.h"
+#include "misc.h"
+#include "lightunit.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+vector<LightUnit> LightUnit::units;
+LightUnit *LightUnit::cur_unit = 0;
+
+LightUnit::LightUnit():
+ light(0)
+{ }
+
+bool LightUnit::set_light(const Light *l)
+{
+ bool result = (l!=light);
+ light = l;
+ return result;
+}
+
+unsigned LightUnit::get_n_units()
+{
+ static int count = get_i(GL_MAX_LIGHTS);
+ return count;
+}
+
+LightUnit &LightUnit::get_unit(unsigned n)
+{
+ if(n>=get_n_units())
+ throw out_of_range("LightUnit::get_unit");
+
+ if(units.size()<=n)
+ {
+ unsigned i = units.size();
+ units.resize(n+1, LightUnit());
+ for(; i<units.size(); ++i)
+ units[i].index = i;
+ }
+
+ return units[n];
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+#ifndef MSP_GL_LIGHTUNIT_H_
+#define MSP_GL_LIGHTUNIT_H_
+
+#include <vector>
+
+namespace Msp {
+namespace GL {
+
+class Light;
+
+class LightUnit
+{
+private:
+ unsigned index;
+ const Light *light;
+
+ static std::vector<LightUnit> units;
+ static LightUnit *cur_unit;
+
+ LightUnit();
+
+public:
+ unsigned get_index() const { return index; }
+ bool set_light(const Light *);
+ const Light *get_light() const { return light; }
+
+ static unsigned get_n_units();
+ static LightUnit &get_unit(unsigned i);
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif