3DWorld/Mesh.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« 3DWorld/OpenGL.pm ↑ Summary 3DWorld/Texture.pm »
1 package SDL::Tutorial::3DWorld::Mesh;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use OpenGL ();
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use OpenGL::List ();
8 use List::MoreUtils ();
9 use SDL::Tutorial::3DWorld::OpenGL ();
10 use SDL::Tutorial::3DWorld::Material ();
11  
12 our $VERSION = '0.33';
13  
14 # Detect support for Vertex Buffer
15  
16  
17  
18 ######################################################################
19 # Constructor
20  
21 sub new {
22 my $class = shift;
23 my $self = bless {
24 material => [
25 # We put in a default base material both so
26 # that mesh without any materials won't end
27 # up being rendered manually, and so that file
28 # formats that describe material changes in delta
29 # form have a base to change from.
30 SDL::Tutorial::3DWorld::Material->new,
31 ],
32 vertex => [ undef ],
33 normal => [ undef ],
34 uv => [ undef ],
35 face => [ ],
36 box => [ ],
37 }, $class;
38 return $self;
39 }
40  
41 sub box {
42 @{$_[0]->{box}};
43 }
44  
45 sub max_vertex {
46 my $self = shift;
47 my $vertex = $self->{vertex};
48 return $#$vertex;
49 }
50  
51  
52  
53  
54  
55 ######################################################################
56 # Material Definition
57  
58 # Add a new material either as full material object or a set of
59 # parameters that act as a delta to the previous material.
60 # Return the material id of the new material.
61 sub add_material {
62 my $self = shift;
63 my $array = $self->{material};
64 my $current = $array->[-1];
65  
66 # Save the material directly if passed an object
67 if ( Params::Util::_INSTANCE($_[0], 'SDL::Tutorial::3DWorld::Material') ) {
68 my $material = shift;
69 push @$array, $material;
70 return $#$array;
71 }
72  
73 # Apply the provided changes
74 my %options = @_;
75 my $material = $current->clone;
76 if ( exists $options{color} ) {
77 $material->set_color( delete $options{color} );
78 }
79 if ( exists $options{texture} ) {
80 $material->set_texture( delete $options{texture} );
81 }
82 if ( exists $options{ambient} ) {
83 $material->set_ambient( delete $options{ambient} );
84 }
85 if ( exists $options{diffuse} ) {
86 $material->set_diffuse( delete $options{diffuse} );
87 }
88 if ( exists $options{opacity} ) {
89 $material->set_opacity( delete $options{opacity} );
90 }
91 if ( %options ) {
92 die "One or more unsupported material options";
93 }
94  
95 push @$array, $material;
96 return $#$array;
97 }
98  
99  
100  
101  
102  
103 ######################################################################
104 # Geometry Assembly
105  
106 sub add_all {
107 my $self = shift;
108 my $i = scalar @{ $self->{vertex} };
109 my @v = @{$_[0]};
110 $self->{vertex}->[$i] = \@v;
111 $self->{uv}->[$i] = $_[1];
112 $self->{normal}->[$i] = $_[2];
113  
114 # Update the bounding box
115 my $box = $self->{box};
116 unless ( @$box ) {
117 @$box = ( @v, @v );
118 return;
119 }
120 if ( $v[0] < $box->[0] ) {
121 $box->[0] = $v[0];
122 } elsif ( $v[0] > $box->[3] ) {
123 $box->[3] = $v[0];
124 }
125 if ( $v[1] < $box->[1] ) {
126 $box->[1] = $v[1];
127 } elsif ( $v[1] > $box->[4] ) {
128 $box->[4] = $v[1];
129 }
130 if ( $v[2] < $box->[2] ) {
131 $box->[2] = $v[2];
132 } elsif ( $v[2] > $box->[5] ) {
133 $box->[5] = $v[2];
134 }
135 return;
136 }
137  
138 sub add_vertex {
139 my $self = shift;
140 push @{ $self->{vertex} }, [ @_ ];
141  
142 # Update the bounding box
143 my $box = $self->{box};
144 unless ( @$box ) {
145 @$box = ( @_, @_ );
146 return;
147 }
148 if ( $_[0] < $box->[0] ) {
149 $box->[0] = $_[0];
150 } elsif ( $_[0] > $box->[3] ) {
151 $box->[3] = $_[0];
152 }
153 if ( $_[1] < $box->[1] ) {
154 $box->[1] = $_[1];
155 } elsif ( $_[1] > $box->[4] ) {
156 $box->[4] = $_[1];
157 }
158 if ( $_[2] < $box->[2] ) {
159 $box->[2] = $_[2];
160 } elsif ( $_[2] > $box->[5] ) {
161 $box->[5] = $_[2];
162 }
163 return;
164 }
165  
166 # Add an explicit normal
167 # The fourth element indicates the normal is explicit, final, and should be
168 # excluded from automatic normal calculations.
169 sub add_normal {
170 push @{ shift->{normal} }, [ @_, 1 ];
171 }
172  
173 sub add_uv {
174 push @{ shift->{uv} }, [ @_ ];
175 }
176  
177 sub add_triangle {
178 my $self = shift;
179 my $material = $self->{material};
180 my $vertex = $self->{vertex};
181 my $normal = $self->{normal};
182  
183 # We get an index set of up to ten things
184 # - One material index
185 # - Three vertex index
186 # - Three optional normal index
187 # - Three optional uv index
188 my $M = $material->[$_[0]] or die "No material $_[0]";
189 my $V0 = $vertex->[$_[1]] or die "No vertex $_[1]";
190 my $V1 = $vertex->[$_[2]] or die "No vertex $_[2]";
191 my $V2 = $vertex->[$_[3]] or die "No vertex $_[3]";
192 my $N0 = $normal->[$_[7] || 0];
193 my $N1 = $normal->[$_[8] || 0];
194 my $N2 = $normal->[$_[9] || 0];
195  
196 # Where a face has no normal defined, use one for the vertex if it
197 # exists, or auto-instantiate a normal to match the vertex if one
198 # does not exist already.
199 $N0 = $normal->[$_[1]] unless $N0;
200 $N1 = $normal->[$_[2]] unless $N1;
201 $N2 = $normal->[$_[3]] unless $N2;
202 $N0 = $normal->[$_[1]] = [ 0, 0, 0 ] unless $N0;
203 $N1 = $normal->[$_[2]] = [ 0, 0, 0 ] unless $N1;
204 $N2 = $normal->[$_[3]] = [ 0, 0, 0 ] unless $N2;
205  
206 # Looks good enough, save the face
207 push @{ $self->{face} }, [ 3, @_ ];
208  
209 # Shortcut if all the normals are final and we don't need to do
210 # automatic normal calculations.
211 if ( $N0->[3] and $N1->[3] and $N2->[3] ) {
212 return 1;
213 }
214  
215 # Find vectors for two sides
216 my $xa = $V0->[0] - $V1->[0];
217 my $ya = $V0->[1] - $V1->[1];
218 my $za = $V0->[2] - $V1->[2];
219 my $xb = $V1->[0] - $V2->[0];
220 my $yb = $V1->[1] - $V2->[1];
221 my $zb = $V1->[2] - $V2->[2];
222  
223 # Calculate the cross product vector
224 my $xn = ($ya * $zb) - ($za * $yb);
225 my $yn = ($za * $xb) - ($xa * $zb);
226 my $zn = ($xa * $yb) - ($ya * $xb);
227  
228 # Add the non-sqrt'ed cross product to each non-final vector so
229 # that vertex normals are averaged in proportion to face sizes.
230 # This is a recommendation seen on a demo scene tutorial.
231 unless ( $N0->[3] ) {
232 $N0->[0] += $xn;
233 $N0->[1] += $yn;
234 $N0->[2] += $zn;
235 }
236 unless ( $N1->[3] ) {
237 $N1->[0] += $xn;
238 $N1->[1] += $yn;
239 $N1->[2] += $zn;
240 }
241 unless ( $N2->[3] ) {
242 $N2->[0] += $xn;
243 $N2->[1] += $yn;
244 $N2->[2] += $zn;
245 }
246 return;
247 }
248  
249 sub add_quad {
250 my $self = shift;
251 my $material = $self->{material};
252 my $vertex = $self->{vertex};
253 my $normal = $self->{normal};
254  
255 # We get an index set of up to thirteen things
256 # - One material index
257 # - Four vertex index
258 # - Four optional normal index
259 # - Four optional uv index
260 my $M = $material->[$_[0]] or die "No material $_[0]";
261 my $V0 = $vertex->[$_[1]] or die "No vertex $_[1]";
262 my $V1 = $vertex->[$_[2]] or die "No vertex $_[2]";
263 my $V2 = $vertex->[$_[3]] or die "No vertex $_[3]";
264 my $V3 = $vertex->[$_[4]] or die "No vertex $_[4]";
265 my $N0 = $normal->[$_[9] || 0];
266 my $N1 = $normal->[$_[10] || 0];
267 my $N2 = $normal->[$_[11] || 0];
268 my $N3 = $normal->[$_[12] || 0];
269  
270 # Where a face has no normal defined, use one for the vertex if it
271 # exists, or auto-instantiate a normal to match the vertex if one
272 # does not exist already.
273 $N0 = $normal->[$_[1]] unless $N0;
274 $N1 = $normal->[$_[2]] unless $N1;
275 $N2 = $normal->[$_[3]] unless $N2;
276 $N3 = $normal->[$_[4]] unless $N3;
277 $N0 = $normal->[$_[1]] = [ 0, 0, 0 ] unless $N0;
278 $N1 = $normal->[$_[2]] = [ 0, 0, 0 ] unless $N1;
279 $N2 = $normal->[$_[3]] = [ 0, 0, 0 ] unless $N2;
280 $N3 = $normal->[$_[4]] = [ 0, 0, 0 ] unless $N3;
281  
282 # Looks good enough, save the face
283 push @{ $self->{face} }, [ 4, @_ ];
284  
285 # Shortcut if all the normals are final and we don't need to do
286 # automatic normal calculations.
287 if ( $N0->[3] and $N1->[3] and $N2->[3] and $N3->[3] ) {
288 return 1;
289 }
290  
291 # Find vectors for two sides
292 my $xa = $V0->[0] - $V1->[0];
293 my $ya = $V0->[1] - $V1->[1];
294 my $za = $V0->[2] - $V1->[2];
295 my $xb = $V1->[0] - $V2->[0];
296 my $yb = $V1->[1] - $V2->[1];
297 my $zb = $V1->[2] - $V2->[2];
298  
299 # Calculate the cross product vector
300 my $xn = ($ya * $zb) - ($za * $yb);
301 my $yn = ($za * $xb) - ($xa * $zb);
302 my $zn = ($xa * $yb) - ($ya * $xb);
303  
304 # Add the non-sqrt'ed cross product to each non-final vector so
305 # that vertex normals are averaged in proportion to face sizes.
306 # This is a recommendation seen on a demo scene tutorial.
307 unless ( $N0->[3] ) {
308 $N0->[0] += $xn;
309 $N0->[1] += $yn;
310 $N0->[2] += $zn;
311 }
312 unless ( $N1->[3] ) {
313 $N1->[0] += $xn;
314 $N1->[1] += $yn;
315 $N1->[2] += $zn;
316 }
317 unless ( $N2->[3] ) {
318 $N2->[0] += $xn;
319 $N2->[1] += $yn;
320 $N2->[2] += $zn;
321 }
322 unless ( $N3->[3] ) {
323 $N3->[0] += $xn;
324 $N3->[1] += $yn;
325 $N3->[2] += $zn;
326 }
327  
328 return;
329 }
330  
331  
332  
333  
334  
335 ######################################################################
336 # Engine Methods
337  
338 # Even through OpenGL can do surface vector normalisation itself, it is
339 # still better that we do it slower and once here than faster and many
340 # times in hardware.
341 sub init {
342 my $self = shift;
343  
344 # Normalise the surface vectors (reduce to 1 unit length)
345 foreach my $n ( @{$self->{normal}} ) {
346 # In some situations (for example if there are vectors not
347 # used by faces and we are doing implicit surface normals)
348 # the normal array might have null entries.
349 next unless $n;
350  
351 # Assume that any explicit surface vectors are normalised
352 if ( $n->[3] ) {
353 delete $n->[3];
354 next;
355 }
356  
357 # Do the normalisation
358 my $l = sqrt( ($n->[0] ** 2) + ($n->[1] ** 2) + ($n->[2] ** 2) ) || 1;
359 $n->[0] /= $l;
360 $n->[1] /= $l;
361 $n->[2] /= $l;
362 }
363  
364 # Initialise the subset of materials actually used by the faces
365 my %seen = ();
366 foreach my $face ( @{$self->{face}} ) {
367 next unless not $seen{$face->[1]}++;
368 $self->{material}->[$face->[1]]->init;
369 }
370  
371 return $self->{init} = 1;
372 }
373  
374 sub display {
375 my $self = shift;
376  
377 # Auto-initialise
378 $self->{init} or $self->init;
379  
380 # Set up and apply defaults
381 my $material = $self->{material};
382 my $vertex = $self->{vertex};
383 my $normal = $self->{normal};
384 my $uv = $self->{uv};
385 my $face = $self->{face};
386 my $begin = 0;
387  
388 # Initialise the material to the material of the first face.
389 # We do this in full here so that initial material setup (which
390 # is being done from an untrusted material state) is not impacted
391 # by any future material delta optimisations below in future.
392 my $current = $face->[0]->[1];
393 $material->[$current]->display;
394  
395 # Render the faces
396 foreach my $f ( @$face ) {
397 my $t = $f->[0];
398 my $m = $f->[1];
399  
400 # End previous drawing sequence if needed
401 unless ( $t == $begin and $m == $current ) {
402 OpenGL::glEnd() if $begin;
403 $begin = 0;
404 }
405  
406 # Switch materials
407 unless ( $m == $current ) {
408 $material->[$m]->display;
409 $current = $m;
410 }
411  
412 if ( $t == 4 ) {
413 # Quad
414 my $v0 = $vertex->[$f->[2]];
415 my $v1 = $vertex->[$f->[3]];
416 my $v2 = $vertex->[$f->[4]];
417 my $v3 = $vertex->[$f->[5]];
418 my $t0 = $uv->[$f->[6] || 0];
419 my $t1 = $uv->[$f->[7] || 0];
420 my $t2 = $uv->[$f->[8] || 0];
421 my $t3 = $uv->[$f->[9] || 0];
422 my $n0 = $normal->[$f->[10] || $f->[2]];
423 my $n1 = $normal->[$f->[11] || $f->[3]];
424 my $n2 = $normal->[$f->[12] || $f->[4]];
425 my $n3 = $normal->[$f->[13] || $f->[5]];
426  
427 # Start the new geometry sequence
428 unless ( $begin ) {
429 OpenGL::glBegin( OpenGL::GL_QUADS );
430 $begin = $t;
431 }
432  
433 # Draw the quad
434 OpenGL::glTexCoord2f( @$t0 ) if $t0;
435 OpenGL::glNormal3f( @$n0 );
436 OpenGL::glVertex3f( @$v0 );
437 OpenGL::glTexCoord2f( @$t1 ) if $t1;
438 OpenGL::glNormal3f( @$n1 );
439 OpenGL::glVertex3f( @$v1 );
440 OpenGL::glTexCoord2f( @$t2 ) if $t2;
441 OpenGL::glNormal3f( @$n2 );
442 OpenGL::glVertex3f( @$v2 );
443 OpenGL::glTexCoord2f( @$t3 ) if $t3;
444 OpenGL::glNormal3f( @$n3 );
445 OpenGL::glVertex3f( @$v3 );
446  
447 # We only support triangles and quads
448 } else {
449 # Triangle
450 my $v0 = $vertex->[$f->[2]];
451 my $v1 = $vertex->[$f->[3]];
452 my $v2 = $vertex->[$f->[4]];
453 my $t0 = $uv->[$f->[5] || 0];
454 my $t1 = $uv->[$f->[6] || 0];
455 my $t2 = $uv->[$f->[7] || 0];
456 my $n0 = $normal->[$f->[8] || $f->[2]];
457 my $n1 = $normal->[$f->[9] || $f->[3]];
458 my $n2 = $normal->[$f->[10] || $f->[4]];
459  
460 # Start the new geometry sequence
461 unless ( $begin ) {
462 OpenGL::glBegin( OpenGL::GL_TRIANGLES );
463 $begin = 3;
464 }
465  
466 # Draw the triangle
467 OpenGL::glTexCoord2f( @$t0 ) if $t0;
468 OpenGL::glNormal3f( @$n0 );
469 OpenGL::glVertex3f( @$v0 );
470 OpenGL::glTexCoord2f( @$t1 ) if $t1;
471 OpenGL::glNormal3f( @$n1 );
472 OpenGL::glVertex3f( @$v1 );
473 OpenGL::glTexCoord2f( @$t2 ) if $t2;
474 OpenGL::glNormal3f( @$n2 );
475 OpenGL::glVertex3f( @$v2 );
476  
477 }
478 }
479  
480 # Clean up the final drawing mode
481 OpenGL::glEnd() if $begin;
482  
483 return 1;
484 }
485  
486 # Generate a display list for the mesh
487 sub as_list {
488 my $self = shift;
489 return OpenGL::List::glpList {
490 $self->display;
491 };
492 }
493  
494  
495  
496  
497  
498 ######################################################################
499 # Vertex Buffer Array Renderer
500  
501 sub as_oga {
502 my $self = shift;
503 my $vertex = $self->{vertex};
504 my $face = $self->{face};
505  
506 # Render the faces
507 my @voga = ();
508 foreach my $f ( @$face ) {
509 my $t = $f->[0];
510  
511 if ( $t == 4 ) {
512 # Quad
513 push @voga, (
514 @{$vertex->[$f->[2]]},
515 @{$vertex->[$f->[3]]},
516 @{$vertex->[$f->[4]]},
517 @{$vertex->[$f->[5]]},
518 );
519 }
520  
521 }
522  
523 return {
524 vertex => OpenGL::Array->new_list(
525 OpenGL::GL_FLOAT,
526 @voga,
527 ),
528 };
529 }
530  
531 1;
« 3DWorld/OpenGL.pm ↑ Summary 3DWorld/Texture.pm »