Skip to content

Commit

Permalink
fix WGLMakie
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Oct 10, 2023
1 parent fbe44f2 commit d05dde4
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions WGLMakie/assets/mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ in vec3 o_camdir;

vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
float backlight = get_backlight();
float diff_coeff = max(dot(L, -N), 0.0) + backlight * max(dot(L, N), 0.0);
float diff_coeff = clamp(dot(L, -N), 0.0, 1.0); // + backlight * max(dot(L, N), 0.0);

// specular coefficient
vec3 H = normalize(L + V);
Expand All @@ -18,7 +18,7 @@ vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
spec_coeff = 0.0;

// final lighting model
return vec3(
return get_light_color() * vec3(
get_diffuse() * diff_coeff * color +
get_specular() * spec_coeff
);
Expand Down
3 changes: 2 additions & 1 deletion WGLMakie/assets/mesh.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ out vec4 frag_color;

uniform mat4 projection;
uniform mat4 view;
uniform vec3 eyeposition;

vec3 tovec3(vec2 v){return vec3(v, 0.0);}
vec3 tovec3(vec3 v){return v;}
Expand Down Expand Up @@ -68,7 +69,7 @@ void render(vec4 position_world, vec3 normal, mat4 view, mat4 projection)
gl_Position = projection * view * position_world; // TODO consider using projectionview directly
gl_Position.z += gl_Position.w * get_depth_shift();
// direction to camera
o_camdir = position_world.xyz / position_world.w - get_eyeposition(); // TODO upload
o_camdir = position_world.xyz / position_world.w - eyeposition;
}

flat out uint frag_instance_id;
Expand Down
4 changes: 2 additions & 2 deletions WGLMakie/assets/particles.frag
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
spec_coeff = 0.0;

// final lighting model
return vec3(
return get_light_color() * vec3(
get_diffuse() * diff_coeff * color +
get_specular() * spec_coeff
);
Expand All @@ -35,7 +35,7 @@ vec4 pack_int(uint id, uint index) {
void main() {
vec3 L, N, light, color;
if (get_shading()) {
L = get_light_direction(); // TODO normalize outside
L = get_light_direction();
N = normalize(frag_normal);
light = blinnphong(N, normalize(o_camdir), L, frag_color.rgb);
color = get_ambient() * frag_color.rgb + light;
Expand Down
3 changes: 2 additions & 1 deletion WGLMakie/assets/particles.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ precision mediump float;

uniform mat4 projection;
uniform mat4 view;
uniform vec3 eyeposition;

out vec3 frag_normal;
out vec3 frag_position;
Expand Down Expand Up @@ -36,7 +37,7 @@ void main(){
frag_normal = N;
frag_color = to_vec4(get_color());
// direction to camera
o_camdir = position_world.xyz / position.w - get_eyeposition(); // TODO get_eyeposition
o_camdir = position_world.xyz / position.w - eyeposition;
// screen space coordinates of the position
gl_Position = projection * view * position_world;
gl_Position.z += gl_Position.w * get_depth_shift();
Expand Down
9 changes: 4 additions & 5 deletions WGLMakie/assets/volume.frag
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
vec3 H = normalize(L + V);
float spec_coeff = pow(max(dot(H, -N), 0.0) + max(dot(H, N), 0.0), shininess);
// final lighting model
return vec3(
ambient * color +
diffuse * diff_coeff * color +
specular * spec_coeff
return ambient * color + get_light_color() * vec3(
get_diffuse() * diff_coeff * color +
get_specular() * spec_coeff
);
}

Expand Down Expand Up @@ -129,7 +128,7 @@ vec4 contours(vec3 front, vec3 dir)
float opacity = density.a;
if(opacity > 0.0){
vec3 N = gennormal(pos, step_size);
vec3 L = get_light_direction(); // TODO normalize outside
vec3 L = get_light_direction();
vec3 opaque = blinnphong(N, camdir, L, density.rgb);
Lo += (T * opacity) * opaque;
T *= 1.0 - opacity;
Expand Down
3 changes: 2 additions & 1 deletion WGLMakie/src/imagelike.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ function create_shader(mscene::Scene, plot::Volume)
:depth_shift => get(plot, :depth_shift, Observable(0.0f0)),
# these get filled in later by serialization, but we need them
# as dummy values here, so that the correct uniforms are emitted
:lightposition => Vec3f(1),
:light_direction => Vec3f(1),
:light_color => Vec3f(1),
:eyeposition => Vec3f(1),
:ambient => Vec3f(1),
:picking => false,
Expand Down
3 changes: 2 additions & 1 deletion WGLMakie/src/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ function draw_mesh(mscene::Scene, per_vertex, plot, uniforms; permute_tex=true)

get!(uniforms, :pattern, false)
get!(uniforms, :model, plot.model)
get!(uniforms, :lightposition, Vec3f(1))
get!(uniforms, :ambient, Vec3f(1))
get!(uniforms, :light_direction, Vec3f(1))
get!(uniforms, :light_color, Vec3f(1))

uniforms[:interpolate_in_fragment_shader] = get(plot, :interpolate_in_fragment_shader, true)

Expand Down
11 changes: 9 additions & 2 deletions WGLMakie/src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,14 @@ function serialize_three(scene::Scene, plot::AbstractPlot)

dirlight = Makie.get_directional_light(scene)
if !isnothing(dirlight)
uniforms[:light_direction] = serialize_three(dirlight.direction[])
uniforms[:light_direction] = serialize_three(normalize(dirlight.direction[]))
on(dirlight.direction) do value
updater[] = [:light_direction, serialize_three(value)]
updater[] = [:light_direction, serialize_three(normalize(value))]
return
end
uniforms[:light_color] = serialize_three(dirlight.color[])
on(dirlight.color) do value
updater[] = [:light_color, serialize_three(value)]
return
end
end
Expand All @@ -357,6 +362,8 @@ function serialize_three(scene::Scene, plot::AbstractPlot)
key = haskey(plot, :markerspace) ? (:markerspace) : (:space)
mesh[:cam_space] = to_value(get(plot, key, :data))

@info uniforms[:light_color]

return mesh
end

Expand Down

0 comments on commit d05dde4

Please sign in to comment.