PShape always writes to main buffer?

Hi all, I'm trying to get PShape to render to an off-screen buffer, but I'm getting unexpected results. I've prepared a sketch to demonstrate. Basically I never use the PImage drawn in the sketch below. and the shape within my Thing class is drawn to the offscreen buffer so I shouldn't be able to see it. The shape also disappears if you comment out the indicated line. (Line 20)

Anyone have any ideas what's happening?

Thanks

Dave

Processing 2.0.3 / Windows 7-64bit.

PImage drawn;
PGraphics buffer;
int s = 512;
Thing thing;

void setup() {
  size(s, s, OPENGL);
  buffer = createGraphics(width, height, OPENGL);
  thing = new Thing(width/2, height/2, 150, buffer);
}

void draw()
{
  render();
}

void render()
{
  thing.display();
  drawn = buffer.get(0, 0, buffer.width, buffer.height); 
 //comment the line above out and the image doesn't draw, despite never using it.
}

class Thing
{
  PShape ps;
  PGraphics buf;
  int x, y, size;

  Thing(int x, int y, int size, PGraphics buf)
  {
    this.x = x;
    this.y = y;
    this.size = size;
    this.buf = buf;
    makeShape(size);
  }

  void makeShape(int s)
  {
    ps = createShape();
    ps.beginShape();
    ps.fill(0, 0, 255);
    ps.noStroke();
    ps.vertex(0, 0);
    ps.vertex(0, size);
    ps.vertex(size, size);
    ps.vertex(size, 0);
    ps.endShape(CLOSE);
  }

  void display()
  {
    buf.shape(ps, x, y);
  }
}
Tagged:

Answers

  • OpenGL is an graphics card renderer, so in theory, off-line rendering is not possible.

    Now, I have seen people using special buffers (on Linux) making possible to make OpenGL to draw to off-line buffers. I don't know if Processing implemented this, in a portable way.

    My guess (from somebody with a sketchy knowledge of OpenGL!): the get() forces a bitmap rendering of the OpenGL instructions, but it renders on screen, as designed...

  • I think the problem with the offscreen rendering in the code posted by ClaxDesign is that it's missing the beginDraw()/endDraw() that are required when using a PGraphics surface:

    PImage drawn;
    PGraphics buffer;
    int s = 512;
    Thing thing;
    
    void setup() {
      size(s, s, OPENGL);
      buffer = createGraphics(width, height, OPENGL);
      thing = new Thing(width/2, height/2, 150, buffer);
    }
    
    void draw()
    {
      render();
    }
    
    void render()
    {
      thing.display();
      image(buffer, 0, 0);    
    }
    
    class Thing
    {
      PShape ps;
      PGraphics buf;
      int x, y, size;
    
      Thing(int x, int y, int size, PGraphics buf)
      {
        this.x = x;
        this.y = y;
        this.size = size;
        this.buf = buf;
        makeShape(size);
      }
    
      void makeShape(int s)
      {
        ps = createShape();
        ps.beginShape();
        ps.fill(0, 0, 255);
        ps.noStroke();
        ps.vertex(0, 0);
        ps.vertex(0, size);
        ps.vertex(size, size);
        ps.vertex(size, 0);
        ps.endShape(CLOSE);
      }
    
      void display()
      {
        buf.beginDraw();
        buf.background(255, 0, 0);
        buf.shape(ps, x, y);
        buf.endDraw();
      }
    }
    
  • Ah, I totally overlooked that! Thanks.

  • Perfect, thanks Andrés, Philippe - My mistake!

Sign In or Register to comment.