curveVertex are wrong on a PGraphics

Hello to all,
I’m trying to put a PShape containing curveVertex on a PGraphics.
The image i’m getting is wrong and all the curveVertex are displayed as vertex instead … I have no clue of what is wrong.

Thank you for your help !

There is my code:

void setup() {
  size(800, 800,P3D);
  background(200);
  PShape h = createShape();
  h.beginShape();
  h.curveVertex(100, 0);
  h.curveVertex(25, 75);
  h.curveVertex(300, 0);
  h.curveVertex(400, 75);
  h.curveVertex(75, 400);
  h.curveVertex(50, 400);
  h.endShape(CLOSE);
  h.setStroke(true);
  shape(h);
  PGraphics pg = createGraphics(width,height);
  pg.beginDraw();
  pg.shape(h);
  pg.endDraw();
  image(pg,0,0);
}

The problem you are facing is that the createGraphics(int, int) method creates a PGraphics using the java2D renderer, which, from my testing, doesn’t handle curveVertices well. If you make your sketch itself use java2D instead of P3D, your shape will just not display. If you want a quick solution, make your PGraphics using P3D with createGraphics(width, height, P3D).

For further insight, keep reading:

I made a sketch that tests drawing your shape on every similar type of PGraphics to show the differences:

void setup() {
  size(1600, 800, P3D);
  background(200);
  PShape h = createShape();
  h.beginShape();
  h.curveVertex(100, 0);
  h.curveVertex(25, 75);
  h.curveVertex(300, 0);
  h.curveVertex(400, 75);
  h.curveVertex(75, 400);
  h.curveVertex(50, 400);
  h.endShape(CLOSE);
  //h.set3D(false); // if this line is run, P2D will draw the shape without error
  translate(-20, 0); // just so everything fits on screen
  shape(h);
  PGraphics pg = createGraphics(width,height);
  pg.beginDraw();
  pg.shape(h);
  pg.endDraw();
  image(pg,width * 0.25,0);
  PGraphics pg2d = createGraphics(width,height, P2D);
  pg2d.beginDraw();
  pg2d.shape(h);
  pg2d.endDraw();
  image(pg2d,width * 0.5,0);
  PGraphics pg3d = createGraphics(width,height, P3D);
  pg3d.beginDraw();
  pg3d.shape(h);
  pg3d.endDraw();
  image(pg3d,width * 0.75,0);
}

Something this sketch shows is that the PShape you’ve created is marked a 3D shape because you are using P3D. So, the P2D renderer catches this and prints an error in the console, saying that you shouldn’t use 3D shapes in 2D. You can force your shape to be 2D by doing h.set3D(false). After this, the shape is drawable in P3D and P2D. I wouldn’t trust doing that with actually 3D shapes, though.
Hopefully this clears things up.

2 Likes

What a great answer !!
What surprises me the most is that if I use the default render when I create the PShape, the PGraphics display nothing …
Thank you, it helps a lot.

1 Like