読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

sphere()を使わずに球を描く

/**
* sphere without sphere()
*
* @author aa_debdeb
* @date 2016/06/19
*/

float radious = 200;

void setup(){
  size(500, 500, P3D);
  fill(0, 206, 209);
  stroke(128);
  strokeWeight(1);
}

void draw(){
  background(0);
//  lights();
  translate(width / 2, height / 2, 0);
  rotateX(frameCount * 0.01);
  rotateY(frameCount * 0.02);
  rotateZ(frameCount * 0.03);
  int resolution = int(map(mouseX, 0, width, 2, 30)) * 2;
  float step = TWO_PI / resolution;
  for(int i = 0; i < resolution / 2; i++){
    for(int j = 0; j < resolution; j++){
      float r1 = step * i;
      float nr1 = step * (i + 1);
      float r2 = step * j;
      float nr2 = step * (j + 1);
      PVector p1 = new PVector(radious * sin(r1) * cos(r2), radious * cos(r1), radious * sin(r1) * sin(r2));
      PVector p2 = new PVector(radious * sin(nr1) * cos(r2), radious * cos(nr1), radious * sin(nr1) * sin(r2));
      PVector p3 = new PVector(radious * sin(nr1) * cos(nr2), radious * cos(nr1), radious * sin(nr1) * sin(nr2));
      PVector p4 = new PVector(radious * sin(r1) * cos(nr2), radious * cos(r1), radious * sin(r1) * sin(nr2));
      beginShape();
      vertex(p1.x, p1.y, p1.z);
      vertex(p2.x, p2.y, p2.z);
      vertex(p3.x, p3.y, p3.z);
      vertex(p4.x, p4.y, p4.z);
      endShape();
    }
  }
}