@@ -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,27 @@ 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+ attribute vec3 normal;
84+ EOF
85+ vs_in => <<'EOF' ,
86+ vec4 the_position = vec4(position, 1);
87+ EOF
88+ vs_out_light => <<'EOF' ,
89+ vNormal = normalize(gl_NormalMatrix * normal);
90+ vPosition = gl_ModelViewMatrix * the_position;
91+ gl_Position = gl_ModelViewProjectionMatrix * the_position;
92+ EOF
93+ fs_in_decl => <<'EOF' ,
94+ varying vec3 vNormal;
95+ varying vec4 vPosition;
96+ EOF
97+ fs_out_light => <<'EOF' ,
98+ vec4 diffuse, spec;
99+ light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, diffuse, spec);
100+ gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
101+ EOF
80102);
81103
82104{ package # hide from PAUSE
@@ -409,30 +431,21 @@ use OpenGL::Modern qw(
409431 glVertexAttribDivisor glDrawElementsInstancedARB_c
410432 GL_TRIANGLE_STRIP GL_UNSIGNED_INT
411433) ;
412- my $vertex_shader = <<'EOF' ;
434+ my $vertex_shader = sprintf <<'EOF' , @SHADERBITS { qw( vs_in_decl fs_in_decl vs_in vs_out_light ) } ;
413435#version 120
414- attribute vec3 position;
415- attribute vec3 normal;
436+ %s%s
416437attribute vec3 offset;
417- varying vec3 vNormal;
418- varying vec4 vPosition;
419438void 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;
439+ %s
440+ the_position += vec4(offset, 0);
441+ %s
424442}
425443EOF
426- my $fragment_shader = sprintf <<'EOF' , $ SHADERBITS {light };
444+ my $fragment_shader = sprintf <<'EOF' , @ SHADERBITS {qw( fs_in_decl light fs_out_light ) };
427445#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
446+ %s%s
432447void main() {
433- vec4 diffuse, spec;
434- light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, diffuse, spec);
435- gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
448+ %s
436449}
437450EOF
438451my %SPHERE ;
0 commit comments