]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/lighting.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / materials / lighting.cpp
index 699aabc02c7daa8679d60b2ab6fd8eaf8d99723b..d8e5400626ae43eb26221ebd92bd748bb8043972 100644 (file)
@@ -1,25 +1,31 @@
 #include <stdexcept>
 #include <cmath>
 #include <msp/core/algorithm.h>
+#include <msp/datafile/collection.h>
 #include <msp/fs/utils.h>
-#include "error.h"
+#include <msp/strings/format.h>
 #include "light.h"
 #include "lighting.h"
-#include "matrix.h"
-#include "misc.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
-Lighting::Lighting():
-       zenith_direction(0, 0, 1),
-       horizon_angle(Geometry::Angle<float>::zero())
+Lighting::Lighting()
 {
        set_ambient(0.2f);
        set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
        set_fog_density(0.0f);
+
+       for(unsigned i=0; i<6; ++i)
+       {
+               string base = format("light_sources[%d]", i);
+               shdata.uniform(base+".position", Vector4(0, 0, 1, 0));
+               shdata.uniform(base+".color", 0.0f, 0.0f, 0.0f);
+               shdata.uniform(base+".type", 0);
+               shdata.uniform(base+".attenuation", 1.0f, 0.0f, 0.0f);
+       }
 }
 
 void Lighting::set_ambient(const Color &a)
@@ -28,24 +34,6 @@ void Lighting::set_ambient(const Color &a)
        shdata.uniform("ambient_color", ambient);
 }
 
-void Lighting::set_sky_color(const Color &s)
-{
-       sky_color = s;
-       shdata.uniform("sky_color", sky_color);
-}
-
-void Lighting::set_zenith_direction(const Vector3 &d)
-{
-       zenith_direction = d;
-       shdata.uniform("world_zenith_dir", zenith_direction);
-}
-
-void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
-{
-       horizon_angle = a;
-       shdata.uniform("horizon_limit", horizon_angle.radians());
-}
-
 void Lighting::set_fog_color(const Color &c)
 {
        fog_color = c;
@@ -74,36 +62,18 @@ void Lighting::attach(const Light &l)
 
 void Lighting::detach(const Light &l)
 {
-       vector<AttachedLight>::iterator i = find_member(lights, &l, &AttachedLight::light);
+       auto i = find_member(lights, &l, &AttachedLight::light);
        if(i!=lights.end())
+       {
                lights.erase(i);
+               shdata.uniform(format("light_sources[%d].enabled", lights.size()), 0);
+       }
 }
 
-void Lighting::detach(unsigned i)
-{
-       if(i>=lights.size())
-               return;
-
-       detach(*lights[i].light);
-}
-
-const Light *Lighting::get_attached_light(unsigned i) const
+int Lighting::find_light_index(const Light &l) const
 {
-       return i<lights.size() ? lights[i].light : 0;
-}
-
-void Lighting::update_shader_data(ProgramData &sd, const Matrix &) const
-{
-       sd.uniform("ambient_color", ambient);
-       sd.uniform("sky_color", sky_color);
-       sd.uniform("world_zenith_dir", zenith_direction);
-       sd.uniform("horizon_limit", horizon_angle.radians());
-       sd.uniform("fog_color", fog_color);
-       sd.uniform("fog_density", fog_density);
-
-       for(unsigned i=0; i<lights.size(); ++i)
-               if(lights[i].light)
-                       lights[i].light->update_shader_data(sd, i);
+       auto i = find_member(lights, &l, &AttachedLight::light);
+       return (i!=lights.end() ? i-lights.begin() : -1);
 }
 
 const ProgramData &Lighting::get_shader_data() const
@@ -130,12 +100,6 @@ void Lighting::set_debug_name(const string &name)
 
 DataFile::Loader::ActionMap Lighting::Loader::shared_actions;
 
-Lighting::Loader::Loader(Lighting &l):
-       CollectionObjectLoader<Lighting>(l, 0)
-{
-       set_actions(shared_actions);
-}
-
 Lighting::Loader::Loader(Lighting &l, Collection &c):
        CollectionObjectLoader<Lighting>(l, &c)
 {
@@ -150,12 +114,6 @@ void Lighting::Loader::init_actions()
        add("fog_half_distance", &Loader::fog_half_distance);
        add("light", &Loader::light);
        add("light", &Loader::light_inline);
-
-       // Deprecated
-       add("horizon_angle", &Loader::horizon_angle);
-       add("light", &Loader::light_inline_index);
-       add("sky_color", &Loader::sky_color);
-       add("zenith_direction", &Loader::zenith_direction);
 }
 
 void Lighting::Loader::ambient(float r, float g, float b)
@@ -178,14 +136,6 @@ void Lighting::Loader::fog_half_distance(float d)
        obj.set_fog_half_distance(d);
 }
 
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-void Lighting::Loader::horizon_angle(float a)
-{
-       obj.set_horizon_angle(Geometry::Angle<float>::from_degrees(a));
-}
-#pragma GCC diagnostic pop
-
 void Lighting::Loader::light(const string &name)
 {
        obj.attach(get_collection().get<Light>(name));
@@ -193,29 +143,11 @@ void Lighting::Loader::light(const string &name)
 
 void Lighting::Loader::light_inline()
 {
-       RefPtr<Light> lgt = new Light;
-       load_sub(*lgt);
-       get_collection().add(format("%s/%d.light", FS::basename(get_source()), obj.lights.size()), lgt.get());
-       obj.attach(*lgt.release());
-}
-
-void Lighting::Loader::light_inline_index(unsigned)
-{
-       light_inline();
-}
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-void Lighting::Loader::sky_color(float r, float g, float b)
-{
-       obj.set_sky_color(Color(r, g, b));
-}
-
-void Lighting::Loader::zenith_direction(float x, float y, float z)
-{
-       obj.set_zenith_direction(Vector3(x, y, z));
+       Light::GenericLoader ldr(get_collection());
+       load_sub_with(ldr);
+       Light *lgt = ldr.store_object(get_collection(), format("%s/%d.light", FS::basename(get_source()), obj.lights.size()));
+       obj.attach(*lgt);
 }
-#pragma GCC diagnostic pop
 
 } // namespace GL
 } // namespace Msp