Skip to content

Commit fa089db

Browse files
committed
generalise lighting material diffuse
1 parent e8c067e commit fa089db

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

  • lib/PDL/Graphics/TriD

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ version => <<'EOF',
7171
EOF
7272
light => <<'EOF',
7373
/* modified from https://community.khronos.org/t/help-with-gouraud-phong-shading-in-shaders/73192/2 */
74-
void light(int lightIndex, vec4 position, vec3 norm, out vec4 diffuse, out vec4 spec) {
74+
void light(int lightIndex, vec4 position, vec3 norm, vec4 in_diffuse, out vec4 diffuse, out vec4 spec) {
7575
vec3 n = normalize(norm);
7676
vec3 s = vec3(normalize(gl_LightSource[lightIndex].position - position));
7777
vec4 v = normalize(-position);
7878
vec4 r = vec4(reflect(-s, n), 1);
7979
float sDotN = max(dot(s, n), 0.0);
80-
diffuse = gl_LightSource[lightIndex].diffuse * gl_FrontMaterial.diffuse * sDotN;
80+
diffuse = gl_LightSource[lightIndex].diffuse * in_diffuse * sDotN;
8181
spec = gl_LightSource[lightIndex].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
8282
}
8383
EOF
@@ -101,9 +101,12 @@ fs_in_light_decl => <<'EOF',
101101
varying vec3 vNormal;
102102
varying vec4 vPosition;
103103
EOF
104+
fs_diffuse_material => <<'EOF',
105+
vec4 in_diffuse = gl_FrontMaterial.diffuse;
106+
EOF
104107
fs_out_light => <<'EOF',
105108
vec4 diffuse, spec;
106-
light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, diffuse, spec);
109+
light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, in_diffuse, diffuse, spec);
107110
gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
108111
EOF
109112
);
@@ -447,10 +450,10 @@ void main() {
447450
%s%s
448451
}
449452
EOF
450-
my $fragment_shader = sprintf <<'EOF', @SHADERBITS{qw(version fs_in_light_decl light fs_out_light)};
453+
my $fragment_shader = sprintf <<'EOF', @SHADERBITS{qw(version fs_in_light_decl light fs_diffuse_material fs_out_light)};
451454
%s%s%s
452455
void main() {
453-
%s
456+
%s%s
454457
}
455458
EOF
456459
my %SPHERE;

0 commit comments

Comments
 (0)