From 48b1ab4fff00c49cc15d70a354eedb3d7a2f3e87 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 28 Nov 2013 02:02:49 +0200 Subject: [PATCH] Rewrite light binding to match textures Both have multiple multiple parallel bind points, so it makes sense they behave in the same way. --- source/light.cpp | 44 +++++++++------------------------------- source/light.h | 9 +++------ source/lightunit.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ source/lightunit.h | 34 +++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 40 deletions(-) create mode 100644 source/lightunit.cpp create mode 100644 source/lightunit.h diff --git a/source/light.cpp b/source/light.cpp index 21925364..f9b45c87 100644 --- a/source/light.cpp +++ b/source/light.cpp @@ -1,5 +1,6 @@ #include #include "light.h" +#include "lightunit.h" #include "misc.h" using namespace std; @@ -7,9 +8,6 @@ using namespace std; namespace Msp { namespace GL { -unsigned Light::current_unit = 0; -vector Light::current_lights(1); - Light::Light(): diffuse(1), specular(1), @@ -60,11 +58,12 @@ void Light::set_attenuation(float c, float l, float q) 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); @@ -75,42 +74,19 @@ void Light::bind() const 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 diff --git a/source/light.h b/source/light.h index 658e8287..cf9ab8a1 100644 --- a/source/light.h +++ b/source/light.h @@ -19,9 +19,6 @@ private: float spot_cutoff; float attenuation[3]; - static unsigned current_unit; - static std::vector current_lights; - public: Light(); @@ -41,11 +38,11 @@ public: 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); }; diff --git a/source/lightunit.cpp b/source/lightunit.cpp new file mode 100644 index 00000000..58d42e6d --- /dev/null +++ b/source/lightunit.cpp @@ -0,0 +1,48 @@ +#include +#include "gl.h" +#include "misc.h" +#include "lightunit.h" + +using namespace std; + +namespace Msp { +namespace GL { + +vector 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 + +namespace Msp { +namespace GL { + +class Light; + +class LightUnit +{ +private: + unsigned index; + const Light *light; + + static std::vector 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 -- 2.45.2