Array of images with effect

Hi, i’m trying to combine an array of random images with an effect i found in the processing sketches called Explode. I don’t realize what kind of variable I need to use to inicialize the action.

So basically, this is the Explode sketch:

PImage img;       // The source image
int cellsize = 2; // Dimensions of each cell in the grid
int columns, rows;   // Number of columns and rows in our system

void setup() {
  size(640, 360, P3D); 
  img = loadImage("eames.jpg");  // Load the image
  columns = img.width / cellsize;  // Calculate # of columns
  rows = img.height / cellsize;  // Calculate # of rows
}

void draw() {
  background(0);
  // Begin loop for columns
  for ( int i = 0; i < columns; i++) {
    // Begin loop for rows
    for ( int j = 0; j < rows; j++) {
      int x = i*cellsize + cellsize/2;  // x position
      int y = j*cellsize + cellsize/2;  // y position
      int loc = x + y*img.width;  // Pixel array location
      color c = img.pixels[loc];  // Grab the color
      // Calculate a z position as a function of mouseX and pixel brightness
      float z = (mouseX / float(width)) * brightness(img.pixels[loc]) - 20.0;
      // Translate to the location, set fill and stroke, and draw the rect
      pushMatrix();
      translate(x + 200, y + 100, z);
      fill(c, 204);
      noStroke();
      rectMode(CENTER);
      rect(0, 0, cellsize, cellsize);
      popMatrix();
    }
  }
}

Now, my random image sketch is this one:

PImage [] images = new PImage[3];
int index = 0;
Names[] name = new Names[images.length];

int cellsize = 2; // Dimensions of each cell in the grid
int columns, rows; 

void setup(){
  size(640,320);
  
  images[0] = loadImage("image0.jpg");
  images[1] = loadImage("image1.jpg");
  images[2] = loadImage("image2.jpg");
 
  for(int i = 0; i < images.length; i++){
    name[i] = new Names(images[i]);
  }
  
  columns = xxxxxx.width / cellsize;  // Calculate # of columns
  rows = xxxxxx.height / cellsize;  // Calculate # of rows
}

void draw(){
  background(0);
  // Begin loop for columns
  for ( int i = 0; i < columns; i++) {
    // Begin loop for rows
    for ( int j = 0; j < rows; j++) {
      int x = i*cellsize + cellsize/2;  // x position
      int y = j*cellsize + cellsize/2;  // y position
      int loc = x + y*xxxxx.width;  // Pixel array location
      color c = xxxxxx.pixels[loc];  // Grab the color
      // Calculate a z position as a function of mouseX and pixel brightness
      float z = (mouseX / float(width)) * brightness(xxxxxx.pixels[loc]) - 20.0;
      // Translate to the location, set fill and stroke, and draw the rect
      pushMatrix();
      translate(x + 200, y + 100, z);
      fill(c, 204);
      noStroke();
      rectMode(CENTER);
      rect(0, 0, cellsize, cellsize);
      popMatrix();
    }
  }
}

void mousePressed(){
  background(0);
  index = int(random(0,images.length));
  name[index].display();
  println(index);
}

class Names{
  
 PImage img;
 
 Names(PImage tempImg){
   img = tempImg;
 }
 
 void display(){
   stroke(0);
   imageMode(CENTER);
   image(img,width/2,height/2);
 }
}

I was trying with differents variables where “xxxxx” is, but it didnt work. Please, maybe there’s something i’m missing and i can’t see it, thanks for taking your time.

1 Like

In your example code img is the variable:
img = loadImage(“eames.jpg”);

In your code ? is the variable:
images[0] = loadImage(“image0.jpg”);

I will leave the ? with you!

Only other issue is you did a 3D translate but you are not using 3D rendering.
You will get to that error soon!

:slight_smile:

4 Likes