curveVertex and PShape

This might be silly, but i can't tell why this work works with pg.vertex but the same code doesn't work with pg.curveVertex

This works:

PShape pg = createShape();

translate(0, height);
scale(1, -1);
size(400, 400);


pg.beginShape();
pg.noFill();
for (int i=1; i<=400; i=i+10) {
  pg.vertex(i,sq(i*2/5)/10);
}
pg.endShape();
shape(pg);

This doesn't work:

PShape pg = createShape();

translate(0, height);
scale(1, -1);
size(400, 400);


pg.beginShape();
pg.noFill();
for (int i=1; i<=400; i=i+10) {
  pg.curveVertex(i,sq(i*2/5)/10);
}
pg.endShape();
shape(pg);

but, oddly enough, if you remove pg. it works

PShape pg = createShape();

translate(0, height);
scale(1, -1);
size(400, 400);


beginShape();
noFill();
for (int i=1; i<=400; i=i+10) {
  curveVertex(i,sq(i*2/5)/10);
}
endShape();
shape(pg);

Answers

  • edited September 2015

    The PShape with the pg.curveVertex() will show correctly if you choose P2D mode.

    size(400, 400, P2D);
    

    Don’t know why it did not appear on default mode (JAVA2D)? But I noticed that every examples of createShape() in the reference file are done in P2D mode to. I have tested in version 2.2.1 and only P2D is compiled correctly. In version 3+, I tested with the 3 modes : P2D, JAVA2D, FX2D. No problem to compiled, but very different rendering on each case. Noticed that FX2D is flipped upside down (which have already been reported).

    You can test it with this code :

    PShape s;
    
    void setup() {
      size(100, 100, P2D);
      //size(100, 100, JAVA2D);
      //size(100, 100, FX2D);
      background(200);
      s = createShape();
      s.beginShape(TRIANGLE_STRIP);
      s.noFill();
      s.vertex(30, 75);
      s.vertex(40, 20);
      s.vertex(50, 75);
      s.vertex(60, 20);
      s.vertex(70, 75);
      s.vertex(80, 20);
      s.vertex(90, 75);
      s.endShape();
    }
    
    void draw() {
      background(200);
      shape(s);
    }
    

    Just uncommented the command size() of the rendering mode you want to test (and commented the two others).

  • Thanks for you answer!

    Sadly my computer doesn't support P2D so i can only test with JAVA2D. I seen in your example you've used vertex. This was in fact your intent, to test PShape rendering in general?

    Here i can only render with the default mode, where i get the result from the middle image you've posted.

    I need to draw curveVertex in a PGraphics using default render. Does anybody knows why my code doesn't work? It works fine when not drawing the curves to a PGraphics.

  • edited September 2015

    @Henri
    Then if you don’t have access to P2D mode, you should check if you really need to create a shape. The conflict is due to the createShape() command. Remember that this methodology will be necessary if you later need to scale up your graphics or to get access to the vertices, (getVertex(), setVertex()), since your intent is to modified the structure of your shape.
    Why not test your code in default mode, and draw your shape in a PGraphics instead of a PShape? The code below works fine in every version of Processing, and in JAVA2D mode. Note that it still use a shape (beginShape(), endShape()), but not creating it.

    PGraphics pg = createGraphics(400,400);
    
    translate(0, height);
    scale(1, -1);
    size(400, 400);
    
    
    pg.beginDraw();
    pg.noFill();
    pg.beginShape();
    for (int i=1; i<=400; i=i+10) {
      pg.curveVertex(i,sq(i*2/5)/10);
    }
    pg.endShape();
    pg.endDraw();
    image(pg,0,0);
    
Sign In or Register to comment.