Difference between PGraphic and PShape ?

edited November 2013 in Programming Questions

What is the difference between PGraphic and PShape ? When to use PGraphic and PShape? What is the most efficient way to draw a lot of moving stuff?

Tagged:

Answers

  • PGraphics is a 2 dimensional image/texture that has pixels that can be changed by direct manipulation through the pixels[] array, or by drawing images or shapes (ellipse, rect, point, line). The default Processing window created with size() is a PGraphics object.

    PShape is 2 or 3 dimensional collection of vertices that can be drawn to the screen.

    PShape is fast because all of the vertices are processed and drawn by the GPU at the same time. If I had a series of rect() commands, each rect would be drawn one at a time. If I consolidated all of the rectangles in to one PShape, it would draw considerably faster.

    Look up "Immediate Mode Rendering" for the reasons why PShape is so great.

    But to answer your question, use PShape when you have a collection of vertices to draw, and use PGraphics when you have a 2 dimensional region of pixels to draw.

    Does this help?

  • yes, thank you bill

  • I want to create a spectrogram. Is this a good method to render streaming of points? I put each column of points in PShape and move it using translate.

    ArrayList<Line> lines;
    
    void setup() {
      size(600, 600, P3D);
      smooth(16);
      lines = new ArrayList<Line>();
    }
    
    void draw() {
      background(9);
    
      Line line = new Line();
      lines.add(line);
    
      for (int i=lines.size()-1; i>=0; i--) {
        Line l = lines.get(i);
        l.render();
        if (l.dead) lines.remove(i);
      }
    
      frame.setTitle(int(frameRate) +  " fps");
    }
    
    class Line {
    
      PShape s;
      int posX = 0;
      boolean dead = false;
    
      Line() {
        s = createShape(POINTS);
        s.beginShape();
        for (float i=0; i<=height; i+=0.5) {
          s.stroke(random(255));
          s.strokeWeight(1);
          s.vertex(0, i);
        }
        s.endShape();
      }
    
      void render() {
        pushMatrix();
        translate(posX, 0);
        shape(s);
        popMatrix();
        posX ++;
        if (posX > width) dead = true;
      }
    }
    
Sign In or Register to comment.