我目前正在为学校开展处理项目。我有点像菜鸟,很抱歉,如果我的代码有点乱!我很难将不同的纹理应用到不同的立方体上。我尝试使用数组应用纹理,但我不确定它是否正确。有评论,但不要介意他们。请大哥帮帮忙!感谢
这是我的主要课程:
float rotx = PI/4;
float roty = PI/4;
float bounds = 300;
Cube[] cubies = new Cube[5];
int maxImages = 3;
int imageIndex = 0;
PImage[] images = new PImage[maxImages];
void setup() {
size(640, 360, P3D);
/*tex = loadImage("image0.jpg");
tex2 = loadImage("image1.jpg");
tex3 = loadImage("image2.jpg");*/
//imageIndex = int(random(images.length));
for (int i = 0; i < cubies.length; i++) {
// Cubies are randomly sized
float cubieSize = random(15, 55);
cubies[i] = new Cube(cubieSize, cubieSize, cubieSize);
for (int j = 0; j<images.length; j ++) {
images[j] = loadImage("image" + j + ".jpg");
texture(images[j]);
}
}
}
void draw() {
/*background(0);
noStroke();
//translate(width/2.0, height/2.0, -100);
scale(90);
//Cube();*/
background(50);
lights();
rotateX(rotx);
rotateY(roty);
// Center in display window
translate(width/2, height/2, -130);
// Rotate everything, including external large cube
rotateX(frameCount * 0.001);
rotateY(frameCount * 0.002);
rotateZ(frameCount * 0.001);
stroke(255);
for(Cube c : cubies){
c.display();
c.update();
}
}
void mouseDragged() {
float rate = 0.01;
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
}
这是我的Cube类:
class Cube {
PVector position;
PVector velocity;
// Also using PVector to hold rotation values for 3 axes
PVector rotation;
// Vertices of the cube
PVector[] vertices = new PVector[24];
// width, height, depth
float w, h, d;
Cube(float w, float h, float d) {
this.w = w;
this.h = h;
this.d = d;
textureMode(NORMAL);
fill(255);
stroke(color(44, 48, 32));
// Start in center
position = new PVector();
// Random velocity vector
velocity = PVector.random3D();
// Random rotation
rotation = new PVector(random(40, 100), random(40, 100), random(40, 100));
vertices[0] = new PVector(-w/2, -h/2, d/2);
vertices[1] = new PVector(w/2, -h/2, d/2);
vertices[2] = new PVector(w/2, h/2, d/2);
vertices[3] = new PVector(-w/2, h/2, d/2);
//left
vertices[4] = new PVector(-w/2, -h/2, d/2);
vertices[5] = new PVector(-w/2, -h/2, -d/2);
vertices[6] = new PVector(-w/2, h/2, -d/2);
vertices[7] = new PVector(-w/2, h/2, d/2);
//right
vertices[8] = new PVector(w/2, -h/2, d/2);
vertices[9] = new PVector(w/2, -h/2, -d/2);
vertices[10] = new PVector(w/2, h/2, -d/2);
vertices[11] = new PVector(w/2, h/2, d/2);
//back
vertices[12] = new PVector(-w/2, -h/2, -d/2);
vertices[13] = new PVector(w/2, -h/2, -d/2);
vertices[14] = new PVector(w/2, h/2, -d/2);
vertices[15] = new PVector(-w/2, h/2, -d/2);
//top
vertices[16] = new PVector(-w/2, -h/2, d/2);
vertices[17] = new PVector(-w/2, -h/2, -d/2);
vertices[18] = new PVector(w/2, -h/2, -d/2);
vertices[19] = new PVector(w/2, -h/2, d/2);
//bottom
vertices[20] = new PVector(-w/2, h/2, d/2);
vertices[21] = new PVector(-w/2, h/2, -d/2);
vertices[22] = new PVector(w/2, h/2, -d/2);
vertices[23] = new PVector(w/2, h/2, d/2);
endShape();
}
void drawCube() {
// Draw cube
for (int i=0; i<6; i++) {
beginShape(QUADS);
for (int j=0; j<4; j++) {
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
}
endShape();
}
}
// Update location
void update() {
position.add(velocity);
// Check wall collisions
if (position.x > bounds/2 || position.x < -bounds/2) {
velocity.x*=-1;
}
if (position.y > bounds/2 || position.y < -bounds/2) {
velocity.y*=-1;
}
if (position.z > bounds/2 || position.z < -bounds/2) {
velocity.z*=-1;
}
}
void display() {
pushMatrix();
translate(position.x, position.y, position.z);
rotateX(frameCount*PI/rotation.x);
rotateY(frameCount*PI/rotation.y);
rotateZ(frameCount*PI/rotation.z);
noStroke();
drawCube(); // Farm out shape to another method
popMatrix();
}
}
编辑:为具有多个纹理的多维数据集添加代码(根据要求)。 here is the final result of this class
float rotx = PI/4;
float roty = PI/4;
int maxImages = 3;
int imageIndex = 0;
PImage[] images = new PImage[maxImages];
//PImage img = createImage(50,50,RGB);
void setup() {
size(640, 360, P3D);
/*tex = loadImage("image0.jpg");
tex2 = loadImage("image1.jpg");
tex3 = loadImage("image2.jpg");*/
for(int i = 0; i<images.length; i ++){
images[i] = loadImage("image" + i + ".jpg");
}
//imageIndex = int(random(images.length));
textureMode(NORMAL);
fill(255);
stroke(color(44,48,32));
}
void draw() {
background(0);
noStroke();
translate(width/2.0, height/2.0, -100);
rotateX(rotx);
rotateY(roty);
scale(90);
TexturedCube();
}
void TexturedCube() {
beginShape(QUADS);
texture(images[0]);
// +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();
// -Z "back" 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);
beginShape(QUADS);
texture(images[1]);
// +Y "bottom" 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);
// -Y "top" 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);
// +X "right" 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);
// -X "left" 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();
}
void mouseDragged() {
float rate = 0.01;
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
}