processing4 icon indicating copy to clipboard operation
processing4 copied to clipboard

Copying PGraphics to PImage using .get() or .copy() clears PGraphics object, if the renderer is P2D

Open agrshch opened this issue 1 year ago • 3 comments

Description

An attempt of copying information from PGraphics to PImage using get() or copy() clears the PGraphics object if P2D renderer is used. It makes impossible many things, for example using PGraphics (or its copy) as a texture for the shader.

Expected Behavior

successful copying, like with default renderer

Current Behavior

both PGraphics and PImage are empty.

Steps to Reproduce

PGraphics img1;

void setup() { size(300, 300, P2D);

img1 = createGraphics(200, 200, P2D);

img1.beginDraw(); img1.background(120); img1.endDraw();

PImage img2 = img1.get(); // if comment out this line — grey rectangle appears }

void draw() { background(255); image(img1, 100, 20); }

Your Environment

  • Processing version: 4.2
  • Operating System and OS version: Mac OS 10.15.7
  • Other information:

agrshch avatar Mar 02 '23 23:03 agrshch

Tested & confirmed this bug report on Windows 11, with both P2D & P3D modes on the main app & PGraphics.

Interestingly, if you move the PGraphics drawing into the draw() loop, this bug doesn't happen, like so:

PGraphics img1;
PImage img2;

void setup() {
  size(300, 300, P2D);
  img1 = createGraphics(200, 200, P2D);
}

void draw() {
  img1.beginDraw();
  img1.background(120);
  img1.endDraw();
  
  if(img2 == null) {
    PImage img2 = img1.get();
  }
  
  background(255);
  image(img1, 100, 20);
}

cacheflowe avatar Mar 16 '23 17:03 cacheflowe

I found a hack that seems to work around it. Include a dummy beginDraw() / endDraw() in setup before doing anything. Anything in the first begin/end gets wiped out, but later ones, in either setup() or draw(), persist.

PGraphics pg;

void setup() {
  size( 300, 300, P2D );
  pg = createGraphics( 200, 200, P2D );
  
  // ---- include to fix ----
  pg.beginDraw();
  pg.endDraw();
  // ------------------------
  pg.beginDraw();
  pg.background( 255 );
  pg.endDraw();
}

void draw() {
  pg.beginDraw();
  pg.line( 10, 10, 190, 190 );
  pg.endDraw();
  
  image( pg, 10, 10 );
}

scudly avatar Apr 02 '23 04:04 scudly

This looks related to what I investigated in #641. I just submitted a pull request.

mvaladas avatar Jun 05 '23 15:06 mvaladas