How to Use Texture on PShape Vertices in a PGraphics Window

Hi all,

I'm having trouble with a bit of code that looks like it should work fine, but doesn't.

I want to draw a texture on vertices, but all of this is happens inside a PGraphics window. In the code below, the shape gets drawn just fine with a fill, but if it's a texture then nothing works. However, when drawn outside of the window the texture works just fine. You can check by using comment/uncomment on each section.

Is this a bug? Or is there some unknown solution?

PImage photo;
PGraphics mf;

void setup(){
  size(800, 600, P3D);
  mf = createGraphics(500, 315, P3D);
  background(100);
  photo = loadImage("test.png");
  textureMode(NORMAL);
  imageMode(CENTER);
}

void draw(){
  background(150);

  //pushMatrix();
  //translate(width/2, height/2);
  //noStroke();
  //beginShape();
  // texture(photo);
  // vertex(-mf.width/2, -mf.height/2, 0, 0);
  // vertex(mf.width/2, -mf.height/2, 1, 0);
  // vertex(mf.width/2, mf.height/2, 1, 1);
  // vertex(-mf.width/2, mf.height/2, 0, 1);
  //endShape();
  //popMatrix();

  mf.beginDraw();
   mf.background(50);
   mf.pushMatrix();
   mf.translate(mf.width/2 + 10, mf.height/2 + 10);
   mf.noStroke();
   mf.beginShape();
     //mf.texture(photo);
     mf.fill(255);
     mf.vertex(-mf.width/2, -mf.height/2, 0, 0);
     mf.vertex(mf.width/2, -mf.height/2, 1, 0);
     mf.vertex(mf.width/2, mf.height/2, 1, 1);
     mf.vertex(-mf.width/2, mf.height/2, 0, 1);
   mf.endShape();
   mf.popMatrix();
  mf.endDraw();

  pushMatrix();
  translate(width/2, height/2);
  image(mf, 0, 0);
  popMatrix();
}

Answers

  • Haven't tried it but you are using 2d vertexes in 3d mode, maybe that's confusing it

  • Unfortunately that's not it. Neither adding a z value, nor changing the renderer to P2D has any effect.

  • Answer ✓

    a-ha!

    mf.textureMode(NORMAL);
    

    you are setting textureMode to NORMAL but that's NOT the texture mode for the created pgraphics...

Sign In or Register to comment.