Randomizing images as textures in 3D solids

3

Well, I have 30 solids and I have a different image for each of them. I created an array for the images, and if I choose to use the same image at all (as I left here in the code, use bandeira [0] ) it runs and everything is fine!

I now want to do a random that runs through the array of images and assigns a different image to each of the solids, but for some reason when I tried to% % - and stay with (int index = int(random(0, bandeira.length)); instead of index ) it continues to create all solids with the same image.

MAIN

int numPaises = 30;

float theta = 0;
PImage logo, chao;
PShape pessoa;

Paises [] solidos = new Paises [numPaises];  //Array solidos!

PImage[] bandeira = new PImage[30];


void setup() {

  bandeira[0] = loadImage("portugal.jpg");
  bandeira[1] = loadImage("espanha.jpg");
  bandeira[2] = loadImage("franca.jpg");
  bandeira[3] = loadImage("italia.jpg");
  bandeira[4] = loadImage("inglaterra.jpg");
  bandeira[5] = loadImage("alemanha.jpg");
  bandeira[6] = loadImage("brasil.jpg");
  bandeira[7] = loadImage("estadosUnidos.jpg");
  bandeira[8] = loadImage("china.jpg");
  bandeira[9] = loadImage("russia.jpg");
  bandeira[10] = loadImage("quenia.jpg");
  bandeira[11] = loadImage("argelia.jpg");
  bandeira[12] = loadImage("africaSul.jpg");
  bandeira[13] = loadImage("armenia.jpg");
  bandeira[14] = loadImage("austria.jpg");
  bandeira[15] = loadImage("bahamas.jpg");
  bandeira[16] = loadImage("belgica.jpg");
  bandeira[17] = loadImage("benin.jpg");
  bandeira[18] = loadImage("bulgaria.jpg");
  bandeira[19] = loadImage("chile.jpg");
  bandeira[20] = loadImage("congo.jpg");
  bandeira[21] = loadImage("cuba.jpg");
  bandeira[22] = loadImage("filandia.jpg");
  bandeira[23] = loadImage("jamaica.jpg");
  bandeira[24] = loadImage("noruega.jpg");

  size (1200, 650, P3D);

  for (int j=0; j < solidos.length; j++) {
    float angle = j * TWO_PI / numPaises;
    float x = 0 + 2000 * cos(angle);  
    float z = 0 + 2000 * sin(angle);
    int index = int(random(0, bandeira.length));     // *******
    solidos[j] = new Paises(x, 700, z, bandeira[0]);
  }
}

void draw() {

  background (0);

  scale(0.8); //escala de visão
  translate(700, 0, -3500);

  for (int j =0; j < solidos.length; j++) {
    solidos[j].render();
  }

  theta += 0.020;
}

CLASS COUNTRIES

class Paises {
  float x, y, z;
  float w, h, d;

  //CONSTRUTOR

  Paises(float nx, float ny, float nz, PImage img ) {
    x =nx;
    y = ny;
    z =nz;
    w= random(130, 200);
    h= random(400, 500);
    d= random(130, 200);

    noStroke();

    pessoa = createShape(BOX, w, h, d);
    pessoa.setTexture(img);
  }

  void render() {
    pushMatrix();
    rotateY(theta);
    translate(x, y-h/2, z);
    shape(pessoa);
    //box(w, h, d);
    popMatrix();
  }
}
    
asked by anonymous 15.06.2016 / 23:03

1 answer

2

The problem is that in the way that pessoa had been declared, the class was overwriting its value for each instance.

Already working correctly, just move the declaration from pessoa to inside class:

class Paises {

  PShape pessoa;
  float x, y, z;
  float w, h, d;

no setup() looks like this:

int index = int(random(0, bandeira.length));     
solidos[j] = new Paises(x, 700, z, bandeira[index]);
    
15.06.2016 / 23:31