Skip to content

Commit

Permalink
update RPRMakie, tweak SpotLight
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Sep 23, 2023
1 parent 81471db commit 0d126b5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
10 changes: 5 additions & 5 deletions GLMakie/assets/shader/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ vec3 calc_spot_light(vec3 light_color, uint idx, vec3 normal, vec3 color) {
// extract args
vec3 position = vec3(light_parameters[idx], light_parameters[idx+1], light_parameters[idx+2]);
vec3 light_dir = -normalize(vec3(light_parameters[idx+3], light_parameters[idx+4], light_parameters[idx+5]));
float limit = light_parameters[idx+6]; // cos(opening_angle)
float inner_angle = light_parameters[idx+6]; // cos applied
float outer_angle = light_parameters[idx+7]; // cos applied

vec3 vertex_dir = normalize(position - o_view_pos);
float epsilon = 0.5 * (1 - limit); // differece between limit and 0.5 * (1 + limit)
float discriminator = smoothstep(limit - epsilon, limit + epsilon, dot(vertex_dir, light_dir));
float intensity = (dot(vertex_dir, light_dir) - outer_angle) / (inner_angle - outer_angle);

return discriminator * blinn_phong(light_color, normal, vertex_dir, color);
return intensity * blinn_phong(light_color, normal, vertex_dir, color);
}

vec3 illuminate(vec3 normal, vec3 base_color) {
Expand All @@ -100,7 +100,7 @@ vec3 illuminate(vec3 normal, vec3 base_color) {
break;
case SpotLight:
final_color += calc_spot_light(light_colors[i], idx, normal, base_color);
idx += 7; // 3 position, 3 direction, 1 parameter
idx += 8; // 3 position, 3 direction, 1 parameter
break;
default:
return vec3(1,0,1); // debug magenta
Expand Down
6 changes: 3 additions & 3 deletions GLMakie/src/drawing_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function handle_lights(attr::Dict, screen::Screen, lights::Vector{Makie.Abstract
elseif light isa DirectionalLight
n_params += 3
elseif light isa SpotLight
n_params += 7
n_params += 8
end
if n_params > MAX_PARAMS || n_lights == MAX_LIGHTS
if n_params > MAX_PARAMS
Expand Down Expand Up @@ -70,8 +70,8 @@ function handle_lights(attr::Dict, screen::Screen, lights::Vector{Makie.Abstract
elseif light isa SpotLight
p = v * to_ndim(Point4f, light.position[], 1)
d = nv * light.direction[]
l = cos(light.opening_angle[])
push!(parameters, p[1] / p[4], p[2] / p[4], p[3] / p[4], d[1], d[2], d[3], l)
l = cos.(light.angles[])
push!(parameters, p[1] / p[4], p[2] / p[4], p[3] / p[4], d[1], d[2], d[3], l[1], l[2])
end
end
notify(light_params_obs)
Expand Down
40 changes: 38 additions & 2 deletions RPRMakie/src/scene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,53 @@ function insert_plots!(context, matsys, scene, mscene::Makie.Scene, @nospecializ
end
end

# TODO attenuation
function to_rpr_light(context::RPR.Context, light::Makie.PointLight)
pointlight = RPR.PointLight(context)
map(light.position) do pos
transform!(pointlight, Makie.translationmatrix(pos))
end
map(light.radiance) do r
setradiantpower!(pointlight, red(r), green(r), blue(r))
map(light.color) do c
setradiantpower!(pointlight, red(c), green(c), blue(c))
end
return pointlight
end

# TODO: Move to RadeonProRender.jl
function RPR.RPR.rprContextCreateSpotLight(context)
out_light = Ref{RPR.rpr_light}()
RPR.RPR.rprContextCreateSpotLight(context, out_light)
return out_light[]
end

function to_rpr_light(context::RPR.Context, light::Makie.DirectionalLight)
directionallight = RPR.DirectionalLight(context)
# TODO
map(light.direction) do dir
quart = Makie.rotation_between(dir, Vec3f(0,0,-1))
transform!(directionallight, Makie.rotationmatrix4(quart))
end
map(light.color) do c
setradiantpower!(directionallight, red(c), green(c), blue(c))
end
return directionallight
end

function to_rpr_light(context::RPR.Context, light::Makie.SpotLight)
spotlight = RPR.SpotLight(context)
map(light.position, light.direction) do pos, dir
quart = Makie.rotation_between(dir, Vec3f(0,0,-1))
transform!(spotlight, Makie.translationmatrix(pos) * Makie.rotationmatrix4(quart))
end
map(light.color) do c
setradiantpower!(spotlight, red(c), green(c), blue(c))
end
map(light.angles) do (inner, outer)
RadeonProRender.RPR.rprSpotLightSetConeShape(spotlight, inner, outer)
end
return spotlight
end

function to_rpr_light(context::RPR.Context, light::Makie.AmbientLight)
env_img = fill(light.color[], 1, 1)
img = RPR.Image(context, env_img)
Expand Down
20 changes: 14 additions & 6 deletions src/lighting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ struct PointLight <: AbstractLight
end

# no attenuation
PointLight(color, position) = PointLight(color, position, Vec2f(0))
function PointLight(color::Union{Colorant, Observable{<: Colorant}}, position::Union{VecTypes{3}, Observable{<: VecTypes{3}}})
return PointLight(color, position, Vec2f(0))
end
# automatic attenuation
PointLight(color, position, range::Real) = PointLight(color, position, default_attenuation(range))
function PointLight(color::Union{Colorant, Observable{<: Colorant}}, position::Union{VecTypes{3}, Observable{<: VecTypes{3}}}, range::Real)
return PointLight(color, position, default_attenuation(range))
end

@deprecate PointLight(position::Union{VecTypes{3}, Observable{<: VecTypes{3}}}, color::Union{Colorant, Observable{<: Colorant}}) PointLight(color, position)

light_type(::PointLight) = LightType.PointLight
light_color(l::PointLight) = l.color[]
Expand Down Expand Up @@ -101,22 +107,24 @@ light_direction(l::DirectionalLight) = l.direction[]


"""
SpotLight(color, position, direction, opening_angle)
SpotLight(color, position, direction, angles)
Creates a spot light which illuminates a cone
Creates a spot light which illuminates objects in a light cone starting at
`position` pointing in `direction`. The opening angle is defined by an inner
and outer angle in `angles` between which the light intensity drops off.
"""
struct SpotLight <: AbstractLight
color::Observable{RGBf}
position::Observable{Vec3f}
direction::Observable{Vec3f}
opening_angle::Observable{Float32}
angles::Observable{Vec2f}
end

light_type(::SpotLight) = LightType.SpotLight
light_color(l::SpotLight) = l.color[]
light_position(l::SpotLight) = l.position[]
light_direction(l::SpotLight) = l.direction[]
light_parameters(l::SpotLight) = l.opening_angle[]
light_parameters(l::SpotLight) = l.angles[]


"""
Expand Down

0 comments on commit 0d126b5

Please sign in to comment.