From: Mikko Rasa Date: Sat, 9 Oct 2021 17:58:14 +0000 (+0300) Subject: Add attenuation calculations and related export code for point lights X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f501ffe2862fd8b4c5793542190e8e0a0d8de667;p=libs%2Fgl.git Add attenuation calculations and related export code for point lights --- diff --git a/blender/io_mspgl/export_light.py b/blender/io_mspgl/export_light.py index 2fc8c8dd..7ccb9663 100644 --- a/blender/io_mspgl/export_light.py +++ b/blender/io_mspgl/export_light.py @@ -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)) diff --git a/shaderlib/common.glsl b/shaderlib/common.glsl index 4d6112d9..33fefe88 100644 --- a/shaderlib/common.glsl +++ b/shaderlib/common.glsl @@ -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) diff --git a/shaderlib/msp_interface.glsl b/shaderlib/msp_interface.glsl index d8de5d24..b0f8a410 100644 --- a/shaderlib/msp_interface.glsl +++ b/shaderlib/msp_interface.glsl @@ -3,6 +3,7 @@ struct LightSourceParameters vec4 position; vec3 color; int type; + vec3 attenuation; }; struct ClipPlane diff --git a/source/materials/lighting.cpp b/source/materials/lighting.cpp index f6bb39ff..6957126b 100644 --- a/source/materials/lighting.cpp +++ b/source/materials/lighting.cpp @@ -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); } } diff --git a/source/materials/pointlight.cpp b/source/materials/pointlight.cpp index 9a726a2a..3147eaa3 100644 --- a/source/materials/pointlight.cpp +++ b/source/materials/pointlight.cpp @@ -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); } diff --git a/source/materials/pointlight.h b/source/materials/pointlight.h index 1c373504..ed9789c7 100644 --- a/source/materials/pointlight.h +++ b/source/materials/pointlight.h @@ -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();