@@ -38,7 +38,7 @@ abstract public class PShape implements PConstants {
3838
3939 protected int kind ;
4040 //protected int drawMode;
41- protected PMatrix3D matrix ;
41+ protected PMatrix matrix ;
4242
4343 // setAxis -> .x and .y to move x and y coords of origin
4444 protected float x ;
@@ -199,6 +199,7 @@ public float getHeight() {
199199
200200 protected void pre (PGraphics g ) {
201201 if (matrix != null ) {
202+ /*
202203 boolean flat = g instanceof PGraphics2D;
203204
204205 g.pushMatrix();
@@ -211,6 +212,8 @@ protected void pre(PGraphics g) {
211212 matrix.m20, matrix.m21, matrix.m22, matrix.m23,
212213 matrix.m30, matrix.m31, matrix.m32, matrix.m33);
213214 }
215+ */
216+ g .applyMatrix (matrix );
214217 }
215218
216219 strokeSaved = g .stroke ;
@@ -394,11 +397,13 @@ protected void addName(String nom, PShape shape) {
394397
395398
396399 public void translate (float tx , float ty ) {
397- translate (tx , ty , 0 );
400+ checkMatrix (2 );
401+ matrix .translate (tx , ty );
398402 }
399403
404+
400405 public void translate (float tx , float ty , float tz ) {
401- checkMatrix ();
406+ checkMatrix (3 );
402407 matrix .translate (tx , ty , 0 );
403408 }
404409
@@ -407,20 +412,25 @@ public void rotateX(float angle) {
407412 rotate (angle , 1 , 0 , 0 );
408413 }
409414
415+
410416 public void rotateY (float angle ) {
411417 rotate (angle , 0 , 1 , 0 );
412418 }
413419
420+
414421 public void rotateZ (float angle ) {
415422 rotate (angle , 0 , 0 , 1 );
416423 }
417424
425+
418426 public void rotate (float angle ) {
419- rotateZ (angle );
427+ checkMatrix (2 ); // at least 2...
428+ matrix .rotate (angle );
420429 }
421430
431+
422432 public void rotate (float angle , float v0 , float v1 , float v2 ) {
423- checkMatrix ();
433+ checkMatrix (3 );
424434 matrix .rotate (angle , v0 , v1 , v2 );
425435 }
426436
@@ -429,17 +439,19 @@ public void rotate(float angle, float v0, float v1, float v2) {
429439
430440
431441 public void scale (float s ) {
432- scale (s , s , s );
442+ checkMatrix (2 ); // at least 2...
443+ matrix .scale (s );
433444 }
434445
435446
436447 public void scale (float sx , float sy ) {
437- scale (sx , sy , 1 );
448+ checkMatrix (2 );
449+ matrix .scale (sx , sy );
438450 }
439451
440452
441453 public void scale (float x , float y , float z ) {
442- checkMatrix ();
454+ checkMatrix (3 );
443455 matrix .scale (x , y , z );
444456 }
445457
@@ -448,24 +460,51 @@ public void scale(float x, float y, float z) {
448460
449461
450462 public void resetMatrix () {
463+ checkMatrix (2 );
464+ matrix .reset ();
451465 }
452466
453467
468+ public void applyMatrix (PMatrix source ) {
469+ if (source instanceof PMatrix2D ) {
470+ applyMatrix ((PMatrix2D ) source );
471+ } else if (source instanceof PMatrix3D ) {
472+ applyMatrix ((PMatrix3D ) source );
473+ }
474+ }
475+
476+
477+ public void applyMatrix (PMatrix2D source ) {
478+ applyMatrix (source .m00 , source .m01 , 0 , source .m02 ,
479+ source .m10 , source .m11 , 0 , source .m12 ,
480+ 0 , 0 , 1 , 0 ,
481+ 0 , 0 , 0 , 1 );
482+ }
483+
484+
454485 public void applyMatrix (float n00 , float n01 , float n02 ,
455486 float n10 , float n11 , float n12 ) {
456- checkMatrix ();
487+ checkMatrix (2 );
457488 matrix .apply (n00 , n01 , n02 , 0 ,
458489 n10 , n11 , n12 , 0 ,
459490 0 , 0 , 1 , 0 ,
460491 0 , 0 , 0 , 1 );
461492 }
462493
463494
495+ public void apply (PMatrix3D source ) {
496+ applyMatrix (source .m00 , source .m01 , source .m02 , source .m03 ,
497+ source .m10 , source .m11 , source .m12 , source .m13 ,
498+ source .m20 , source .m21 , source .m22 , source .m23 ,
499+ source .m30 , source .m31 , source .m32 , source .m33 );
500+ }
501+
502+
464503 public void applyMatrix (float n00 , float n01 , float n02 , float n03 ,
465504 float n10 , float n11 , float n12 , float n13 ,
466505 float n20 , float n21 , float n22 , float n23 ,
467506 float n30 , float n31 , float n32 , float n33 ) {
468- checkMatrix ();
507+ checkMatrix (3 );
469508 matrix .apply (n00 , n01 , n02 , n03 ,
470509 n10 , n11 , n12 , n13 ,
471510 n20 , n21 , n22 , n23 ,
@@ -476,9 +515,20 @@ public void applyMatrix(float n00, float n01, float n02, float n03,
476515 //
477516
478517
479- protected void checkMatrix () {
518+ /**
519+ * Make sure that the shape's matrix is 1) not null, and 2) has a matrix
520+ * that can handle <em>at least</em> the specified number of dimensions.
521+ */
522+ protected void checkMatrix (int dimensions ) {
480523 if (matrix == null ) {
481- matrix = new PMatrix3D ();
524+ if (dimensions == 2 ) {
525+ matrix = new PMatrix2D ();
526+ } else {
527+ matrix = new PMatrix3D ();
528+ }
529+ } else if (dimensions == 3 && (matrix instanceof PMatrix2D )) {
530+ // time for an upgrayedd for a double dose of my pimpin'
531+ matrix = new PMatrix3D (matrix );
482532 }
483533 }
484534
0 commit comments