Processingのサンプルを参考にコードを書いた。(立方体の回転2)

コードを改良した。

昨日書いたコードを改良した。

改良点

  • TexturedCubeに立方体の大きさを指定する引数を追加。
  • 関数TexturedCubeP1, TexturedCubeP2を追加。立方体の面の半分を描画。この2つの関数で描画されたものは対称になっている。

(ここでは、TexturedCubeP1のみを使用。このコードを走らせると、入れ子になった立方体の面が描画される。)

[,w485,h300]

PImage tex;
float rotx = PI/4;
float roty = PI/4;
int value = 0;

void setup() 
{
  size(809, 500, P3D);
  tex = loadImage("01.JPG");
  textureMode(NORMALIZED);
  fill(255);
  stroke(color(44,48,32));
}

void draw() 
{
  background(#dddddd);
  noStroke();
  translate(width/2.0, height/2.0, -100);
  rotateX(rotx);
  rotateY(roty);
  scale(90);
  TexturedCubeP1(tex, 1);
  TexturedCubeP1(tex, 1.5);
  TexturedCubeP1(tex, 1.7);
  autorotate();
}

void autorotate()
{
  float rate = 0.01;
  float speed = (mouseX+mouseY) * 0.02;
  rotx += speed * rate;
  roty += speed * rate;;
}

void TexturedCube(PImage tex, float i) {
  beginShape(QUADS);
  texture(tex);
  // +Z "front" face
  vertex(-i, -i,  i, 0, 0);
  vertex( i, -i,  i, i, 0);
  vertex( i,  i,  i, i, i);
  vertex(-i,  i,  i, 0, i);

  // -Z "back" face
  vertex( i, -i, -i, 0, 0);
  vertex(-i, -i, -i, i, 0);
  vertex(-i,  i, -i, i, i);
  vertex( i,  i, -i, 0, i);

  // +Y "bottom" face
  vertex(-i,  i,  i, 0, 0);
  vertex( i,  i,  i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex(-i,  i, -i, 0, i);

  // -Y "top" face
  vertex(-i, -i, -i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i, -i,  i, i, i);
  vertex(-i, -i,  i, 0, i);

  // +X "right" face
  vertex( i, -i,  i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex( i,  i,  i, 0, i);

  // -X "left" face
  vertex(-i, -i, -i, 0, 0);
  vertex(-i, -i,  i, i, 0);
  vertex(-i,  i,  i, i, i);
  vertex(-i,  i, -i, 0, i);

  endShape();
}

void TexturedCubeP1(PImage tex, float i) {
  beginShape(QUADS);
  texture(tex);
  // +Z "front" face
  vertex(-i, -i,  i, 0, 0);
  vertex( i, -i,  i, i, 0);
  vertex( i,  i,  i, i, i);
  vertex(-i,  i,  i, 0, i);

  // +Y "bottom" face
  vertex(-i,  i,  i, 0, 0);
  vertex( i,  i,  i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex(-i,  i, -i, 0, i);

  // +X "right" face
  vertex( i, -i,  i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex( i,  i,  i, 0, i);

  endShape();
}

void TexturedCubeP2(PImage tex, float i) {
  beginShape(QUADS);
  texture(tex);

  // -Z "back" face
  vertex( i, -i, -i, 0, 0);
  vertex(-i, -i, -i, i, 0);
  vertex(-i,  i, -i, i, i);
  vertex( i,  i, -i, 0, i);

  // -Y "top" face
  vertex(-i, -i, -i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i, -i,  i, i, i);
  vertex(-i, -i,  i, 0, i);

  // -X "left" face
  vertex(-i, -i, -i, 0, 0);
  vertex(-i, -i,  i, i, 0);
  vertex(-i,  i,  i, i, i);
  vertex(-i,  i, -i, 0, i);

  endShape();
}