Using PShapes as a canvas for images

PImage tex;
PImage tex2;

PGraphics pg;

PImage img = createImage(50, 50, RGB);
PImage img2 = createImage(50, 50, RGB);

int psX, psY = 200;

float rotx, roty, rotz;
float size;

void setup() 
{
  size(600, 600, P3D);
  textureMode(NORMAL);

  pg = createGraphics(200, 200);
}

void draw() 
{
  background(128);
  noStroke();

  //rotx += TAU/500;
  //roty += TAU/500;

  pg.beginDraw();
  pg.pushMatrix();
  pg.translate(100, 100);
  pg.pushMatrix();
  pg.rotate(radians(mouseX));
  pg.translate(-100, -100);

  pg.strokeWeight(5);
  pg.stroke(0);
  pg.fill(255, 255, 0);
  pg.circle(100, 100, 190);
  pg.rectMode(CENTER);
  pg.fill(0, 255, 0);
  pg.rect(100, 100, 50, 50);

  pg.popMatrix(); 
  pg.popMatrix();

  pg.pushMatrix();
  pg.translate(psX, psY);
  pg.pushMatrix();
  pg.rotate(rotx*2);
  pg.translate(-100, -100);

  pg.strokeWeight(5);
  pg.stroke(0);
  pg.fill(0, 255, 255);
  pg.circle(100, 100, 190);
  pg.rectMode(CENTER);
  pg.fill(255, 0, 0);
  pg.rect(100, 100, 50, 50);

  pg.popMatrix(); 
  pg.popMatrix();

  pg.endDraw();

  //img = get(0, 0, 200, 200);

  //image(pg, 400, 400); 

  pushMatrix();
  translate(width/2.0, height/2.0, -500);
  //rotateX(radians(mouseX));
  //rotateY(radians(mouseY));
  size = 100;
  noStroke();
  TexturedCube(img);
  popMatrix();
}

void keyPressed()
{
  if (keyCode == UP)
  {
    psY--;
  }

  if (keyCode == DOWN)
  {
    psY++;
  }

  if (keyCode == LEFT)
  {
    psX--;
  }

  if (keyCode == RIGHT)
  {
    psX++;
  }
}

void TexturedCube(PImage tex) 
{
  beginShape(QUADS);
  texture(pg); 
  // +Z "front" face
  vertex(-size, -size, size, 0, 0);
  vertex( size, -size, size, 1, 0);
  vertex( size, size, size, 1, 1);
  vertex(-size, size, size, 0, 1);
  endShape();

  beginShape(QUADS);
  texture(pg);
  // +Y "bottom" face
  vertex(-size, size, size, 0, 0);
  vertex( size, size, size, 1, 0);
  vertex( size, size, -size, 1, 1);
  vertex(-size, size, -size, 0, 1);
  endShape();

  beginShape(QUADS);
  texture(pg);
  //  fill(255, 255, 0);
  // +X "right" face
  vertex( size, -size, size, 0, 0);
  vertex( size, -size, -size, 1, 0);
  vertex( size, size, -size, 1, 1);
  vertex( size, size, size, 0, 1);
  endShape();
}

… I figured it out… I missed the endDraw. This was causing the problem. Edited to fix.

1 Like