Skip to content

Commit 35f2d54

Browse files
committed
move common gdraw code to togl
1 parent 980f546 commit 35f2d54

File tree

1 file changed

+129
-194
lines changed
  • lib/PDL/Graphics/TriD

1 file changed

+129
-194
lines changed

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 129 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -138,74 +138,50 @@ sub PDL::Graphics::TriD::Quaternion::togl {
138138
##################################
139139
# Graph Objects
140140

141-
# (this,points)
142141
sub PDL::Graphics::TriD::GObject::togl {
143-
print "togl $_[0]\n" if $PDL::Graphics::TriD::verbose;
144-
$_[0]->gdraw($_[1] // $_[0]->{Points});
142+
my ($this, $points) = @_;
143+
print "togl $this\n" if $PDL::Graphics::TriD::verbose;
144+
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
145+
$this->glOptions;
146+
eval {
147+
$this->gdraw($points // $this->{Points});
148+
};
149+
{ local $@; glPopAttrib(); }
150+
die if $@;
145151
}
146152

147153
sub PDL::Graphics::TriD::Points::gdraw {
148-
my($this,$points) = @_;
149-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
150-
$this->glOptions;
151-
glDisable(GL_LIGHTING);
152-
eval {
153-
PDL::gl_points_col($points,$this->{Colors});
154-
};
155-
{ local $@; glPopAttrib(); }
156-
die if $@;
154+
my($this,$points) = @_;
155+
glDisable(GL_LIGHTING);
156+
PDL::gl_points_col($points,$this->{Colors});
157157
}
158158

159159
sub PDL::Graphics::TriD::Spheres::gdraw {
160160
my($this,$points) = @_;
161-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
162-
$this->glOptions;
163161
glEnable(GL_LIGHTING);
164162
glShadeModel(GL_SMOOTH);
165-
eval {
166-
PDL::gl_spheres($points, 0.025, 15, 15);
167-
};
168-
{ local $@; glPopAttrib(); }
169-
die if $@;
163+
PDL::gl_spheres($points, 0.025, 15, 15);
170164
}
171165

172166
sub PDL::Graphics::TriD::Lattice::gdraw {
173-
my($this,$points) = @_;
174-
barf "Need 3D points AND colours"
175-
if grep $_->ndims < 3, $points, $this->{Colors};
176-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
177-
$this->glOptions;
178-
glDisable(GL_LIGHTING);
179-
eval {
180-
PDL::gl_line_strip_col($points,$this->{Colors});
181-
PDL::gl_line_strip_col($points->xchg(1,2),$this->{Colors}->xchg(1,2));
182-
};
183-
{ local $@; glPopAttrib(); }
184-
die if $@;
167+
my($this,$points) = @_;
168+
barf "Need 3D points AND colours"
169+
if grep $_->ndims < 3, $points, $this->{Colors};
170+
glDisable(GL_LIGHTING);
171+
PDL::gl_line_strip_col($points,$this->{Colors});
172+
PDL::gl_line_strip_col($points->xchg(1,2),$this->{Colors}->xchg(1,2));
185173
}
186174

187175
sub PDL::Graphics::TriD::LineStrip::gdraw {
188-
my($this,$points) = @_;
189-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
190-
$this->glOptions;
191-
glDisable(GL_LIGHTING);
192-
eval {
193-
PDL::gl_line_strip_col($points,$this->{Colors});
194-
};
195-
{ local $@; glPopAttrib(); }
196-
die if $@;
176+
my($this,$points) = @_;
177+
glDisable(GL_LIGHTING);
178+
PDL::gl_line_strip_col($points,$this->{Colors});
197179
}
198180

199181
sub PDL::Graphics::TriD::Lines::gdraw {
200-
my($this,$points) = @_;
201-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
202-
$this->glOptions;
203-
glDisable(GL_LIGHTING);
204-
eval {
205-
PDL::gl_lines_col($points,$this->{Colors});
206-
};
207-
{ local $@; glPopAttrib(); }
208-
die if $@;
182+
my($this,$points) = @_;
183+
glDisable(GL_LIGHTING);
184+
PDL::gl_lines_col($points,$this->{Colors});
209185
}
210186

211187
sub PDL::Graphics::TriD::GObject::glOptions {
@@ -229,32 +205,26 @@ sub PDL::Graphics::TriD::GObject::_lattice_lines {
229205

230206
sub PDL::Graphics::TriD::Contours::gdraw {
231207
my ($this,$points) = @_;
232-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
233-
$this->glOptions;
234208
glDisable(GL_LIGHTING);
235-
eval {
236-
my $pi = $this->{PathIndex};
237-
my ($pcnt, $i, $thisind) = (0, 0, 0);
238-
for my $ie (grep defined, @{$this->{ContourPathIndexEnd}}) {
239-
my $colors = $this->{Colors};
240-
$colors = $colors->slice(":,($i)") if $colors->getndims==2;
241-
my $this_pi = $pi->slice("$pcnt:$ie");
242-
for ($this_pi->list) {
243-
PDL::gl_line_strip_col($points->slice(",$thisind:$_"), $colors);
244-
$thisind = $_ + 1;
245-
}
246-
$i++;
247-
$pcnt=$ie+1;
248-
}
249-
if (defined $this->{Labels}){
250-
glColor3d(1,1,1);
251-
my $seg = sprintf ":,%d:%d",$this->{Labels}[0],$this->{Labels}[1];
252-
PDL::Graphics::OpenGLQ::gl_texts($points->slice($seg),
253-
$this->{LabelStrings});
209+
my $pi = $this->{PathIndex};
210+
my ($pcnt, $i, $thisind) = (0, 0, 0);
211+
for my $ie (grep defined, @{$this->{ContourPathIndexEnd}}) {
212+
my $colors = $this->{Colors};
213+
$colors = $colors->slice(":,($i)") if $colors->getndims==2;
214+
my $this_pi = $pi->slice("$pcnt:$ie");
215+
for ($this_pi->list) {
216+
PDL::gl_line_strip_col($points->slice(",$thisind:$_"), $colors);
217+
$thisind = $_ + 1;
254218
}
255-
};
256-
{ local $@; glPopAttrib(); }
257-
die if $@;
219+
$i++;
220+
$pcnt=$ie+1;
221+
}
222+
if (defined $this->{Labels}){
223+
glColor3d(1,1,1);
224+
my $seg = sprintf ":,%d:%d",$this->{Labels}[0],$this->{Labels}[1];
225+
PDL::Graphics::OpenGLQ::gl_texts($points->slice($seg),
226+
$this->{LabelStrings});
227+
}
258228
}
259229

260230
my @sls1 = (
@@ -277,119 +247,89 @@ sub _lattice_slice {
277247
}
278248

279249
sub PDL::Graphics::TriD::SLattice::gdraw {
280-
my($this,$points) = @_;
281-
barf "Need 3D points"
282-
if grep $_->ndims < 3, $points;
283-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
284-
$this->glOptions;
285-
glDisable(GL_LIGHTING);
286-
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
287-
eval {
288-
_lattice_slice(\&PDL::gl_triangles, $points, $this->{Colors});
289-
$this->_lattice_lines($points) if $this->{Options}{Lines};
290-
};
291-
{ local $@; glPopAttrib(); }
292-
die if $@;
250+
my($this,$points) = @_;
251+
barf "Need 3D points"
252+
if grep $_->ndims < 3, $points;
253+
glDisable(GL_LIGHTING);
254+
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
255+
_lattice_slice(\&PDL::gl_triangles, $points, $this->{Colors});
256+
$this->_lattice_lines($points) if $this->{Options}{Lines};
293257
}
294258

295259
sub PDL::Graphics::TriD::SCLattice::gdraw {
296-
my($this,$points) = @_;
297-
barf "Need 3D points"
298-
if grep $_->ndims < 3, $points;
299-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
300-
$this->glOptions;
301-
glDisable(GL_LIGHTING);
302-
glShadeModel(GL_FLAT); # By-vertex doesn't make sense otherwise.
303-
eval {
304-
_lattice_slice(\&PDL::gl_triangles, $points, $this->{Colors});
305-
$this->_lattice_lines($points) if $this->{Options}{Lines};
306-
};
307-
{ local $@; glPopAttrib(); }
308-
die if $@;
260+
my($this,$points) = @_;
261+
barf "Need 3D points"
262+
if grep $_->ndims < 3, $points;
263+
glDisable(GL_LIGHTING);
264+
glShadeModel(GL_FLAT); # By-vertex doesn't make sense otherwise.
265+
_lattice_slice(\&PDL::gl_triangles, $points, $this->{Colors});
266+
$this->_lattice_lines($points) if $this->{Options}{Lines};
309267
}
310268

311269
sub PDL::Graphics::TriD::SLattice_S::gdraw {
312270
my($this,$points) = @_;
313271
barf "Need 3D points"
314272
if grep $_->ndims < 3, $points;
315-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
316-
$this->glOptions;
317273
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
318-
eval {
319-
my $f = 'PDL::gl_triangles_';
320-
$f .= 'w' if $this->{Options}{Smooth};
321-
$f .= 'n_mat';
322-
{ no strict 'refs'; $f = \&$f; }
323-
my @pdls = $points;
324-
push @pdls, $this->{Normals} if $this->{Options}{Smooth};
325-
push @pdls, $this->{Colors};
326-
_lattice_slice($f, @pdls);
327-
$this->_lattice_lines($points) if $this->{Options}{Lines};
328-
if ($this->{Options}{ShowNormals}) {
329-
die "No normals to show!" if !defined $this->{Normals};
330-
my $arrows = $points->append($points + $this->{Normals}*0.1)->splitdim(0,3);
331-
glDisable(GL_LIGHTING);
332-
glColor3d(1,1,1);
333-
PDL::Graphics::OpenGLQ::gl_arrows($arrows, 0, 1, 0.5, 0.02);
334-
}
335-
};
336-
{ local $@; glPopAttrib(); }
337-
die if $@;
274+
my $f = 'PDL::gl_triangles_';
275+
$f .= 'w' if $this->{Options}{Smooth};
276+
$f .= 'n_mat';
277+
{ no strict 'refs'; $f = \&$f; }
278+
my @pdls = $points;
279+
push @pdls, $this->{Normals} if $this->{Options}{Smooth};
280+
push @pdls, $this->{Colors};
281+
_lattice_slice($f, @pdls);
282+
$this->_lattice_lines($points) if $this->{Options}{Lines};
283+
if ($this->{Options}{ShowNormals}) {
284+
die "No normals to show!" if !defined $this->{Normals};
285+
my $arrows = $points->append($points + $this->{Normals}*0.1)->splitdim(0,3);
286+
glDisable(GL_LIGHTING);
287+
glColor3d(1,1,1);
288+
PDL::Graphics::OpenGLQ::gl_arrows($arrows, 0, 1, 0.5, 0.02);
289+
}
338290
}
339291

340292
sub PDL::Graphics::TriD::STrigrid_S::gdraw {
341293
my($this,$points) = @_;
342294
my $faces = $points->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
343-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
344-
$this->glOptions;
345-
eval {
346-
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
347-
my @sls = (":,(0)",":,(1)",":,(2)");
348-
my $idx = [0,1,2,0]; # for lines, below
349-
if ($this->{Options}{Smooth}) {
350-
my $tmpn=$this->{Normals}->dice_axis(1,$this->{Faceidx}->flat)
351-
->splitdim(1,$this->{Faceidx}->dim(0));
352-
PDL::gl_triangles_wn_mat(map $_->mv(1,-1)->dog, $faces, $tmpn, $this->{Colors});
353-
if ($this->{Options}{ShowNormals}) {
354-
my $arrows = $points->append($points + $this->{Normals}*0.1)->splitdim(0,3);
355-
glDisable(GL_LIGHTING);
356-
glColor3d(1,1,1);
357-
PDL::Graphics::OpenGLQ::gl_arrows($arrows, 0, 1, 0.5, 0.02);
358-
my $facecentres = $faces->transpose->avgover;
359-
my $facearrows = $facecentres->append($facecentres + $this->{FaceNormals}*0.1)->splitdim(0,3);
360-
glColor3d(0.5,0.5,0.5);
361-
PDL::Graphics::OpenGLQ::gl_arrows($facearrows, 0, 1, 0.5, 0.02);
362-
}
363-
} else {
364-
PDL::gl_triangles_n_mat(map $_->mv(1,-1)->dog, $faces, $this->{Colors});
365-
}
366-
if ($this->{Options}{Lines}) {
295+
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
296+
my @sls = (":,(0)",":,(1)",":,(2)");
297+
my $idx = [0,1,2,0]; # for lines, below
298+
if ($this->{Options}{Smooth}) {
299+
my $tmpn=$this->{Normals}->dice_axis(1,$this->{Faceidx}->flat)
300+
->splitdim(1,$this->{Faceidx}->dim(0));
301+
PDL::gl_triangles_wn_mat(map $_->mv(1,-1)->dog, $faces, $tmpn, $this->{Colors});
302+
if ($this->{Options}{ShowNormals}) {
303+
my $arrows = $points->append($points + $this->{Normals}*0.1)->splitdim(0,3);
367304
glDisable(GL_LIGHTING);
368-
glColor3f(0,0,0);
369-
PDL::gl_lines_nc($this->{Faces}->dice_axis(1,$idx));
305+
glColor3d(1,1,1);
306+
PDL::Graphics::OpenGLQ::gl_arrows($arrows, 0, 1, 0.5, 0.02);
307+
my $facecentres = $faces->transpose->avgover;
308+
my $facearrows = $facecentres->append($facecentres + $this->{FaceNormals}*0.1)->splitdim(0,3);
309+
glColor3d(0.5,0.5,0.5);
310+
PDL::Graphics::OpenGLQ::gl_arrows($facearrows, 0, 1, 0.5, 0.02);
370311
}
371-
};
372-
{ local $@; glPopAttrib(); }
373-
die if $@;
312+
} else {
313+
PDL::gl_triangles_n_mat(map $_->mv(1,-1)->dog, $faces, $this->{Colors});
314+
}
315+
if ($this->{Options}{Lines}) {
316+
glDisable(GL_LIGHTING);
317+
glColor3f(0,0,0);
318+
PDL::gl_lines_nc($this->{Faces}->dice_axis(1,$idx));
319+
}
374320
}
375321

376322
sub PDL::Graphics::TriD::STrigrid::gdraw {
377323
my($this,$points) = @_;
378324
my $faces = $points->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
379325
# faces is 3D pdl slices of points, giving cart coords of face verts
380-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
381-
$this->glOptions;
382-
eval {
383-
glDisable(GL_LIGHTING);
384-
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
385-
PDL::gl_triangles(map $_->mv(1,-1)->dog, $faces, $this->{Colors});
386-
if ($this->{Options}{Lines}) {
387-
glColor3f(0,0,0);
388-
PDL::gl_lines_nc($faces->dice_axis(1, [0,1,2,0]));
389-
}
390-
};
391-
{ local $@; glPopAttrib(); }
392-
die if $@;
326+
glDisable(GL_LIGHTING);
327+
glShadeModel(GL_SMOOTH); # By-vertex doesn't make sense otherwise.
328+
PDL::gl_triangles(map $_->mv(1,-1)->dog, $faces, $this->{Colors});
329+
if ($this->{Options}{Lines}) {
330+
glColor3f(0,0,0);
331+
PDL::gl_lines_nc($faces->dice_axis(1, [0,1,2,0]));
332+
}
393333
}
394334

395335
##################################
@@ -409,37 +349,32 @@ sub PDL::Graphics::TriD::Image::togl {
409349

410350
# The quick method is to use texturing for the good effect.
411351
sub PDL::Graphics::TriD::Image::gdraw {
412-
my($this,$vert) = @_;
413-
my ($p,$xd,$yd,$txd,$tyd) = $this->flatten(1); # do binary alignment
414-
if(!defined $vert) {$vert = $this->{Points}}
415-
barf "Need 3,4 vert"
416-
if grep $_->dim(1) < 4 || $_->dim(0) != 3, $vert;
417-
glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT);
418-
glColor3d(1,1,1);
419-
glTexImage2D_s(GL_TEXTURE_2D, 0, GL_RGB, $txd, $tyd, 0, GL_RGB, GL_FLOAT, $p->get_dataref());
420-
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
421-
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
422-
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
423-
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
424-
glDisable(GL_LIGHTING);
425-
glNormal3d(0,0,1);
426-
glEnable(GL_TEXTURE_2D);
427-
glBegin(GL_QUADS);
428-
eval {
429-
my @texvert = (
430-
[0,0],
431-
[$xd/$txd, 0],
432-
[$xd/$txd, $yd/$tyd],
433-
[0, $yd/$tyd]
434-
);
435-
for(0..3) {
436-
glTexCoord2f(@{$texvert[$_]});
437-
glVertex3f($vert->slice(":,($_)")->list);
438-
}
439-
};
440-
glEnd();
441-
{ local $@; glEnd(); glPopAttrib(); }
442-
die if $@;
352+
my($this,$vert) = @_;
353+
my ($p,$xd,$yd,$txd,$tyd) = $this->flatten(1); # do binary alignment
354+
if(!defined $vert) {$vert = $this->{Points}}
355+
barf "Need 3,4 vert"
356+
if grep $_->dim(1) < 4 || $_->dim(0) != 3, $vert;
357+
glColor3d(1,1,1);
358+
glTexImage2D_s(GL_TEXTURE_2D, 0, GL_RGB, $txd, $tyd, 0, GL_RGB, GL_FLOAT, $p->get_dataref());
359+
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
360+
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
361+
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
362+
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
363+
glDisable(GL_LIGHTING);
364+
glNormal3d(0,0,1);
365+
glEnable(GL_TEXTURE_2D);
366+
glBegin(GL_QUADS);
367+
my @texvert = (
368+
[0,0],
369+
[$xd/$txd, 0],
370+
[$xd/$txd, $yd/$tyd],
371+
[0, $yd/$tyd]
372+
);
373+
for(0..3) {
374+
glTexCoord2f(@{$texvert[$_]});
375+
glVertex3f($vert->slice(":,($_)")->list);
376+
}
377+
glEnd();
443378
}
444379

445380
sub PDL::Graphics::TriD::SimpleController::togl {

0 commit comments

Comments
 (0)