I want to convert this processing code to OF

#1

Hi, All.

I want to convert this processing code to OF.

float boxSize = 2; //立方体のサイズの初期値
float distance = 28; //縦方向の立方体同士の距離
float halfDis; //立方体同士の一辺の全体の距離の半分
float radius = 4; //x, y軸方向の立方体の距離の初期値
int boxNum = 8; //立方体の数
int angleX = -50; //x軸を中心とした回転角度の初期値
int angleZ = 0; //z軸を中心とした回転角度の初期値

void setup() {
size(400, 400, P3D);
colorMode(HSB, 360, 100, 100);

//立方体を縦方向に並べた際の距離の半分
halfDis = distance*(boxNum - 1)/2;

noStroke();
}

void draw() {
background(0);

translate(width/2, height/2); //基準点を画面中央に移動
rotateX(radians(-angleX)); //x軸を中心に回転
rotateZ(radians(-angleZ)); //z軸を中心に回転

for (int z = 0; z < boxNum; z ++) { //縦にも増やす
for (int angle = 0; angle < 360; angle += 15) { //15度ずつ移動

  //boxNumの数だけx軸方向に立方体を増やす
  for (int i = 0; i < boxNum; i ++) {

    pushMatrix();

    //円状に配置するためのxy座標の計算
    float x = i*(radius + i*1.5)*cos(radians(angle));
    float y = -i*(radius + i*1.5)*sin(radians(angle));  

    float saturation = float(i)/float(boxNum - 1)*100.0;  //彩度を計算
    float brigtness = float(z)/float(boxNum - 1)*100.0;  //明度を計算

    translate(x, y, z*distance - halfDis);  //一つずつ移動させる
    rotateZ(radians(-angle));  //円状に配置
    fill(angle, saturation, brigtness);  //色を指定する

    //立方体を描く
    box(boxSize + boxSize*i, boxSize + boxSize*i, boxSize + boxSize*i);

    popMatrix();
  }
}

}
}

How can ?

Thanks in advance.

#2

ofApp.h

ofFloatColor color;

int boxNum = 8;
int angleX = -50;
int angleZ = 0;

float boxSize = 2.f;
float distance = 28.f;
float halfDis = 0.f;
float radius = 4.f;

ofApp.cpp

void setup()
{
  halfDis = distance * (boxNum - 1) * .5f;
}

void draw()
{
  ofTranslate(ofGetWidth() * .5f, ofGetHeight() * .5f);
  ofRotateXDeg(-angleX);
  ofRotateZDeg(-angleZ);

  for (int z = 0; z < boxNum; ++z)
  {
    for (int angle = 0; angle < 360; angle += 15)
    {
      for (int i = 0; i < boxNum; ++i)
      {
        ofPushMatrix();

        float x =  i * (radius + i * 1.5f) * cos(ofDegToRad(angle));
        float y = -i * (radius + i * 1.5f) * sin(ofDegToRad(angle));

        float saturation = float(i) / float(boxNum - 1);
        float brigtness  = float(z) / float(boxNum - 1);

        ofTranslate(x, y, z * distance - halfDis);
        ofRotateZDeg(-angle);

        color.setHsb(angle / 360.f, saturation, brigtness);

        ofSetColor(color);
        ofDrawBox(boxSize + boxSize*i, boxSize + boxSize*i, boxSize + boxSize*i);

        ofPopMatrix();
      }
    }
  }
}
2 Likes
#3

Hi, @bidul

Thanks, It works ~

It is my first 3D OF.

Thanks.

@bemoregt.

1 Like
#4

glad it helps

rereading the code, it could be better to declare just once the x, y, saturation, brightness variables in the header file and just update them in the nested for loops

1 Like