Skip to content

Commit 0646a70

Browse files
committed
::Triangles to also take \[$2dcolor,$texcoord] as colours, ::Lattice uses
1 parent 9bf601b commit 0646a70

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,18 @@ use OpenGL::Modern qw(
338338
glShadeModel glColorMaterial glEnable glDisable
339339
glDrawElements_c
340340
GL_FLAT GL_SMOOTH GL_FRONT_AND_BACK GL_DIFFUSE GL_COLOR_MATERIAL
341-
GL_TRIANGLES GL_UNSIGNED_INT
341+
GL_TRIANGLES GL_UNSIGNED_INT GL_RGB
342342
);
343343
sub togl_setup {
344344
my ($this,$points) = @_;
345345
print "togl_setup $this\n" if $PDL::Graphics::TriD::verbose;
346346
$this->load_buffer(vert_buf => $points);
347-
$this->load_buffer(color_buf => $this->{Colors});
347+
if (defined $this->{Colors}) {
348+
$this->load_buffer(color_buf => $this->{Colors});
349+
} else {
350+
$this->load_buffer(texc_buf => $this->{TexCoord});
351+
$this->load_texture(tex_id => $this->{TexColors}, GL_RGB, ($this->{TexColors}->dims)[1,2], GL_RGB);
352+
}
348353
$this->load_idx_buffer(indx_buf => $this->{Faceidx});
349354
$this->load_buffer(norm_buf => $this->{Normals}) if $this->{Options}{Shading} > 2;
350355
$this->togl_unbind;

lib/PDL/Graphics/TriD/Object.pm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ sub new {
1919

2020
sub normalise_as {
2121
my ($this, $as, $what, $points) = @_;
22+
if (ref $what eq 'REF') {
23+
die "Given scalar-ref but not as 'COLOR'" if $as ne "COLOR";
24+
die "Given scalar-ref as 'COLOR' but not to array-ref" if ref $$what ne 'ARRAY';
25+
die "Given \\[...] as 'COLOR' but not 2 elts" if @$$what != 2;
26+
die "Given \\[\$x,\$y] as 'COLOR' but at least one is not ndarray" if grep !UNIVERSAL::isa($_, 'PDL'), @$$what;
27+
my @xdims = $$what->[0]->dims;
28+
die "Given \\[\$x,\$y] as 'COLOR' but \$x is not float(3,x,y)" if @xdims != 3 or $xdims[0] != 3 or $$what->[0]->type ne 'float';
29+
my @ydims = $$what->[1]->dims;
30+
die "Given \\[\$x,\$y] as 'COLOR' but \$y is not float(2,...)" if @ydims < 2 or $ydims[0] != 2 or $$what->[1]->type ne 'float';
31+
return $what;
32+
}
2233
if ($as eq "COLOR" and UNIVERSAL::isa($what, 'PDL') and $what->ndims == 1) {
2334
die "Given 1D ndarray as colour but no points to match" if !defined $points;
2435
return $this->cdummies($what->float,$points);

lib/PDL/Graphics/TriD/Objects.pm

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,20 @@ sub cdummies { $_[1]->dummy(1,$_[2]->getdim(1)); }
175175
package # hide from PAUSE
176176
PDL::Graphics::TriD::Triangles;
177177
use base qw/PDL::Graphics::TriD::GObject/;
178-
use fields qw/Faceidx Normals/;
178+
use fields qw/Faceidx Normals TexColors TexCoord/;
179179
use PDL::Graphics::OpenGLQ;
180180
sub new {
181181
my $options = ref($_[-1]) eq 'HASH' ? pop : {};
182182
my ($type,$points,$faceidx,$colors) = @_;
183183
my $this = $type->SUPER::new($points,$colors,$options);
184184
$faceidx = $this->{Faceidx} = $faceidx->ulong; # (3,nfaces) indices
185-
PDL::barf "Triangles error: broadcast dimensions forbidden for '$_' [@{[$this->{$_}->dims]}]" for grep $this->{$_}->ndims != 2, qw(Points Colors Faceidx);
186-
PDL::barf "Triangles error: dimension mismatch between Points [@{[$this->{Points}->dims]}] and Colors [@{[$this->{Colors}->dims]}]" if $this->{Points}->ndims != $this->{Colors}->ndims or $this->{Points}->dim(1) != $this->{Colors}->dim(1);
185+
if (ref $this->{Colors} eq 'REF') {
186+
@$this{qw(TexColors TexCoord)} = @${ $this->{Colors} };
187+
$this->{Colors} = undef;
188+
}
189+
PDL::barf "Triangles error: broadcast dimensions forbidden for '$_' [@{[$this->{$_}->dims]}]" for grep defined $this->{$_} && $this->{$_}->ndims != 2, qw(Points Colors Faceidx TexCoord);
190+
my ($colour_cmp) = grep defined, @$this{qw(Colors TexCoord)};
191+
PDL::barf "Triangles error: dimension mismatch between Points [@{[$this->{Points}->dims]}] and Colors/TexCoord [@{[$colour_cmp->dims]}]" if $this->{Points}->ndims != $colour_cmp->ndims or $this->{Points}->dim(1) != $colour_cmp->dim(1);
187192
$options = $this->{Options};
188193
my ($idxflat, $idx0, @idxdims) = ($faceidx->flat, $faceidx->dims);
189194
if ($options->{Shading} or $options->{ShowNormals}) {
@@ -264,7 +269,10 @@ sub new {
264269
my @colordims = $colors->dims;
265270
PDL::barf "Lattice: colours must be 3,x,y: got (@colordims)" if @colordims != 3 or $colordims[0] != 3;
266271
PDL::barf "Lattice: colours' x,y must equal points: got colour=(@colordims) points=($x,$y)" if $colordims[1] != $x or $colordims[2] != $y;
267-
$this->add_object(PDL::Graphics::TriD::Triangles->new($points->clump(1..2), $faceidx, $colors->clump(1..$colors->ndims-1), \%less));
272+
my $tc = PDL->zeroes(PDL::float, 2, @colordims[1,2]);
273+
$tc->slice('(0)') .= $tc->slice('(0)')->xlinvals(0,1); # should be inplace but PDL bugfix not yet released XXX
274+
$tc->slice('(1)') .= $tc->slice('(1)')->ylinvals(0,1);
275+
$this->add_object(PDL::Graphics::TriD::Triangles->new($points->clump(1..2), $faceidx, \[$colors, $tc->clump(1..2)], \%less));
268276
}
269277
if ($shading == 0 or $options->{Lines}) {
270278
my $lcolors = $shading ? $this->cdummies(PDL::float(0,0,0),$points) : $colors;

0 commit comments

Comments
 (0)