PShape.resetMatrix() not resetting rotation in P2D

Hello everyone!
I have a quite weird resetMatrix bug on PShape. It works fine in the default renderer, but in P2D it seems to reset only the translation and not the rotation. So what I am seeing is a spinning quad, instead of a fixed one. If you are testing the code below, are you seeing the same difference?
Processing reference mentions that resetMatrix() is similar to OpenGL’s glLoadIdentity(), so is there an equivalent way to do this?

PShape s;
void setup(){
  size(300, 300, P2D); // works fine with default renderer
  s = createShape(QUAD, -75, -75, 75, -75, 75, 75, -75, 75);
}
void draw(){
  background(0);
  s.resetMatrix();
  s.rotate(0.01); // rotate is not reset, so it keeps adding up
  s.translate(30,10); // translate is reset properly
  shape(s, 150, 150);
}

:slight_smile:

Why are you calling rotate and translate on the shape and not directly? Changing the matrix in the PShape can be quite inefficient.

I am using a shape group, I did not include it here to simplify the problem. Each shape in the group needs to rotate and translate individually.

OK. Out of interest does this work with Processing 3.4? I know there was some changes to try and make matrix operations on shapes more efficient. What you’re doing actually performs worse than just drawing the shapes directly unfortunately.

1 Like

Actually I tried making about 10000 of those quad PShapes without the shape group and I got worse performances, frameRate dropped by half.

I haven’t tried reverting to 3.4, I am using 3.5.2. I would try it but my code needs to run on machines that do not belong to me but to a museum, the guy that runs the IT department barely lets me install external libraries :unamused:

I meant vs using quad(...) directly without PShape.

Purely an idea to see if this is a regression caused by recent change in behaviour.

1 Like

Oh ok, sorry I misunderstood! :slight_smile: I need an image texture on those quads, hence the shape. If I got it correctly texture() only works with shapes.

Well, you could use beginShape(QUADS) and calls to vertex(..).

The problem is that resetting matrices on a PShape causes performance issues which negates the benefit of the retained shape. This issue, and some changes that I wonder might be causing a difference between 3.4 and 3.5, are covered at https://github.com/processing/processing/issues/5685 and have been discussed in a few threads here.

Also see https://processing.org/examples/texturequad.html

1 Like

So I just tried that with 10000 quads and it is even slower. Thank you for suggesting it though.

I think I am going to stick with the original group shape and compute a difference angle for the rotation.

a really dirty workaround (that should never be used)

PShape s;
PMatrix2D matrix;

void setup(){
  size(300, 300, P2D); // works fine with default renderer
  s = createShape(QUAD, -75, -75, 75, -75, 75, 75, -75, 75);
  matrix = new PMatrix2D();
}
void draw(){
  background(0);
  
  matrix.reset();
  matrix.translate(30, 10);
  matrix.rotate(45);
  s.resetMatrix();
  s.applyMatrix(matrix);
  
  shape(s, 150, 150);
}

but don’t ask me why :stuck_out_tongue:

1 Like

@hotfooted Great! this solved the rotation issue without slowing down anything :star_struck:

Interesting, and unexpected! So I was going to suggest you test the Demos/Performance/DynamicParticles(Immediate|Retained) examples. On 3.4 the performance of the retained example is dramatically worse. But I just noticed that on 3.5 it isn’t.

So, then I tested your original code. On 3.4 it’s correct, on 3.5 it continually rotates. So it’s definitely a regression caused by the performance improvements in PShape matrix handling.

2 Likes