PMatrices Broken in Processing 2.1.1 on Windows?

I am trying to build a sketch that involves a lot re-orientation of points; I need to move and rotate some points and then measure those points against un-rotated, un-translated and un-scaled points. The most sensible and straightforward-seeming approach to me would be to do something like rotate/translate/scale to my destination, record the current matrix, and then multiply my points in my data structure to get in place 3D relationships. This, however, doesn't appear to work as advertised.

In theory, if I manipulate the matrix stack (with translations, rotations, and scaling), draw something, say an ellipse, record the current matrix, pop out of the stack (or otherwise reset the current matrix), and then apply my recorded matrix and draw an identical ellipse, they should draw on top of one another. This isn't what happens on my machine. Why?

Here is an example sketch that, using the above algorithm, should draw two ellipses on top of one another.

void setup(){
  size(100,100,P3D);  
}

void draw(){
  background(0);
  //draw first ellipse
  noFill();  stroke(255);
  pushMatrix();
    translate(width/2,height/2);
    rotateX(millis()/2000.f);
    PMatrix3D pMat = new PMatrix3D();
    getMatrix(pMat); //record current matrix
    ellipse(0,0,50,50);
  popMatrix();

  //draw second ellipse
  fill(255,0,0);  noStroke();
  pushMatrix();
    applyMatrix(pMat);
    ellipse(0,0,50,50);
  popMatrix();
}

This simple test fails on windows in the 64bit Processing 2.1.1, why?

Answers

  • Part of the issue is that translate(width/2,height/2); only applies to the first ellipse. Try this

    void setup(){
      size(100,100,P3D);  
    }
    
    
    void draw(){
    
      background(255);
    
      translate(width/2,height/2); //put outside push/pop - so both ellipses will now start from the centre of the screen
    
      //draw first ellipse
    
      noFill();  
    
      stroke(0);
    
    
      pushMatrix();
    
        rotateY(12);
    
        PMatrix3D pMat = new PMatrix3D();
    
        getMatrix(pMat); //record current matrix
    
        ellipse(0,0,50,50);
    
      popMatrix();
    
    
    
      //draw second ellipse
    
      fill(255,0,0);  noStroke();
    
      pushMatrix();
    
        applyMatrix(pMat);
    
        ellipse(0,0,50,50);
    
      popMatrix();
    
    }
    
Sign In or Register to comment.