Skip to content

Commit 25753b3

Browse files
committed
add lightIndex param to shader light function
1 parent 671e82a commit 25753b3

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

  • lib/PDL/Graphics/TriD

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,25 +414,19 @@ my $fragment_shader = <<'EOF';
414414
#version 120
415415
varying vec3 vNormal;
416416
varying vec4 vPosition;
417-
418417
/* modified from https://community.khronos.org/t/help-with-gouraud-phong-shading-in-shaders/73192/2 */
419-
void light(vec4 position, vec3 norm, out vec4 diffuse, out vec4 spec) {
418+
void light(int lightIndex, vec4 position, vec3 norm, out vec4 diffuse, out vec4 spec) {
420419
vec3 n = normalize(norm);
421-
vec3 s = vec3(normalize(gl_LightSource[0].position - position));
420+
vec3 s = vec3(normalize(gl_LightSource[lightIndex].position - position));
422421
vec4 v = normalize(-position);
423422
vec4 r = vec4(reflect(-s, n), 1);
424423
float sDotN = max(dot(s, n), 0.0);
425-
diffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * sDotN;
426-
spec = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
424+
diffuse = gl_LightSource[lightIndex].diffuse * gl_FrontMaterial.diffuse * sDotN;
425+
spec = gl_LightSource[lightIndex].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
427426
}
428-
429427
void main() {
430428
vec4 diffuse, spec;
431-
if (gl_FrontFacing) {
432-
light(vPosition, vNormal, diffuse, spec);
433-
} else {
434-
light(vPosition, -vNormal, diffuse, spec);
435-
}
429+
light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, diffuse, spec);
436430
gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
437431
}
438432
EOF

0 commit comments

Comments
 (0)