@@ -65,135 +65,6 @@ sub PDL::Graphics::TriD::Quaternion::togl {
6565# #################################
6666# Graph Objects
6767
68- my %SHADERBITS = (
69- version => <<'EOF' ,
70- #version 120
71- EOF
72- light => <<'EOF' ,
73- /* 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, vec4 in_diffuse, out vec4 diffuse, out vec4 spec) {
75- vec3 n = normalize(norm);
76- vec3 s = vec3(normalize(gl_LightSource[lightIndex].position - position));
77- vec4 v = normalize(-position);
78- vec4 r = vec4(reflect(-s, n), 1);
79- float sDotN = max(dot(s, n), 0.0);
80- diffuse = gl_LightSource[lightIndex].diffuse * in_diffuse * sDotN;
81- spec = gl_LightSource[lightIndex].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
82- }
83- EOF
84- vs_in_decl => <<'EOF' ,
85- attribute vec3 position;
86- EOF
87- vs_in_light_decl => <<'EOF' ,
88- attribute vec3 normal;
89- EOF
90- vs_in => <<'EOF' ,
91- vec4 the_position = vec4(position, 1);
92- EOF
93- vs_out => <<'EOF' ,
94- gl_Position = gl_ModelViewProjectionMatrix * the_position;
95- EOF
96- vs_out_light => <<'EOF' ,
97- vNormal = normalize(gl_NormalMatrix * normal);
98- vPosition = gl_ModelViewMatrix * the_position;
99- EOF
100- fs_in_light_decl => <<'EOF' ,
101- varying vec3 vNormal;
102- varying vec4 vPosition;
103- EOF
104- fs_diffuse_material => <<'EOF' ,
105- vec4 in_diffuse = gl_FrontMaterial.diffuse;
106- EOF
107- fs_out_light => <<'EOF' ,
108- vec4 diffuse, spec;
109- light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, in_diffuse, diffuse, spec);
110- gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
111- EOF
112- );
113-
114- { package # hide from PAUSE
115- PDL::Graphics::TriD::Labels;
116- use OpenGL::Modern qw(
117- glEnable glBlendFunc
118- glDrawElements_c
119- GL_BLEND GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
120- GL_TRIANGLES GL_UNSIGNED_INT
121- GL_RGBA32F GL_RGBA
122- ) ;
123- use PDL::Graphics::OpenGLQ;
124- my %FONT ;
125- sub _font_setup {
126- my ($fref ) = @_ ;
127- my ($texture , $rightbound , $orig ) = gl_font_texture();
128- $fref -> {texture } = PDL::float(1,1,1,1) * $texture -> dummy(0,1);
129- my $widthpix = $rightbound -> numdiff; $widthpix -> slice(' 0' ) += 1;
130- @{ $fref -> {widthpix } } = $widthpix -> list;
131- $fref -> {widthflt } = $widthpix -> float;
132- $fref -> {widthflt11 } = $fref -> {widthflt }-> t-> append([1,1])-> dummy(1);
133- $fref -> {heightpix } = $texture -> dim(1);
134- $fref -> {numchars } = my $numchars = $rightbound -> nelem;
135- $fref -> {texwidthm1 } = my $texwidthm1 = $texture -> dim(0) - 1;
136- $fref -> {leftbound } = my $leftbound = $rightbound -> rotate(1) + 1;
137- $leftbound -> slice(' 0' ) .= 0; $leftbound -> set_datatype(PDL::float-> enum);
138- $fref -> {rightbound } = $rightbound = $rightbound -> float;
139- $_ /= $texwidthm1 for $leftbound , $rightbound ;
140- @$fref {qw( xorig yorig) } = $orig -> list;
141- $fref -> {texture } = PDL::float(1,1,1,1) * $texture -> dummy(0,1);
142- # 4 = top-left, bot-left, top-right, bot-right, triangle idx=012,213
143- $fref -> {idx } = PDL-> new(PDL::ulong, [0,1,2], [2,1,3]);
144- $fref -> {texcoords } = my $texcoords = PDL-> zeroes(PDL::float,2,4,$numchars );
145- $texcoords -> slice(' (0),0:1' ) .= $leftbound -> dummy(0,1); # u of left
146- $texcoords -> slice(' (0),2:3' ) .= $rightbound -> dummy(0,1); # u of right
147- $texcoords -> slice(' (1),0::2' ) .= 1; # v of top, v bot=already 0
148- }
149- sub togl_setup {
150- my ($this ,$points ) = @_ ;
151- print " togl_setup $this \n " if $PDL::Graphics::TriD::verbose ;
152- if (!keys %FONT ) {
153- _font_setup(\%FONT );
154- $this -> load_texture(font_id => $FONT {texture }, GL_RGBA32F, ($FONT {texture }-> dims)[1,2], GL_RGBA);
155- $FONT {font_id } = $this -> {Impl }{font_id };
156- } else {
157- $this -> {Impl }{font_id } = $FONT {font_id };
158- }
159- $points //= $this -> {Points }; # as Labels is used in Graph
160- my $numchars = $FONT {numchars };
161- my $vert_template = PDL-> new(PDL::float, [0,0,1], [0,0,0], [1,0,1], [1,0,0]);
162- my $dwidth = $PDL::Graphics::TriD::Window::DEFAULT_WIDTH / 1.5;
163- my $dheight = $PDL::Graphics::TriD::Window::DEFAULT_HEIGHT / 1.5;
164- $vert_template *= PDL::float(1 / $dwidth , 1, $FONT {heightpix } / $dheight );
165- my @codes = map [map ord , split //], @{ $this -> {Strings } };
166- my ($v2 , @v1 , @v3 ) = PDL-> null;
167- for (0..$#codes ) {
168- my ($l , $xoffset ) = ($codes [$_ ], 0);
169- PDL::barf " Codepoint $_ >= $numchars " for grep $_ >= $numchars , @$l ;
170- push @v1 , ($_ ) x @$l ;
171- push @v3 , @$l ;
172- $v2 = PDL::glue(0,$v2 ,$FONT {widthflt }-> dice_axis(0,$l )-> cumusumover);
173- }
174- my $v = $points -> dice_axis(1, \@v1 )-> dummy(1) +
175- ($v2 -> t-> append([0,0])-> dummy(1) / $dwidth ) +
176- $vert_template * $FONT {widthflt11 }-> dice_axis(2,\@v3 );
177- $this -> load_buffer(vert_buf => $v -> clump(1,2));
178- $this -> load_buffer(texc_buf => $FONT {texcoords }-> dice_axis(2,\@v3 )-> clump(1,2));
179- $this -> load_idx_buffer(indx_buf => $this -> {Impl }{idx } = $FONT {idx }-> flat + 4 * PDL-> sequence(PDL::ulong,1,0+@v1 ));
180- $this -> togl_unbind;
181- }
182- sub gdraw {
183- my ($this ,$points ) = @_ ;
184- $this -> togl_bind;
185- glEnable(GL_BLEND);
186- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
187- glDrawElements_c(GL_TRIANGLES, $this -> {Impl }{idx }-> nelem, GL_UNSIGNED_INT, 0);
188- $this -> togl_unbind;
189- }
190- }
191-
192- my %mode2enum = (
193- linestrip => GL_LINE_STRIP,
194- lineloop => GL_LINE_LOOP,
195- );
196-
19768{ package # hide from PAUSE
19869 PDL::Graphics::TriD::GObject;
19970use OpenGL::Modern qw(
@@ -414,6 +285,130 @@ sub DESTROY {
414285}
415286}
416287
288+ my %SHADERBITS = (
289+ version => <<'EOF' ,
290+ #version 120
291+ EOF
292+ light => <<'EOF' ,
293+ /* modified from https://community.khronos.org/t/help-with-gouraud-phong-shading-in-shaders/73192/2 */
294+ void light(int lightIndex, vec4 position, vec3 norm, vec4 in_diffuse, out vec4 diffuse, out vec4 spec) {
295+ vec3 n = normalize(norm);
296+ vec3 s = vec3(normalize(gl_LightSource[lightIndex].position - position));
297+ vec4 v = normalize(-position);
298+ vec4 r = vec4(reflect(-s, n), 1);
299+ float sDotN = max(dot(s, n), 0.0);
300+ diffuse = gl_LightSource[lightIndex].diffuse * in_diffuse * sDotN;
301+ spec = gl_LightSource[lightIndex].specular * gl_FrontMaterial.specular * pow(max(dot(r,v), 0.0), gl_FrontMaterial.shininess);
302+ }
303+ EOF
304+ vs_in_decl => <<'EOF' ,
305+ attribute vec3 position;
306+ EOF
307+ vs_in_light_decl => <<'EOF' ,
308+ attribute vec3 normal;
309+ EOF
310+ vs_in => <<'EOF' ,
311+ vec4 the_position = vec4(position, 1);
312+ EOF
313+ vs_out => <<'EOF' ,
314+ gl_Position = gl_ModelViewProjectionMatrix * the_position;
315+ EOF
316+ vs_out_light => <<'EOF' ,
317+ vNormal = normalize(gl_NormalMatrix * normal);
318+ vPosition = gl_ModelViewMatrix * the_position;
319+ EOF
320+ fs_in_light_decl => <<'EOF' ,
321+ varying vec3 vNormal;
322+ varying vec4 vPosition;
323+ EOF
324+ fs_diffuse_material => <<'EOF' ,
325+ vec4 in_diffuse = gl_FrontMaterial.diffuse;
326+ EOF
327+ fs_out_light => <<'EOF' ,
328+ vec4 diffuse, spec;
329+ light(0, vPosition, gl_FrontFacing ? vNormal : -vNormal, in_diffuse, diffuse, spec);
330+ gl_FragColor = gl_FrontLightProduct[0].ambient + diffuse + spec;
331+ EOF
332+ );
333+
334+ { package # hide from PAUSE
335+ PDL::Graphics::TriD::Labels;
336+ use OpenGL::Modern qw(
337+ glEnable glBlendFunc
338+ glDrawElements_c
339+ GL_BLEND GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
340+ GL_TRIANGLES GL_UNSIGNED_INT
341+ GL_RGBA32F GL_RGBA
342+ ) ;
343+ use PDL::Graphics::OpenGLQ;
344+ my %FONT ;
345+ sub _font_setup {
346+ my ($fref ) = @_ ;
347+ my ($texture , $rightbound , $orig ) = gl_font_texture();
348+ $fref -> {texture } = PDL::float(1,1,1,1) * $texture -> dummy(0,1);
349+ my $widthpix = $rightbound -> numdiff; $widthpix -> slice(' 0' ) += 1;
350+ @{ $fref -> {widthpix } } = $widthpix -> list;
351+ $fref -> {widthflt } = $widthpix -> float;
352+ $fref -> {widthflt11 } = $fref -> {widthflt }-> t-> append([1,1])-> dummy(1);
353+ $fref -> {heightpix } = $texture -> dim(1);
354+ $fref -> {numchars } = my $numchars = $rightbound -> nelem;
355+ $fref -> {texwidthm1 } = my $texwidthm1 = $texture -> dim(0) - 1;
356+ $fref -> {leftbound } = my $leftbound = $rightbound -> rotate(1) + 1;
357+ $leftbound -> slice(' 0' ) .= 0; $leftbound -> set_datatype(PDL::float-> enum);
358+ $fref -> {rightbound } = $rightbound = $rightbound -> float;
359+ $_ /= $texwidthm1 for $leftbound , $rightbound ;
360+ @$fref {qw( xorig yorig) } = $orig -> list;
361+ $fref -> {texture } = PDL::float(1,1,1,1) * $texture -> dummy(0,1);
362+ # 4 = top-left, bot-left, top-right, bot-right, triangle idx=012,213
363+ $fref -> {idx } = PDL-> new(PDL::ulong, [0,1,2], [2,1,3]);
364+ $fref -> {texcoords } = my $texcoords = PDL-> zeroes(PDL::float,2,4,$numchars );
365+ $texcoords -> slice(' (0),0:1' ) .= $leftbound -> dummy(0,1); # u of left
366+ $texcoords -> slice(' (0),2:3' ) .= $rightbound -> dummy(0,1); # u of right
367+ $texcoords -> slice(' (1),0::2' ) .= 1; # v of top, v bot=already 0
368+ }
369+ sub togl_setup {
370+ my ($this ,$points ) = @_ ;
371+ print " togl_setup $this \n " if $PDL::Graphics::TriD::verbose ;
372+ if (!keys %FONT ) {
373+ _font_setup(\%FONT );
374+ $this -> load_texture(font_id => $FONT {texture }, GL_RGBA32F, ($FONT {texture }-> dims)[1,2], GL_RGBA);
375+ $FONT {font_id } = $this -> {Impl }{font_id };
376+ } else {
377+ $this -> {Impl }{font_id } = $FONT {font_id };
378+ }
379+ $points //= $this -> {Points }; # as Labels is used in Graph
380+ my $numchars = $FONT {numchars };
381+ my $vert_template = PDL-> new(PDL::float, [0,0,1], [0,0,0], [1,0,1], [1,0,0]);
382+ my $dwidth = $PDL::Graphics::TriD::Window::DEFAULT_WIDTH / 1.5;
383+ my $dheight = $PDL::Graphics::TriD::Window::DEFAULT_HEIGHT / 1.5;
384+ $vert_template *= PDL::float(1 / $dwidth , 1, $FONT {heightpix } / $dheight );
385+ my @codes = map [map ord , split //], @{ $this -> {Strings } };
386+ my ($v2 , @v1 , @v3 ) = PDL-> null;
387+ for (0..$#codes ) {
388+ my ($l , $xoffset ) = ($codes [$_ ], 0);
389+ PDL::barf " Codepoint $_ >= $numchars " for grep $_ >= $numchars , @$l ;
390+ push @v1 , ($_ ) x @$l ;
391+ push @v3 , @$l ;
392+ $v2 = PDL::glue(0,$v2 ,$FONT {widthflt }-> dice_axis(0,$l )-> cumusumover);
393+ }
394+ my $v = $points -> dice_axis(1, \@v1 )-> dummy(1) +
395+ ($v2 -> t-> append([0,0])-> dummy(1) / $dwidth ) +
396+ $vert_template * $FONT {widthflt11 }-> dice_axis(2,\@v3 );
397+ $this -> load_buffer(vert_buf => $v -> clump(1,2));
398+ $this -> load_buffer(texc_buf => $FONT {texcoords }-> dice_axis(2,\@v3 )-> clump(1,2));
399+ $this -> load_idx_buffer(indx_buf => $this -> {Impl }{idx } = $FONT {idx }-> flat + 4 * PDL-> sequence(PDL::ulong,1,0+@v1 ));
400+ $this -> togl_unbind;
401+ }
402+ sub gdraw {
403+ my ($this ,$points ) = @_ ;
404+ $this -> togl_bind;
405+ glEnable(GL_BLEND);
406+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
407+ glDrawElements_c(GL_TRIANGLES, $this -> {Impl }{idx }-> nelem, GL_UNSIGNED_INT, 0);
408+ $this -> togl_unbind;
409+ }
410+ }
411+
417412{ package # hide from PAUSE
418413 PDL::Graphics::TriD::GL::Primitive;
419414use OpenGL::Modern qw( glDrawArrays) ;
@@ -530,6 +525,11 @@ sub gdraw {
530525}
531526}
532527
528+ my %mode2enum = (
529+ linestrip => GL_LINE_STRIP,
530+ lineloop => GL_LINE_LOOP,
531+ );
532+
533533{ package # hide from PAUSE
534534 PDL::Graphics::TriD::DrawMulti;
535535use OpenGL::Modern qw( glMultiDrawElements_c GL_UNSIGNED_INT) ;
0 commit comments