]> git.tdb.fi Git - libs/gl.git/blobdiff - source/lighting.cpp
Refresh lighting and culling uniforms if the camera changes in pop_state
[libs/gl.git] / source / lighting.cpp
index 0d6e7dc4ab6e5da6a1521677a9a8689398ed6ca5..ce522ac06a11e77151a5c7fbe0967347674f2aa3 100644 (file)
@@ -1,19 +1,24 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2008  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <stdexcept>
+#include <cmath>
+#include <msp/gl/extensions/msp_legacy_features.h>
+#include "error.h"
 #include "light.h"
 #include "lighting.h"
+#include "lightunit.h"
+#include "matrix.h"
 #include "misc.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
 Lighting::Lighting():
-       ambient(0.2)
+       ambient(0.2),
+       sky_direction(0, 0, 1),
+       horizon_angle(Geometry::Angle<float>::zero()),
+       fog_color(0.0f, 0.0f, 0.0f, 0.0f),
+       fog_density(0.0f)
 { }
 
 void Lighting::set_ambient(const Color &a)
@@ -21,12 +26,47 @@ void Lighting::set_ambient(const Color &a)
        ambient = a;
 }
 
+void Lighting::set_sky_color(const Color &s)
+{
+       sky_color = s;
+}
+
+void Lighting::set_sky_direction(const Vector3 &d)
+{
+       sky_direction = d;
+}
+
+void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
+{
+       horizon_angle = a;
+}
+
+void Lighting::set_fog_color(const Color &c)
+{
+       fog_color = c;
+}
+
+void Lighting::set_fog_density(float d)
+{
+       if(d<0)
+               throw invalid_argument("Lighting::set_fog_density");
+
+       fog_density = d;
+}
+
+void Lighting::set_fog_half_distance(float d)
+{
+       set_fog_density(-log(pow(0.5, 1.0/d)));
+}
+
 void Lighting::attach(unsigned i, const Light &l)
 {
        if(i>=lights.size())
                lights.resize(i+1);
 
        lights[i] = &l;
+       if(current()==this)
+               l.bind_to(i);
 }
 
 void Lighting::detach(unsigned i)
@@ -35,37 +75,78 @@ void Lighting::detach(unsigned i)
                return;
 
        lights[i] = 0;
+       if(current()==this)
+               Light::unbind_from(i);
+}
+
+const Light *Lighting::get_attached_light(unsigned i) const
+{
+       return i<lights.size() ? lights[i] : 0;
+}
+
+void Lighting::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
+{
+       shdata.uniform("ambient_color", ambient);
+       shdata.uniform("sky_color", sky_color);
+       shdata.uniform("eye_sky_dir", view_matrix.block<3, 3>(0, 0)*sky_direction);
+       shdata.uniform("horizon_limit", horizon_angle.radians());
+       shdata.uniform("fog_color", fog_color);
+       shdata.uniform("fog_density", fog_density);
+
+       for(unsigned i=0; i<lights.size(); ++i)
+               if(lights[i])
+                       lights[i]->update_shader_data(shdata, view_matrix, i);
 }
 
 void Lighting::bind() const
 {
-       if(current!=this)
+       static Require _req(MSP_legacy_features);
+       if(lights.size()>LightUnit::get_n_units())
+               throw invalid_operation("Lighting::bind");
+
+       const Lighting *old = current();
+       if(!set_current(this))
+               return;
+
+       enable(GL_LIGHTING);
+       glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
+       for(unsigned i=0; i<lights.size(); ++i)
        {
-               enable(LIGHTING);
-               glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &ambient.r);
-               for(unsigned i=0; i<lights.size(); ++i)
-                       if(lights[i])
-                               lights[i]->bind_to(i);
-               current = this;
+               if(lights[i])
+                       lights[i]->bind_to(i);
+               else
+                       Light::unbind_from(i);
        }
-}
 
-void Lighting::unbind()
-{
-       if(current)
+       if(old)
+       {
+               for(unsigned i=lights.size(); i<old->lights.size(); ++i)
+                       Light::unbind_from(i);
+       }
+
+       if(fog_density)
        {
-               for(unsigned i=0; i<current->lights.size(); ++i)
-                       if(current->lights[i])
-                       {
-                               Light::activate(i);
-                               Light::unbind();
-                       }
-               disable(LIGHTING);
-               current = 0;
+               enable(GL_FOG);
+               glFogi(GL_FOG_MODE, GL_EXP);
+               glFogf(GL_FOG_DENSITY, fog_density);
+               glFogfv(GL_FOG_COLOR, &fog_color.r);
        }
 }
 
-const Lighting *Lighting::current = 0;
+void Lighting::unbind()
+{
+       const Lighting *old = current();
+       if(!set_current(0))
+               return;
+
+       for(unsigned i=0; i<old->lights.size(); ++i)
+               if(old->lights[i])
+                       Light::unbind_from(i);
+
+       disable(GL_LIGHTING);
+       if(old->fog_density)
+               disable(GL_FOG);
+}
 
 } // namespace GL
 } // namespace Msp