From f501ffe2862fd8b4c5793542190e8e0a0d8de667 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 9 Oct 2021 20:58:14 +0300 Subject: [PATCH] Add attenuation calculations and related export code for point lights --- blender/io_mspgl/export_light.py | 1 + shaderlib/common.glsl | 3 ++- shaderlib/msp_interface.glsl | 1 + source/materials/lighting.cpp | 3 ++- source/materials/pointlight.cpp | 1 + source/materials/pointlight.h | 2 +- 6 files changed, 8 insertions(+), 3 deletions(-) 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(); -- 2.43.0