본문 바로가기
데모

[processing] Clover 01

by hansoo.labs 한수댁 2013. 5. 29.

클로버 그리기





코드에서 beginCamera(), camera(), endCamera() 는 processingjs 에서만 사용할 수 있다. IDE 에서는 camera(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) 을 사용하면 된다.

import processing.opengl.*;

CloverLeaf leaf1, leaf2, leaf3;

void setup() {
  
  size(500, 500, OPENGL);
  smooth();
  
  leaf1 = new CloverLeaf(0.0, 0.0, 0.0, 30.0, 20.0);
  leaf2 = new CloverLeaf(0.0, 0.0, 0.0, 150.0, 20.0);
  leaf3 = new CloverLeaf(0.0, 0.0, 0.0, 270.0, 20.0);
  
}

void draw() {
  
  lights();
  background(230);
  
  //CAMERA
  beginCamera();
  camera();
  translate(-250,-250,0);
  //rotateX(PI / 10);
  endCamera();
  
  rotateX(PI / map(mouseY, 0, height, 0.1, 1));
  rotateY(PI / map(mouseX, 0, width, 0.1, 1));
  leaf1.draw();
  leaf2.draw();
  leaf3.draw();
  
}

class CloverLeaf {
 
  //잎이 시작되는 부분 위치
  float x, y, z;
  //잎의 회전값 360계
  float rotation;
  //y축 기울기
  float lean = 0;
  //잎맥의 두께
  float thick = 2.0;
  //폴리곤 단계
  int detail = 10;
  //가로 사이즈
  float wsize = 60.0;
  //세로 사이즈(픽셀 사이즈 아님)
  float hsize = 20.0;
  
  CloverLeaf(float x, float y, float z, float rotation, float lean) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.rotation = rotation;
    this.lean = lean;
  }
  
  void draw() {
    
    stroke(60);
    float dis = 0.0;
    float fx[] = new float[detail];//point of stream
    float fy[] = new float[detail];//point of stream
    float fz[] = new float[detail];
    float leafMac = thick;
    
    pushMatrix();
    translate(x, y, z);
    rotateY(rotation * PI / 180.0);
    
    fill(0xff71C570);
    
    //center 잎맥
    beginShape(QUAD_STRIP);
    for(int a=0; a < detail; a++) 
    {
      dis += hsize * sin(PI * (float(a) / detail));
      vertex(-leafMac, 0, dis);
      vertex(leafMac, 0, dis);
      leafMac *= 0.75f;
      if(leafMac < 0.5) {
        leafMac = 0;
      }
      fx[a] = leafMac;
      fz[a] = dis;
      
    }
    endShape();
    
    fill(0xffBBF1A3);
   
    //right 잎
    beginShape(QUAD_STRIP);
    for(int a=0; a < detail; a++) 
    {
      float x = wsize * sin(PI * (float(a) / detail));//0~PI 까지의 사인값을 이용해 둥글게 외곽점을 잡는다.
      vertex(fx[a], 0, fz[a]);
      vertex(x, 0, fz[a]);
    }
    endShape();
    
    //left 잎
    
    beginShape(QUAD_STRIP);
    for(int a=0; a < detail; a++) 
    {
      float x = wsize * sin(PI * (float(a) / detail));
      vertex(-fx[a], 0, fz[a]);
      vertex(-x, 0, fz[a]);
    }
    endShape();
    
    popMatrix();
  }
}





댓글0