Processingのサンプルを参考にコードを書いた。(立方体: 回転、拡大、照明の調整、ポリモーフィズム)

追記: 2012/12/20

現在、processing の最新版が Ver 2.0b7 です。
バージョンアップに伴い、いくつか変更があるようです。

本エントリのコード「textureMode(NORMALIZED);」は
"NORMALIZED"を"NORMAL"に読み替えてください。

追記: 2012/12/21

本エントリのサンプルをキャプチャしたものを
youtube にアップしました。


改良点

ポリモーフィズムを利用した。class TexturedCubeを書いて、if文を消した。

コード

//変数の宣言
PImage tex;
float rotx = PI/4;
float roty = PI/4;
int valuex = 0;
int valuey = 1;

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

//描画
void draw() 
{
  int ColorValue = #dddddd;
  background(ColorValue);
  noStroke();
  directionalLight(126, 126, 126, 0, 0, -1);
  ambientLight(10+valuex, 10+valuex, 10+valuex);

  translate(width/2.0, height/2.0, -100);
  rotateX(rotx);
  rotateY(roty);
  scale(90);
  TexturedCube t = new TexturedCube();
  t.Type1(1+mouseY * 0.002);
  autorotate();
}

//自動回転のための関数
void autorotate()
{
  float rate = 0.01;
  float speed = mouseX * 0.02;
  rotx += speed * rate;
  roty += speed * rate;;
}

//出力する図形の種類について記述したクラス(6面ある立方体と、3面だけのものを2個を用意した。)
class TexturedCube {
  void Type1(float i){
    beginShape(QUADS);
    texture(tex);
    CubeFace c1 = new CubeFace();
    c1.PZface(i);
    c1.MZface(i);
    c1.BYface(i);
    c1.TYface(i);
    c1.RXface(i);
    c1.LXface(i);
    endShape();
    }
  void Type2(float i){
    beginShape(QUADS);
    texture(tex);
    CubeFace c1 = new CubeFace();
    c1.PZface(i);
    c1.BYface(i);
    c1.RXface(i);
    endShape();
  }
  void Type3(float i){
    beginShape(QUADS);
    texture(tex);
    CubeFace c1 = new CubeFace();
    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);
 }
}

//マウスのドラッグを、値に反映させる関数。
void mouseDragged(){
  valuex += (mouseX-pmouseX)*0.25; 
  valuey += (mouseY-pmouseY)*0.25; 
}