]> git.tdb.fi Git - libs/gl.git/commitdiff
Add attenuation calculations and related export code for point lights
authorMikko Rasa <tdb@tdb.fi>
Sat, 9 Oct 2021 17:58:14 +0000 (20:58 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 9 Oct 2021 17:58:14 +0000 (20:58 +0300)
blender/io_mspgl/export_light.py
shaderlib/common.glsl
shaderlib/msp_interface.glsl
source/materials/lighting.cpp
source/materials/pointlight.cpp
source/materials/pointlight.h

index 2fc8c8dd02bd3dbccec4b0204dd75ed0930c378e..7ccb966354faa2c2fd739316aa324379938ec240 100644 (file)
@@ -16,6 +16,7 @@ class LightExporter:
                        light_res.statements.append(Statement("type", Token("point")))
                        pos = obj.matrix_world@mathutils.Vector((0.0, 0.0, 0.0, 1.0))
                        light_res.statements.append(Statement("position", *obj.matrix_world.col[3][0:3]))
+                       light_res.statements.append(Statement("attenuation", 1.0, 0.0, 1.0))
                else:
                        raise Exception("Can't export light {} of unknown type {}".format(light.name, light.type))
 
index 4d6112d9f2b37a58127f6a5330837b5b55973217..33fefe8806deb4ddfa7240c5394f1778f082d1c1 100644 (file)
@@ -90,7 +90,8 @@ virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
        vec4 light_pos = light_sources[index].position;
        vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w;
        float d = length(rel_pos);
-       return IncomingLight(rel_pos/d, light_sources[index].color);
+       float attenuation = 1.0/dot(vec3(1.0, d, d*d), light_sources[index].attenuation);
+       return IncomingLight(rel_pos/d, light_sources[index].color*attenuation);
 }
 
 virtual vec3 get_environment_sample(vec3 direction, float roughness)
index d8de5d240b90f6c416cac9a7b7cc37a361debe1c..b0f8a410cfce832dadc737b918f77017b3242d08 100644 (file)
@@ -3,6 +3,7 @@ struct LightSourceParameters
        vec4 position;
        vec3 color;
        int type;
+       vec3 attenuation;
 };
 
 struct ClipPlane
index f6bb39ffce98ecd701b901c223bf1244e415a9ae..6957126b6c3bfeef3ad37192c541454fecde2eef 100644 (file)
@@ -18,12 +18,13 @@ Lighting::Lighting()
        set_fog_color(Color(0.0f, 0.0f, 0.0f, 0.0f));
        set_fog_density(0.0f);
 
-       for(unsigned i=0; i<8; ++i)
+       for(unsigned i=0; i<7; ++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);
        }
 }
 
index 9a726a2a982f143d1adaee43f9226c233d9ade7e..3147eaa31bab0640fbe9c7aa62b54f5729890f5e 100644 (file)
@@ -39,6 +39,7 @@ void PointLight::update_shader_data(ProgramData &shdata, const string &base) con
        shdata.uniform(base+".type", 2);
        shdata.uniform(base+".position", position.x, position.y, position.z, 1.0f);
        shdata.uniform(base+".color", color.r, color.g, color.b);
+       shdata.uniform3(base+".attenuation", attenuation);
 }
 
 
index 1c37350439a61e8adac9d87581f6d453e365f1d0..ed9789c7b0d2de622bc04e61800205260347dc0d 100644 (file)
@@ -31,7 +31,7 @@ public:
 
 private:
        Vector3 position = { 0.0f, 0.0f, 0.0f };
-       float attenuation[3] = { 1.0f, 0.0f, 0.1f };
+       float attenuation[3] = { 1.0f, 0.0f, 1.0f };
 
        void update_matrix();