PGraphics Unexpected Behavior

Hi. I am doing the following process to use PGraphics with a texture and display it in a geometry. The same part of the code works in another sketch, but not isolated as you find here. Can someone explain to me why I am getting errors: texture() is not available with this renderer. vertex(x, y, u, v) is not available with this renderer.

PGraphics tempPG;
PImage img;

void setup() {
  size(225, 195, P3D);
  tempPG = createGraphics(225, 195);
  img = loadImage("img.png");
}

void draw() {
  background(0);

  tempPG.beginDraw();
  tempPG.beginShape();
  tempPG.translate(width/2, height/2);
  tempPG.fill(255, 0, 0);

  tempPG.texture(img);
  tempPG.noStroke();
  tempPG.scale(1.03);

  tempPG.vertex(-106, 0, 0, 97);
  tempPG.vertex( -53, -92, 225/4, 0);
  tempPG.vertex(  53, -92, 225-(225/4), 0);
  tempPG.vertex( 106, 0, 225, 97);
  tempPG.vertex(  53, 92, 225-(225/4), 195);
  tempPG.vertex( -53, 92, 225/4, 195);
  tempPG.endShape();
  tempPG.endDraw();

  image(tempPG, 0, 0);
}

Answers

  • Answer ✓

    Perhaps we should be more explicit in createGraphics() by specifying which renderer to instantiate:

    http://processing.org/reference/createGraphics_.html

    1. tempPG = createGraphics(width, height, JAVA2D);
    2. tempPG = createGraphics(width, height, P2D);
    3. tempPG = createGraphics(width, height, P3D);
  • GoToLoop is right. You are not specifying a renderer for your PGraphics, so then the default JAVA2D renderer is used (similar to not specifying a renderer in size()). When you look at the reference for texture() you will see that this method only works with the P2D and P3D renderers: https://processing.org/reference/texture_.html

Sign In or Register to comment.