Skip to content

Commit c9ee108

Browse files
committed
Trigrid always pass normals if shading
1 parent 28752ea commit c9ee108

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,18 @@ sub PDL::Graphics::TriD::Trigrid::gdraw {
251251
my($this,$points) = @_;
252252
my $faces = $points->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
253253
my $colours = $this->{Colors}->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
254+
my $options = $this->{Options};
254255
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
255256
my $f = 'PDL::gl_triangles';
256-
my $send_normals = $this->{Options}{Smooth} && $this->{Options}{Shading};
257-
$f .= '_' . ($send_normals ? 'w' : '') . 'n' if $this->{Options}{Shading};
257+
$f .= '_wn' if $options->{Shading};
258258
{ no strict 'refs'; $f = \&$f; }
259-
my $tmpn = $send_normals
259+
my $tmpn = !$options->{Shading} ? undef : $options->{Smooth}
260260
? $this->{VertexNormals}->dice_axis(1,$this->{Faceidx}->flat)
261-
->splitdim(1,$this->{Faceidx}->dim(0)) : undef;
262-
if ($this->{Options}{Shading}) { glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); }
263-
$f->(map $_->mv(1,-1)->dog, $faces, $send_normals ? $tmpn : (), $colours);
264-
if ($this->{Options}{Shading}) { glDisable(GL_COLOR_MATERIAL); }
265-
if ($this->{Options}{ShowNormals}) {
261+
->splitdim(1,$this->{Faceidx}->dim(0)) : $this->{FaceNormals}->dummy(1,3);
262+
if ($options->{Shading}) { glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); }
263+
$f->(map $_->mv(1,-1)->dog, $faces, $options->{Shading} ? $tmpn : (), $colours);
264+
if ($options->{Shading}) { glDisable(GL_COLOR_MATERIAL); }
265+
if ($options->{ShowNormals}) {
266266
die "No normals to show!" if !grep defined $this->{$_}, qw(FaceNormals VertexNormals);
267267
if (defined $this->{VertexNormals}) {
268268
my $arrows = $points->append($points + $this->{VertexNormals}*0.1)->splitdim(0,3);
@@ -278,7 +278,7 @@ sub PDL::Graphics::TriD::Trigrid::gdraw {
278278
PDL::Graphics::OpenGLQ::gl_arrows($facearrows, 0, 1, 0.5, 0.02);
279279
}
280280
}
281-
if ($this->{Options}{Lines}) {
281+
if ($options->{Lines}) {
282282
glColor3f(0,0,0);
283283
PDL::gl_lines_nc($faces->dice_axis(1,[0,1,2,0]));
284284
}

lib/PDL/Graphics/TriD/Objects.pm

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ sub new {
145145
my $this = $type->SUPER::new($points,$colors,$options);
146146
# faceidx is 2D pdl of indices into points for each face
147147
$this->{Faceidx} = $faceidx->ulong;
148-
$this->{VertexNormals} = $this->smoothn($points->dice_axis(1,$faceidx->flat)->splitdim(1,3)) if $this->{Options}{Smooth};
148+
$options = $this->{Options};
149+
if ($options->{Shading} or $options->{ShowNormals}) {
150+
my ($fn, $vn) = triangle_normals($this->{Points}, $faceidx);
151+
$this->{VertexNormals} = $vn if $options->{Smooth} or $options->{ShowNormals};
152+
$this->{FaceNormals} = $fn if !$options->{Smooth} or $options->{ShowNormals};
153+
}
149154
$this;
150155
}
151156
sub get_valid_options { +{
@@ -157,12 +162,6 @@ sub get_valid_options { +{
157162
Lighting => 1,
158163
}}
159164
sub cdummies { $_[1]->dummy(1,$_[2]->getdim(1)); }
160-
sub smoothn { my ($this, $faces) = @_;
161-
my ($points, $faceidx) = @$this{qw(Points Faceidx)};
162-
my ($fn, $vn) = triangle_normals($this->{Points}, $faceidx);
163-
$this->{FaceNormals} = $fn if $this->{Options}{ShowNormals};
164-
$vn;
165-
}
166165

167166
package PDL::Graphics::TriD::Lattice;
168167
use base qw/PDL::Graphics::TriD::GObject/;

0 commit comments

Comments
 (0)