Skip to content

Commit

Permalink
add attenuation
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Sep 21, 2023
1 parent 99b3596 commit f68ca35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 4 additions & 6 deletions GLMakie/assets/shader/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ vec3 calc_point_light(int idx, vec3 normal, vec3 color) {
vec3 light_dir = normalize(light_vec);

// How weak has the light gotten due to distance
float attentuation = 1.0;
// float attentuation = 1.0 / (
// light_parameters[idx].x +
// light_parameters[idx].y * dist +
// light_parameters[idx].z * dist * dist
// );
// float attentuation = 1.0 / (1.0 + dist * dist * dist);
float attentuation = 1.0 / (
1.0 + light_parameters[idx].x * dist + light_parameters[idx].y * dist * dist
);

return attentuation * blinn_phong(light_colors[idx], normal, light_dir, color);
}
Expand Down
15 changes: 15 additions & 0 deletions src/lighting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,26 @@ A point-like light source placed at the given `position` with the given light
struct PointLight <: AbstractLight
color::Observable{RGBf}
position::Observable{Vec3f}
attenuation::Observable{Vec2f}
end

# no attenuation
PointLight(color, position) = PointLight(color, position, Vec2f(0))
# automatic attenuation
PointLight(color, position, range::Real) = PointLight(color, position, default_attenuation(range))

light_type(::PointLight) = LightType.PointLight
light_color(l::PointLight) = l.color[]
light_position(l::PointLight) = l.position[]
light_parameters(l::PointLight) = to_ndim(Vec3f, l.attenuation[], 0)

# fit of values used on learnopengl/ogre3d
function default_attenuation(range::Real)
return Vec2f(
4.690507869767646 * range ^ -1.009712247799057,
82.4447791934059 * range ^ -2.0192061630628966
)
end


"""
Expand Down

0 comments on commit f68ca35

Please sign in to comment.