PGraphics Shape Layers

In the following example I'm drawing two shapes on a PGraphics object, however the shape on top does not render properly and interferes with the bottom one. Any idea why is this happening?

Shape shape;

void setup() {
  size(225, 195, P3D);
  smooth();
  shape = new Shape();
}

void draw() {
  background(0);
  image(shape.pgShape(), 0, 0);
  }

class Shape {
  float thetaMQ2;
  PGraphics pg;

  Shape() {
    pg = createGraphics(width, height, P3D);
  }

  PGraphics pgShape() {  
    float sizeOscillationMQ2 = 1.;
    float periodMQ2 = 300;
    float oscillationScaling = sizeOscillationMQ2 * cos(TWO_PI*frameCount/periodMQ2);
    float newOscMQ2 = sin(thetaMQ2);
    thetaMQ2+= 0.01;
    float mapOscMQ2 = map(newOscMQ2, -1., 1., 0, 1);

    pg.beginDraw();
    pg.noStroke();
    pg.fill(0, 2);
    pg.rect(0, 0, width, height);
    pg.pushMatrix();
    pg.translate(width/2, height/2);

    int circleResolutionMQ2 = (int)map(100, 0, height, 2, 10);
    float radiusMQ2 = (mapOscMQ2*width)-width/2 + 0.5;
    float angleMQ2 = TWO_PI/circleResolutionMQ2;

    pg.strokeWeight(2);
    pg.stroke(180);
    pg.beginShape();
    for (int i=0; i<=circleResolutionMQ2; i++) {
      float x = 0 + cos(angleMQ2*i) * radiusMQ2;
      float y = 0 + sin(angleMQ2*i) * radiusMQ2;
      pg.vertex(x, y);
    }
    pg.endShape();
    pg.popMatrix();
    pg.fill(255, 0, 0);
    pg.noStroke();
    pg.rect(-10, 15, 230, 40);

    pg.endDraw();  
    return pg;
  }
}
Tagged:

Answers

  • edited January 2015

    "the shape on top does not render properly and interferes with the bottom one"
    Not sure what you mean by that. I see a red rectangle over the hexagonal shape, but I don't know what you expect instead.
    Mmm, note that I removed the P3D declarations, because the OpenGL used by Processing is slow on my computer, not sure if it changes something.

    Also note that I moved your thread to the proper category...

  • Well, the rendering as you see from the following pictures is really bad even though I use simple shapes to draw. The first image is with P3D and the second without. The problem with the first is that the stroke of the bottom image passes on top of the top red shape, and the problem in the second case is that the rendering is very rough especially at the lines' edges. P3D

    noP3D

Sign In or Register to comment.