]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/light.cpp
Use persistent uniform blocks for Camera, Lighting and Clipping
[libs/gl.git] / source / materials / light.cpp
index e910411f276e27ebd305f507513177787caebb25..3d3cba0a87127116068aa85900e24656e1f83841 100644 (file)
@@ -11,12 +11,13 @@ namespace Msp {
 namespace GL {
 
 Light::Light():
-       diffuse(1),
-       specular(1),
+       color(1),
+       transmittance(1),
        position(0, 0, 1, 0),
        spot_dir(0, 0, -1),
        spot_exp(0),
-       spot_cutoff(Geometry::Angle<float>::straight())
+       spot_cutoff(Geometry::Angle<float>::straight()),
+       generation(0)
 {
        attenuation[0] = 1;
        attenuation[1] = 0;
@@ -40,14 +41,16 @@ void Light::update_matrix()
        matrix = Matrix::from_columns(columns);
 }
 
-void Light::set_diffuse(const Color &c)
+void Light::set_color(const Color &c)
 {
-       diffuse = c;
+       color = c;
+       ++generation;
 }
 
-void Light::set_specular(const Color &c)
+void Light::set_transmittance(const Color &t)
 {
-       specular = c;
+       transmittance = t;
+       ++generation;
 }
 
 void Light::set_matrix(const Matrix &m)
@@ -57,6 +60,7 @@ void Light::set_matrix(const Matrix &m)
        spot_dir = normalize(-matrix.column(2).slice<3>(0));
        direction = (position.w ? spot_dir : normalize(-position.slice<3>(0)));
        update_matrix();
+       ++generation;
 }
 
 void Light::set_position(const Vector4 &p)
@@ -65,6 +69,7 @@ void Light::set_position(const Vector4 &p)
        if(!position.w)
                direction = normalize(-position.slice<3>(0));
        update_matrix();
+       ++generation;
 }
 
 void Light::set_spot_direction(const Vector3 &d)
@@ -73,6 +78,7 @@ void Light::set_spot_direction(const Vector3 &d)
        if(position.w)
                direction = spot_dir;
        update_matrix();
+       ++generation;
 }
 
 void Light::set_spot_exponent(float e)
@@ -81,6 +87,7 @@ void Light::set_spot_exponent(float e)
                throw invalid_argument("Light::set_spot_exponent");
 
        spot_exp = e;
+       ++generation;
 }
 
 void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
@@ -89,11 +96,13 @@ void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
                throw invalid_argument("Light::set_spot_cutoff");
 
        spot_cutoff = c;
+       ++generation;
 }
 
 void Light::disable_spot_cutoff()
 {
        set_spot_cutoff(Geometry::Angle<float>::straight());
+       ++generation;
 }
 
 void Light::set_attenuation(float c, float l, float q)
@@ -101,14 +110,15 @@ void Light::set_attenuation(float c, float l, float q)
        attenuation[0] = c;
        attenuation[1] = l;
        attenuation[2] = q;
+       ++generation;
 }
 
-void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const
+void Light::update_shader_data(ProgramData &shdata, unsigned i) const
 {
        string base = format("light_sources[%d]", i);
-       shdata.uniform(base+".position", view_matrix*position);
-       shdata.uniform(base+".diffuse", diffuse);
-       shdata.uniform(base+".specular", specular);
+       shdata.uniform(base+".position", position);
+       shdata.uniform(base+".color", color.r*transmittance.r, color.g*transmittance.g, color.b*transmittance.b);
+       shdata.uniform(base+".enabled", 1);
 }
 
 
@@ -116,12 +126,15 @@ Light::Loader::Loader(Light &l):
        DataFile::ObjectLoader<Light>(l)
 {
        add("attenuation", &Loader::attenuation);
-       add("diffuse", &Loader::diffuse);
+       add("color", &Loader::color);
        add("position", &Loader::position);
-       add("specular", &Loader::specular);
        add("spot_direction", &Loader::spot_direction);
        add("spot_exponent", &Loader::spot_exponent);
        add("spot_cutoff", &Loader::spot_cutoff);
+
+       // Deprecated
+       add("diffuse", &Loader::color);
+       add("specular");
 }
 
 void Light::Loader::attenuation(float c, float l, float q)
@@ -129,9 +142,9 @@ void Light::Loader::attenuation(float c, float l, float q)
        obj.set_attenuation(c, l, q);
 }
 
-void Light::Loader::diffuse(float r, float g, float b)
+void Light::Loader::color(float r, float g, float b)
 {
-       obj.set_diffuse(Color(r, g, b));
+       obj.set_color(Color(r, g, b));
 }
 
 void Light::Loader::position(float x, float y, float z, float w)
@@ -139,11 +152,6 @@ void Light::Loader::position(float x, float y, float z, float w)
        obj.set_position(Vector4(x, y, z, w));
 }
 
-void Light::Loader::specular(float r, float g, float b)
-{
-       obj.set_specular(Color(r, g, b));
-}
-
 void Light::Loader::spot_direction(float x, float y, float z)
 {
        obj.set_spot_direction(Vector3(x, y, z));