首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >处理-来自类的数组

处理-来自类的数组
EN

Stack Overflow用户
提问于 2016-02-15 10:13:27
回答 1查看 208关注 0票数 2

我正在处理一个与我的学习有关的项目。我想要制作带有特定纹理的3d盒子,到目前为止,我在它上创建了一个不同纹理的盒子。我还希望通过使用数组在网格中显示它们,因此代码如下所示,无法确定如何将其转换为数组

ps。作为图像,我只是简单地创建了6个不同的颜色框来检查它是如何工作的。

代码语言:javascript
运行
复制
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();

}
}

这是凯文帮忙后的密码。

代码语言:javascript
运行
复制
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();




}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-15 13:26:20

首先,必须使Cube类的每个实例在不同的位置显示,否则您将无法判断是否有多个Cubes。就像这样:

代码语言:javascript
运行
复制
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(){
  pushMatrix();
  noStroke();
  translate(x,y, 0);
  rotateY(map(mouseX, 0, width, 0, PI));
  rotateX(map(mouseY, 0, height, 0, PI));
  scale(scale);
  //rest of your code
  popMatrix();

注意,我已经向pushMatrix()popMatrix()添加了调用,这样您的翻译和旋转就不会堆叠起来。

然后,您可以轻松地使用数组而不是单个实例:

代码语言:javascript
运行
复制
Cube[] myCubes = new Cube[10];

void setup(){
   size(800,800,P3D);
   for(int i = 0; i < myCubes.length; i++){
      myCubes[i] = new Cube();
   }
}

void draw(){
   backgrond(0);
   for(Cube myCube : myCubes){
      myCube.display();
   }
}

您还可以使用ArrayList

代码语言:javascript
运行
复制
ArrayList<Cube> myCubes = new ArrayList<Cube>();

void setup(){
   size(800,800,P3D);
   for(int i = 0; i < 10; i++){
      myCubes.add(new Cube());
   }
}

void draw(){
   for(Cube myCube : myCubes){
      myCube.display();
   }
}

编辑:在您更新代码之后,您只能看到一个Cube,因为您正在为每个Cube重新绘制背景,所以您最终会在以前的Cubes上进行绘制。将其移到draw()函数的开头。而且,每次绘制Cube时都会加载纹理文件,这会导致速度慢。将其移动到Cube构造函数。把这一切结合在一起:

代码语言:javascript
运行
复制
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() {
  background(0);
  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);

    tex = loadImage("image0.jpg");
    tex1 = loadImage("image1.jpg");
    tex2 = loadImage("image2.jpg");
    tex3 = loadImage("image3.jpg");
    tex4 = loadImage("image4.jpg");
    tex5 = loadImage("image5.jpg");
  }

  void display() {

    noStroke();
    pushMatrix();
    noStroke();
    translate(x, y, 0);
    rotateY(map(mouseX, 0, width, 0, PI));
    rotateX(map(mouseY, 0, height, 0, PI));
    scale(scale);

    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();
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35406593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档