Modified PImage inside new PImage to use with Texture()

I am using a PImage to run some animation inside it. It seems I can't use Texture() in my animated PImage.

I am curious about the best way to optimize an sprite image and put it in perspective using Texture() mapping it to specific points.

Even if I think there must be a better way to do it without using a loop to get the pixels from image, I am not being able to run the sprite when scale is negative for X.

PImage sprite, animation;
boolean goRight = true;
int currentFrame;
int numFrames = 15;
int w = 182;
int h = 122;

void setup() {
  size(184, 122, P3D);
  sprite = loadImage("Muybridge.png");
  animation = new PImage(width, height);
}

void draw() {
  currentFrame = (millis()/4/(12)) % numFrames;

  if (goRight) {
    image(sprite, -w*(currentFrame%4), -h*round(currentFrame/4));
  } else {
    scale(-1, 1);
    image(sprite, -w -w*(currentFrame%4), -h*round(currentFrame/4));
  }

  loadPixels();
  for (int y = 0; y < height; y++ ) {
    for (int x = 0; x < width; x++ ) {
      int loc = x + y*width;
      animation.set(x, y, pixels[loc]);
    }
  }
  //animation.updatePixels();
  //background(0);//working only when goRight == true
  beginShape();
  texture(animation); //can't directly use "sprite" here
  vertex(10, 20, 0, 0);
  vertex(130, 5, w, 0);
  vertex(160, 90, w, h);
  vertex(40, 95, 0, h);
  endShape();
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      goRight = false;
    } else if (keyCode == RIGHT) {
      goRight = true;
    }
  }
}

Muybridge

Tagged:

Answers

  • I got it working now but I still wonder if this is the right way to use the texture() with this kind of animated sprite.

    PImage sprite, animation;
    boolean goRight = true;
    int currentFrame;
    int numFrames = 15;
    int w = 182;
    int h = 122;
    
    void setup() {
      size(184, 122, P3D);
      sprite = loadImage("Muybridge.png");
      animation = new PImage(width, height);
    }
    
    void draw() {
      currentFrame = (millis()/4/(12)) % numFrames;
      image(sprite, -w*(currentFrame%4), -h*round(currentFrame/4));
    
      loadPixels();
      for (int y = 0; y < height; y++ ) {
        for (int x = 0; x < width; x++ ) {
          int loc = x + y*width;
          animation.set(x, y, pixels[loc]);
        }
      }
    
      animation.updatePixels();
      background(0);
      beginShape();
      texture(animation); 
      if (goRight) {
        vertex(10, 20, 0, 0);
        vertex(130, 5, w, 0);
        vertex(160, 90, w, h);
        vertex(40, 95, 0, h);
      }else{
        vertex(10, 20, w, 0);
        vertex(130, 5, 0, 0);
        vertex(160, 90, 0, h);
        vertex(40, 95, w, h);
      }
      endShape();
    }
    
    void keyPressed() {
      if (key == CODED) {
        if (keyCode == LEFT) {
          goRight = false;
        } else if (keyCode == RIGHT) {
          goRight = true;
        }
      }
    }
    
Sign In or Register to comment.