@@ -66,7 +66,8 @@ sub PDL::Graphics::TriD::Quaternion::togl {
6666# Graph Objects
6767
6868my %SHADERBITS = (
69- light => <<'EOF' ,
69+ light => <<'EOF' ,
70+ /* modified from https://community.khronos.org/t/help-with-gouraud-phong-shading-in-shaders/73192/2 */
7071void light(int lightIndex, vec4 position, vec3 norm, out vec4 diffuse, out vec4 spec) {
7172 vec3 n = normalize(norm);
7273 vec3 s = vec3(normalize(gl_LightSource[lightIndex].position - position));
@@ -77,6 +78,31 @@ void light(int lightIndex, vec4 position, vec3 norm, out vec4 diffuse, out vec4
7778 spec = gl_LightSource[lightIndex].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
7879}
7980EOF
81+ vs_in_decl => <<'EOF' ,
82+ attribute vec3 position;
83+ EOF
84+ vs_in_light_decl => <<'EOF' ,
85+ attribute vec3 normal;
86+ EOF
87+ vs_in => <<'EOF' ,
88+ vec4 the_position = vec4(position, 1);
89+ EOF
90+ vs_out => <<'EOF' ,
91+ gl_Position = gl_ModelViewProjectionMatrix * the_position;
92+ EOF
93+ vs_out_light => <<'EOF' ,
94+ vNormal = normalize(gl_NormalMatrix * normal);
95+ vPosition = gl_ModelViewMatrix * the_position;
96+ EOF
97+ fs_in_light_decl => <<'EOF' ,
98+ varying vec3 vNormal;
99+ varying vec4 vPosition;
100+ EOF
101+ fs_out_light => <<'EOF' ,
102+ vec4 diffuse, spec;
103+ light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, diffuse, spec);
104+ gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
105+ EOF
80106);
81107
82108{ package # hide from PAUSE
@@ -409,30 +435,21 @@ use OpenGL::Modern qw(
409435 glVertexAttribDivisor glDrawElementsInstancedARB_c
410436 GL_TRIANGLE_STRIP GL_UNSIGNED_INT
411437) ;
412- my $vertex_shader = <<'EOF' ;
438+ my $vertex_shader = sprintf <<'EOF' , @SHADERBITS { qw( vs_in_decl vs_in_light_decl fs_in_light_decl vs_in vs_out vs_out_light ) } ;
413439#version 120
414- attribute vec3 position;
415- attribute vec3 normal;
440+ %s%s%s
416441attribute vec3 offset;
417- varying vec3 vNormal;
418- varying vec4 vPosition;
419442void main() {
420- vec4 offset_position = vec4(position + offset, 1);
421- vNormal = normalize(gl_NormalMatrix * normal);
422- vPosition = gl_ModelViewMatrix * offset_position;
423- gl_Position = gl_ModelViewProjectionMatrix * offset_position;
443+ %s
444+ the_position += vec4(offset, 0);
445+ %s%s
424446}
425447EOF
426- my $fragment_shader = sprintf <<'EOF' , $ SHADERBITS {light };
448+ my $fragment_shader = sprintf <<'EOF' , @ SHADERBITS {qw( fs_in_light_decl light fs_out_light ) };
427449#version 120
428- varying vec3 vNormal;
429- varying vec4 vPosition;
430- /* modified from https://community.khronos.org/t/help-with-gouraud-phong-shading-in-shaders/73192/2 */
431- %s
450+ %s%s
432451void main() {
433- vec4 diffuse, spec;
434- light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, diffuse, spec);
435- gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
452+ %s
436453}
437454EOF
438455my %SPHERE ;
0 commit comments