I'm working on processing for a project related to my studies. I want to make 3d boxes with specific textures, so far I created a box with different textures on it. I also want to display them in a grid by using an array so the code is below and can't figure out how to convert it to an array
ps. as image i just simply created 6 different color box to check how it works.
Cube myCube;
int x;
void setup(){
size(800,800,P3D);
myCube = new Cube();
}
void draw(){
myCube.display();
}
class Cube{
PImage tex, tex1, tex2, tex3, tex4, tex5;
void display(){
background(0);
noStroke();
translate(100+x,100+x, 0);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
scale(50);
tex = loadImage("image0.jpg");
tex1 = loadImage("image1.jpg");
tex2 = loadImage("image2.jpg");
tex3 = loadImage("image3.jpg");
tex4 = loadImage("image4.jpg");
tex5 = loadImage("image5.jpg");
textureMode(NORMAL);
beginShape(QUADS);
texture(tex);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -Z "back" face
texture(tex1);
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// +Y "bottom" face
texture(tex2);
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// -Y "top" face
texture(tex3);
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
endShape();
beginShape(QUADS);
// +X "right" face
texture(tex4);
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -X "left" face
texture(tex5);
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
}
}
Here is the code after Kevin's help.
ArrayList<Cube> myCubes = new ArrayList<Cube>();
void setup(){
size(800,800,P3D);
frameRate(60);
for(int i = 0; i < 10; i++){
myCubes.add(new Cube());
}
}
void draw(){
for(Cube myCube : myCubes){
myCube.display();
}
}
class Cube{
PImage tex, tex1, tex2, tex3, tex4, tex5;
float x;
float y;
float scale;
public Cube(){
this.x = random(width);
this.y = random(height);
this. scale = random(10, 50);
}
void display(){
background(0);
noStroke();
pushMatrix();
background(0);
noStroke();
translate(x,y, 0);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
scale(scale);
tex = loadImage("image0.jpg");
tex1 = loadImage("image1.jpg");
tex2 = loadImage("image2.jpg");
tex3 = loadImage("image3.jpg");
tex4 = loadImage("image4.jpg");
tex5 = loadImage("image5.jpg");
textureMode(NORMAL);
beginShape(QUADS);
texture(tex);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -Z "back" face
texture(tex1);
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// +Y "bottom" face
texture(tex2);
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// -Y "top" face
texture(tex3);
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
endShape();
beginShape(QUADS);
// +X "right" face
texture(tex4);
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -X "left" face
texture(tex5);
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
popMatrix();
}
}