processing4 icon indicating copy to clipboard operation
processing4 copied to clipboard

noStroke makes PShape.setTexture crash (P2D)

Open TidensBarn opened this issue 2 years ago • 1 comments

I had inexplicable crashes when using textures on PShapes that almost made me loose my will to live until I finally found the cause: If I create a PShape in "noStroke" state, setTexture will work the first time, but when I then try to set a different texture and redraw, it'll crash:

    PShape shape;
    
    void settings() {
      size(400, 400, P2D); 
    }
    
    void setup() {
    }
  
    void draw() {

      noStroke(); // Remove this, no crash

      shape = createShape(RECT, 0, 0, 200, 200);
      
      PImage p1 = loadImage("img1.png");
      shape.setTexture(p1);
      shape(shape);
      
      PImage p2 = loadImage("img2.png"); // p2 needs to be a different size from p1
      shape.setTexture(p2);
      shape(shape); // Crash!
    }

It's an ArrayIndexOutofBoundsException in PShapeOpenGL, line 843, because the field "firstLineVertex" happens to be -1... Here's a sketch with images: crash.zip Tested on Win 10, Processing 4.2

TidensBarn avatar Mar 27 '23 00:03 TidensBarn

It appears to me that the shape.setTexture(...) has some issues. As doing:

PShape shape;
PImage p1, p2;

void settings() {
  size(400, 400, P2D); 
}

void setup() {
    p1 = loadImage("img1.png");
    p2 = loadImage("img2.png"); // p2 needs to be a different size from p1
}
  
void draw() {

    noStroke(); // using nostroke

    shape = createShape(RECT, 0, 0, 200, 200);
      
    shape.setTexture(p1);
    shape(shape);
      
    shape = createShape(RECT, 0, 0, 200, 200); // doing this...

    shape.setTexture(p2);
    shape(shape); // No Crash!
}

works just fine. You'll have to translate(...) to see both the images though.

krishna2803 avatar Dec 16 '23 06:12 krishna2803