2010年7月23日 星期五

sincosFORM (Processing 3D )

Video Inside
video
colorful one
video
white clean one

使用Processing中的3D功能
簡單的運用Box function去製作的造形
box(x);這個function當中
的x為方體的大小值

而位置我是使用translate()的function
使得原點不斷變換位置
而在變換原點位製的同時也製作方盒

Trying to use the BOX function in Processing
In the box(x); the x is the value of the box size.
The way we populate is using the "translate" function
to translate the ZERO point 
and populate the box at the same time

在Processing裡面你只要將void setup
裡面的SIZE後面加上P3D即可變成3D的圖形
像是size(300,300,P3D);
攝影機的控制可以使用Example裡面的範例去改變

In Processing code, you just need to add "P3D"
in the size, then you will have 3D environment, 
just like size(300,300,P3D);
For the camera part, you could find out some example
from the EXAMPLE file

如同用點畫Sin或是Cos弧形一樣
只是這次用BOX取代點
當我們畫BOX的時候也
會依中軸的X, Y, Z三個方向旋轉
(也可以只選轉一或兩個軸)
因此他不會只限於畫在2D中

Just as simple as you draw sin or cos curves by points,
but this time we use Boxes replace the points.
When we populate the boxes,
we alse rotate the X, Y, Z Axis
so it won't only draw in 2D
(you can choose to rotate any
one or two of the three axis)

這邊還有一個非常重要的function
叫做
pushMatrix跟popMatrix
他們是一對的有push就要有pop
他像是你在裡面自己有一個遊戲規則
當跳出之後就不成立
因為當我們使用translate這個功能
他會依照你上一個移動到的原點位置去做之後的計算
如果使用
pushMatrix跟popMatrix
將translate包在裡面的話
則跳出後原點會回到
(0,0,0)的位置
後篇再舉例說明
或參看此

In our code, there are two important function called
"popMatrix" and "pushMatrix" 
It allow the rules you set only happen inside the  
"popMatrix" and "pushMatrix."
When we use "translate",
it will always coculate the zero point base on the 
change of the translation.
Once you use  
"popMatrix" and "pushMatrix",
it will only translate the zero point between
"popMatrix" and "pushMatrix",
and will change back to the (0,0,0)
out of the
"popMatrix" and "pushMatrix",
chech here for more detail:

在程式中
d1, d2, s1, r1是可以改變的數字
將造成結果的不同
同樣的你也可以改變spherecubes()
功能裡面translate的x,y,z點的sin cos
亦可改變形狀

in the code
the number of d1, d2, s1, r1 
are changable for populating differnt forms,
and also you can change the "sin" and "cos" function
in the "spherecubes" to have variable form

當你按下"R"鍵盤的時候
程式會幫你自動匯出DWG到你的檔案夾裡面
When you press "R",
it will export the DWG file for you
in your folder

以下就是不同的變數所產生的不同圖形
the image below are how we populate by different variable


紅色表是可改的地方
程式如下
the code in red means you can try to change
the program:
import processing.dxf.*;
boolean record = false;

float d1 =5;
float s1 =100;
float r1 = 2;
float d2 = 50;

void setup() {
size(700, 700, P3D);
background(0);
noStroke();
//noLoop();
}

void draw() {
if (record == true) {
beginRaw(DXF, "output.dxf"); // Start recording to the file
}
int r =100;
int s =100;
lights();
background(0);
translate(width/2, height/2, -mouseX/2);
rotateZ(map(mouseY*2, 10, height, 10, PI));
rotateY(map(mouseX*2, 10, width, 10, PI));

for(int i=0; i<360; i+=d1){
pushMatrix();
translate(0,0,0);
rotateX(PI*i/360);
rotateY(PI*i/360);
rotateZ(PI*i/360);

//spherecubes(mouseX-s1);
spherecubes(mouseX/2-i);
// diferrent mode to change
popMatrix();

}

if (record == true) {
endRaw();
record = false; // Stop recording to the file
}
}

void keyPressed() {
if (key == 'R'
key == 'r') { // Press R to save the file
record = true;
}
}


void spherecubes(float r){
for (float x = 0; x < r1*PI; x+=PI/d2) {
pushMatrix();
translate(r*sin(x), r*cos(x), r*sin(x));
// you can try the put the sin cos in different places 
//fill(random(0,255),random(0,255),random(0,255));
//the random color
//sphere(3*x);
box(x*5);
popMatrix();
}
}

沒有留言:

張貼留言