-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <stdexcept>
+#include <msp/gl/extensions/msp_legacy_features.h>
+#include <msp/strings/format.h>
#include "light.h"
+#include "lightunit.h"
+#include "matrix.h"
+#include "misc.h"
+#include "programdata.h"
+
+using namespace std;
namespace Msp {
namespace GL {
Light::Light():
- ambient(0),
diffuse(1),
specular(1),
- x(0), y(0), z(1), w(0),
- sdx(0), sdy(0), sdz(-1),
+ position(0, 0, 1, 0),
+ spot_dir(0, 0, -1),
spot_exp(0),
- spot_cutoff(180)
-{ }
+ spot_cutoff(Geometry::Angle<float>::straight())
+{
+ attenuation[0] = 1;
+ attenuation[1] = 0;
+ attenuation[2] = 0;
+}
-void Light::set_ambient(const Color &c)
+Light::~Light()
{
- ambient=c;
+ while(LightUnit *unit = LightUnit::find_unit(this))
+ unbind_from(unit->get_index());
+}
+
+void Light::update_parameter(int mask, int index) const
+{
+ if(index<0)
+ {
+ LightUnit *unit = LightUnit::find_unit(this);
+ if(!unit)
+ return;
+
+ index = unit->get_index();
+ }
+
+ GLenum l = GL_LIGHT0+index;
+ if(mask&DIFFUSE)
+ glLightfv(l, GL_DIFFUSE, &diffuse.r);
+ if(mask&SPECULAR)
+ glLightfv(l, GL_SPECULAR, &specular.r);
+ if(mask&POSITION)
+ glLightfv(l, GL_POSITION, &position.x);
+ if(mask&SPOT_DIR)
+ glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
+ if(mask&SPOT_EXP)
+ glLightf(l, GL_SPOT_EXPONENT, spot_exp);
+ if(mask&SPOT_CUTOFF)
+ glLightf(l, GL_SPOT_CUTOFF, spot_cutoff.degrees());
+ if(mask&ATTENUATION)
+ {
+ glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
+ glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
+ glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
+ }
+}
+
+void Light::update_matrix()
+{
+ Vector3 up_dir;
+ if(20*abs(direction.z)>abs(direction.x)+abs(direction.y))
+ up_dir.y = 1;
+ else
+ up_dir.z = 1;
+ Vector3 right_dir = normalize(cross(direction, up_dir));
+
+ Vector4 columns[4];
+ columns[0] = compose(right_dir, 0.0f);
+ columns[1] = compose(cross(right_dir, direction), 0.0f);
+ columns[2] = compose(-direction, 0.0f);
+ columns[3] = position;
+ matrix = Matrix::from_columns(columns);
}
void Light::set_diffuse(const Color &c)
{
- diffuse=c;
+ diffuse = c;
+ update_parameter(DIFFUSE);
}
void Light::set_specular(const Color &c)
{
- specular=c;
+ specular = c;
+ update_parameter(SPECULAR);
+}
+
+void Light::set_matrix(const Matrix &m)
+{
+ Placeable::set_matrix(m);
+ position = matrix.column(3);
+ spot_dir = normalize(-matrix.column(2).slice<3>(0));
+ direction = (position.w ? spot_dir : normalize(-position.slice<3>(0)));
+ update_parameter(POSITION|SPOT_DIR);
+ update_matrix();
+}
+
+void Light::set_position(const Vector4 &p)
+{
+ position = p;
+ update_parameter(POSITION);
+ if(!position.w)
+ direction = normalize(-position.slice<3>(0));
+ update_matrix();
+}
+
+void Light::set_spot_direction(const Vector3 &d)
+{
+ spot_dir = normalize(d);
+ if(position.w)
+ direction = spot_dir;
+ update_parameter(SPOT_DIR);
+ update_matrix();
+}
+
+void Light::set_spot_exponent(float e)
+{
+ if(e<0)
+ throw invalid_argument("Light::set_spot_exponent");
+
+ spot_exp = e;
+ update_parameter(SPOT_EXP);
+}
+
+void Light::set_spot_cutoff(float c)
+{
+ set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));
+}
+
+void Light::set_spot_cutoff(const Geometry::Angle<float> &c)
+{
+ if(c<Geometry::Angle<float>::zero() || (c>Geometry::Angle<float>::right() && c!=Geometry::Angle<float>::straight()))
+ throw invalid_argument("Light::set_spot_cutoff");
+
+ spot_cutoff = c;
+ update_parameter(SPOT_CUTOFF);
+}
+
+void Light::disable_spot_cutoff()
+{
+ set_spot_cutoff(Geometry::Angle<float>::straight());
+}
+
+void Light::set_attenuation(float c, float l, float q)
+{
+ attenuation[0] = c;
+ attenuation[1] = l;
+ attenuation[2] = q;
+ update_parameter(ATTENUATION);
+}
+
+void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, 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);
+}
+
+void Light::bind_to(unsigned i) const
+{
+ static Require _req(MSP_legacy_features);
+
+ LightUnit &unit = LightUnit::get_unit(i);
+ if(unit.set_light(this))
+ {
+ enable(GL_LIGHT0+unit.get_index());
+ update_parameter(-1, unit.get_index());
+ }
+}
+
+const Light *Light::current(unsigned i)
+{
+ return LightUnit::get_unit(i).get_light();
+}
+
+void Light::unbind_from(unsigned i)
+{
+ LightUnit &unit = LightUnit::get_unit(i);
+ if(unit.set_light(0))
+ disable(GL_LIGHT0+unit.get_index());
+}
+
+
+Light::Loader::Loader(Light &l):
+ DataFile::ObjectLoader<Light>(l)
+{
+ add("attenuation", &Loader::attenuation);
+ add("diffuse", &Loader::diffuse);
+ 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);
+}
+
+void Light::Loader::attenuation(float c, float l, float q)
+{
+ obj.set_attenuation(c, l, q);
}
-void Light::set_position(float x_, float y_, float z_, float w_)
+void Light::Loader::diffuse(float r, float g, float b)
{
- x=x_;
- y=y_;
- z=z_;
- w=w_;
+ obj.set_diffuse(Color(r, g, b));
}
-void Light::apply()
+void Light::Loader::position(float x, float y, float z, float w)
{
- apply_to(current);
+ obj.set_position(Vector4(x, y, z, w));
}
-void Light::apply_to(unsigned l)
+void Light::Loader::specular(float r, float g, float b)
{
- l+=GL_LIGHT0;
- glLightfv(l, GL_AMBIENT, &ambient.r);
- glLightfv(l, GL_DIFFUSE, &diffuse.r);
- glLightfv(l, GL_SPECULAR, &specular.r);
- glLightfv(l, GL_POSITION, &x);
+ obj.set_specular(Color(r, g, b));
}
-unsigned Light::current=0;
+void Light::Loader::spot_direction(float x, float y, float z)
+{
+ obj.set_spot_direction(Vector3(x, y, z));
+}
+
+void Light::Loader::spot_exponent(float e)
+{
+ obj.set_spot_exponent(e);
+}
+
+void Light::Loader::spot_cutoff(float c)
+{
+ obj.set_spot_cutoff(Geometry::Angle<float>::from_degrees(c));
+}
} // namespace GL
} // namespace Msp