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

改良点

  • TexturedCubeP1, TexturedCubeP2を削除。代わりに、TexturedCubeに描画する面を指定する引数を追加。

引数は、1: 全部の面, 2: 半分の面, 3: 2と対称な半分の面

[,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);
  TexturedCube(tex, 1, 1);
  TexturedCube(tex, 1.5, 2);
  TexturedCube(tex, 1.7, 3);
  autorotate();
}

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

void PZface(float i) {
  vertex(-i, -i,  i, 0, 0);
  vertex( i, -i,  i, i, 0);
  vertex( i,  i,  i, i, i);
  vertex(-i,  i,  i, 0, i);
}

void MZface(float i) {
  vertex( i, -i, -i, 0, 0);
  vertex(-i, -i, -i, i, 0);
  vertex(-i,  i, -i, i, i);
  vertex( i,  i, -i, 0, i);
}

void BYface(float i) {
  vertex(-i,  i,  i, 0, 0);
  vertex( i,  i,  i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex(-i,  i, -i, 0, i);
}

void TYface(float i) {
  vertex(-i, -i, -i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i, -i,  i, i, i);
  vertex(-i, -i,  i, 0, i);
}

void RXface(float i) {
  vertex( i, -i,  i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex( i,  i,  i, 0, i);
}

void LXface(float i) {
  vertex(-i, -i, -i, 0, 0);
  vertex(-i, -i,  i, i, 0);
  vertex(-i,  i,  i, i, i);
  vertex(-i,  i, -i, 0, i);
}

void TexturedCube(PImage tex, float i, int type) {
  beginShape(QUADS);
  texture(tex);
  
  switch(type) {
  case 1: 
    PZface(i);
    MZface(i);
    BYface(i);
    TYface(i);
    RXface(i);
    LXface(i);
    break;
  case 2: 
    PZface(i);
    BYface(i);
    RXface(i); 
    break;
  default:
    MZface(i);
    BYface(i);
    LXface(i);
    break;
  }
  
  endShape();
}

(追記)
class CubeFaceを書いた。

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);
  TexturedCube(tex, 1, 1);
  TexturedCube(tex, 1.5, 2);
  TexturedCube(tex, 1.7, 3);
  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, int type) {
  beginShape(QUADS);
  texture(tex);
  
  CubeFace c1 = new CubeFace();
  
  if(type == 1){
    c1.PZface(i);
    c1.MZface(i);
    c1.BYface(i);
    c1.TYface(i);
    c1.RXface(i);
    c1.LXface(i);
  }
  else if(type == 2){ 
    c1.PZface(i);
    c1.BYface(i);
    c1.RXface(i); 
  }
  else{
    c1.MZface(i);
    c1.BYface(i);
    c1.LXface(i);
  }
  
  endShape();
}

class CubeFace {
  void PZface(float i) {
   vertex(-i, -i,  i, 0, 0);
   vertex( i, -i,  i, i, 0);
   vertex( i,  i,  i, i, i);
   vertex(-i,  i,  i, 0, i);
 }

 void MZface(float i) {
  vertex( i, -i, -i, 0, 0);
  vertex(-i, -i, -i, i, 0);
  vertex(-i,  i, -i, i, i);
  vertex( i,  i, -i, 0, i);
 }

 void BYface(float i) {
  vertex(-i,  i,  i, 0, 0);
  vertex( i,  i,  i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex(-i,  i, -i, 0, i);
 }

 void TYface(float i) {
  vertex(-i, -i, -i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i, -i,  i, i, i);
  vertex(-i, -i,  i, 0, i);
 }

 void RXface(float i) {
  vertex( i, -i,  i, 0, 0);
  vertex( i, -i, -i, i, 0);
  vertex( i,  i, -i, i, i);
  vertex( i,  i,  i, 0, i);
}

 void LXface(float i) {
  vertex(-i, -i, -i, 0, 0);
  vertex(-i, -i,  i, i, 0);
  vertex(-i,  i,  i, i, i);
  vertex(-i,  i, -i, 0, i);
 }
}