import processing.opengl.*;
import processing.dxf.*;
boolean record = false;
tube[] T;
int steps=17;
int segments = 7;
float x, y, z, px, py, pz,px1,py1,pz1,px2,py2,pz2;
Beach beach;
float angle;
float angleZ;
float scala=25.0;
float tr=0.75;
int turnC=1;
int tCount=0;
int swi=steps/2;//round(random(0,10));
void setup(){
//size(3307,2339,OPENGL);
//size(800,600,OPENGL);
size(1280,800,OPENGL);
smooth();
T=new tube[0];
beach= new Beach();
beach.makeB();
}
void draw(){
if (record == true) {
beginRaw(DXF, “output.dxf”);
}
//saveFrame(“ani/prev-####.png”);
if(keyPressed&&key==’o'||key==’O'){
saveFrame(“screens/screen-####.png”);
}
background(255);
lights();
if(keyPressed&&key==’q'||key==’Q'){
scala+=0.4;
}
if(keyPressed&&key==’a'||key==’A'){
scala-=0.4;
}
if(keyPressed&&key==’w'||key==’W'){
tr-=0.004;
}
if(keyPressed&&key==’s’||key==’S'){
tr+=0.004;
}
if(keyPressed&&key==’e'||key==’E'){
angleZ += 0.02;
}
if(keyPressed&&key==’d'||key==’D'){
angleZ -= 0.02;
}
if(keyPressed&&key==’y'||key==’Y'){
if(swi>0){
swi–;
}
}
if(keyPressed&&key==’u'||key==’U'){
if(swi<steps){
swi++;
}
}
if(mousePressed&&mouseButton==LEFT){
angle -= 0.03;
}
if(mousePressed&&mouseButton==RIGHT){
angle += 0.03;
}
if(keyPressed&&key==’h'||key==’H'){
px = random(-5,5);
py =random(-5,2);
pz = random(-5,5);
px1=px+random(-2,2);
py1=py+random(-2,2);
pz1=pz+random(-2,2);
px2=px1+random(-2,2);
py2=py1+random(-2,2);
pz2=pz1+random(-2,2);
x =px2+ random(-5,5);
y =py2+ random(-5,5);
z =pz2+ random(-5,5);
T = append(T,new tube(new Vector(px,py,pz),new Vector(px1,py1,pz1),new Vector(px2,py2,pz2), new Vector(x,y,z), color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
if(keyPressed&&key==’j'||key==’J'){
if (tCount>0){
T = append(T,new tube(T[tCount-1].giveProfile(turnC)[0],T[tCount-1].giveProfile(turnC)[1],T[tCount-1].giveProfile(turnC)[2],T[tCount-1].giveProfile(turnC)[3], color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
}
if(keyPressed&&key==’k'||key==’K'){
if (tCount>0){
x = T[tCount-1].giveProfile(turnC)[3].x+random(-1,1);
y = T[tCount-1].giveProfile(turnC)[3].y+random(-2,0);
z = T[tCount-1].giveProfile(turnC)[3].z+random(-1,1);
T = append(T,new tube(T[tCount-1].giveProfile(turnC)[1],T[tCount-1].giveProfile(turnC)[2],T[tCount-1].giveProfile(turnC)[3], new Vector(x,y,z), color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
}
if(keyPressed&&key==’l'||key==’L'){
if (tCount>0){
T = append(T,new tube(T[tCount-1].giveProfile(turnC)[0],T[tCount-1].giveProfile(turnC)[3],T[tCount-1].giveProfile(), color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
}
translate(width/2, height*tr, 0);
scale(scala);
rotateY(angle);
rotateX(angleZ);
// beach.displayB();
for(int i=0;i<T.length;i++)
{
T[i].display();
}
if (record == true) {
endRaw();
record = false;
}
}
void keyPressed() {
if (key == ‘R’ || key == ‘r’) {
record = true;
}
}
float fuzzyDist(float dis, float ix, float iy){
float result=0;
float d=dis;
//range
float u1=ix;
float u2=iy;
if(d>=u1){
result=0;
}
else if(d<=u2){
result=1;
}
else{
result=(d /(u2-u1))-(u1/(u2-u1));
}
return result;
}
class Beach{
int det=100;
float[][][]coordB =new float[det][det][3];
void makeB(){
for (int i=0;i<det;i++){
for (int j=0;j<det;j++){
coordB[i][j][0]=-2*det+i*4;
coordB[i][j][1]=0;
coordB[i][j][2]=-2*det+j*4;
}
}
}
void displayB(){
noStroke();
fill(230);
strokeWeight(1);
beginShape(TRIANGLES);
for (int i=1;i<det;i++){
for (int j=1;j<det;j++){
//line(coord[i][j][0],coord[i][j][1],coord[i][j][2],coord[i][j][0]+0.1,coord[i][j][1]+0.1,coord[i][j][2]+0.1);
vertex(coordB[i][j][0],coordB[i][j][1],coordB[i][j][2]);
vertex(coordB[i][j-1][0],coordB[i][j-1][1],coordB[i][j-1][2]);
vertex(coordB[i-1][j][0],coordB[i-1][j][1],coordB[i-1][j][2]);
vertex(coordB[i][j-1][0],coordB[i][j-1][1],coordB[i][j-1][2]);
vertex(coordB[i-1][j][0],coordB[i-1][j][1],coordB[i-1][j][2]);
vertex(coordB[i-1][j-1][0],coordB[i-1][j-1][1],coordB[i-1][j-1][2]);
}
}
endShape();
}
}
class Quad
{
Vector[] v;
color col;
Quad(Vector[] _v, color _col)
{
v=_v;
col=_col;
}
Quad(Vector a, Vector b, Vector c, Vector d, color _col)
{
v=new Vector[4];
v[0]=a;
v[1]=b;
v[2]=c;
v[3]=d;
col=_col;
}
void draw()
{
beginShape(QUADS);
fill(col,85);
stroke(40);
//noStroke();
{
vertex(v[0].x,v[0].y,v[0].z);
vertex(v[1].x,v[1].y,v[1].z);
vertex(v[2].x,v[2].y,v[2].z);
vertex(v[3].x,v[3].y,v[3].z);
}
endShape();
}
}
class tube
{
Vector root, end, mid1, mid2, roott,eend;
Vector[][] a=new Vector[steps+1][4];
Quad[][] polys;
float r1,r2,p;
int crvMidPtId1=round(steps/3);
int crvMidPtId2=round(2*steps/3);
float time=0.0;
color col;
float bpX[]=new float[steps+1];
float bpY[]=new float[steps+1];
float bpZ[]=new float[steps+1];
tube(Vector _root,Vector _mid1,Vector _mid2, Vector _end, color c){
polys=new Quad[steps][segments];
root=_root;
end=_end;
mid1=_mid1;
mid2=_mid2;
col=c;
giveCurves();
compile();
blow();
}
tube(Vector _root,Vector _end,Vector[] _edge, color c){
polys=new Quad[steps+1][segments];
Vector[] edge=_edge;
roott=_root;
eend=_end;
root=edge[0];
mid1=edge[crvMidPtId1];
mid2=edge[crvMidPtId2];
end=edge[steps-1];
col=c;
giveCurves(edge);
compile();
blow();
}
void giveCurves(){
for (int u=0;u<steps;u++){
float p = u / float(steps);
bpX[u]=bezierPoint(root.x,mid1.x,mid2.x,end.x,p);
bpY[u]=bezierPoint(root.y,mid1.y,mid2.y,end.y,p);
bpZ[u]=bezierPoint(root.z,mid1.z,mid2.z,end.z,p);
}
}
void giveCurves(Vector[] _edge){
Vector[] edge=_edge;
for (int u=0;u<steps;u++){
bpX[u]=edge[u].x;
bpY[u]=edge[u].y;
bpZ[u]=edge[u].z;
}
}
void compile()
{
for(int j=0;j<segments;j++)
{
for (int u=0;u<steps;u++){
float len1,len2;
Vector vec1,vec2,mid1,mid2;
float edst=end.distance(end,root)/2;
r1=0.05+0.2*fuzzyDist(dist(bpX[u],bpY[u],bpZ[u],bpX[swi],bpY[swi],bpZ[swi]),edst,0.01);
r2=0.05+0.2*fuzzyDist(dist(bpX[u+1],bpY[u+1],bpZ[u+1],bpX[swi],bpY[swi],bpZ[swi]),edst,0.01);
len1=sqrt((bpX[u]-bpX[u+1]) * (bpX[u]-bpX[u+1]) + (bpY[u]-bpY[u+1]) * (bpY[u]-bpY[u+1]) + (bpZ[u]-bpZ[u+1]) * (bpZ[u]-bpZ[u+1]))/2;
len2=sqrt((bpX[u+1]-end.x) * (bpX[u+1]-end.x) + (bpY[u+1]-end.y) * (bpY[u+1]-end.y) + (bpZ[u+1]-end.z) * (bpZ[u+1]-end.z))/2;
vec1=new Vector((bpX[u]-bpX[u+1]),(bpY[u]-bpY[u+1]),(bpZ[u]-bpZ[u+1]));
vec2=new Vector((bpX[u+1]-end.x),(bpY[u+1]-end.y),(bpZ[u+1]-end.z));
mid1=new Vector((bpX[u]+bpX[u+1])/2,(bpY[u]+bpY[u+1])/2,(bpZ[u]+bpZ[u+1])/2);
mid2=new Vector((bpX[u+1]+end.x)/2,(bpY[u+1]+end.y)/2,(bpZ[u+1]+end.z)/2);
if(u<steps-2){
len1=sqrt((bpX[u]-bpX[u+1]) * (bpX[u]-bpX[u+1]) + (bpY[u]-bpY[u+1]) * (bpY[u]-bpY[u+1]) + (bpZ[u]-bpZ[u+1]) * (bpZ[u]-bpZ[u+1]))/2;
len2=sqrt((bpX[u+1]-bpX[u+2]) * (bpX[u+1]-bpX[u+2]) + (bpY[u+1]-bpY[u+2]) * (bpY[u+1]-bpY[u+2]) + (bpZ[u+1]-bpZ[u+2]) * (bpZ[u+1]-bpZ[u+2]))/2;
vec1=new Vector((bpX[u]-bpX[u+1]),(bpY[u]-bpY[u+1]),(bpZ[u]-bpZ[u+1]));
vec2=new Vector((bpX[u+1]-bpX[u+2]),(bpY[u+1]-bpY[u+2]),(bpZ[u+1]-bpZ[u+2]));
mid1=new Vector((bpX[u]+bpX[u+1])/2,(bpY[u]+bpY[u+1])/2,(bpZ[u]+bpZ[u+1])/2);
mid2=new Vector((bpX[u+1]+bpX[u+2])/2,(bpY[u+1]+bpY[u+2])/2,(bpZ[u+1]+bpZ[u+2])/2);
}
float heading1=-atan2(vec1.z,vec1.x);
float heading2=-atan2(vec2.z,vec2.x);
float pitch1=atan2(vec1.y,sqrt((vec1.x*vec1.x)+(vec1.z*vec1.z)));
float pitch2=atan2(vec2.y,sqrt((vec2.x*vec2.x)+(vec2.z*vec2.z)));
float inc = TWO_PI/segments;
a[u][0]=new Vector(len1,r1*cos(j*inc),r1*sin(j*inc));
a[u][1]=new Vector(len2,r2*cos(j*inc),r2*sin(j*inc));
a[u][2]=new Vector(len2,r2*cos((j+1)*inc),r2*sin((j+1)*inc));
a[u][3]=new Vector(len1,r1*cos((j+1)*inc),r1*sin((j+1)*inc));
for(int i=0;i<a[u].length;i++)
{
if (i==1||i==2){
float _x=a[u][i].x;
float _y=a[u][i].y;
a[u][i].x=_x*cos(pitch2)+_y*cos(pitch2+HALF_PI);
a[u][i].y=_x*sin(pitch2)+_y*sin(pitch2+HALF_PI);
}
else{
float _x=a[u][i].x;
float _y=a[u][i].y;
a[u][i].x=_x*cos(pitch1)+_y*cos(pitch1+HALF_PI);
a[u][i].y=_x*sin(pitch1)+_y*sin(pitch1+HALF_PI);
}
}
for(int i=0;i<a[u].length;i++)
{
if (i==1||i==2){
float _z=a[u][i].z;
float _x=a[u][i].x;
a[u][i].z=_z*cos(heading2)+_x*cos(heading2+HALF_PI);
a[u][i].x=_z*sin(heading2)+_x*sin(heading2+HALF_PI);
}
else{
float _z=a[u][i].z;
float _x=a[u][i].x;
a[u][i].z=_z*cos(heading1)+_x*cos(heading1+HALF_PI);
a[u][i].x=_z*sin(heading1)+_x*sin(heading1+HALF_PI);
}
}
for(int i=0;i<a[u].length;i++)
{
if (i==1||i==2){
a[u][i].x+=mid2.x;
a[u][i].y+=mid2.y;
a[u][i].z+=mid2.z;
}
else{
a[u][i].x+=mid1.x;
a[u][i].y+=mid1.y;
a[u][i].z+=mid1.z;
}
}
polys[u][j]=new Quad(a[u][0],a[u][1],a[u][2],a[u][3],col);
}
}
}
void blow(){
Vector g= new Vector(0,0.01,0);
for(int j=0;j<segments;j++)
{
for (int u=0;u<steps;u++){
polys[u][j].v[0]=polys[u][j].v[0].add(polys[u][j].v[0],g);
polys[u][j].v[1]=polys[u][j].v[1].add(polys[u][j].v[1],g);
polys[u][j].v[2]=polys[u][j].v[2].add(polys[u][j].v[2],g);
polys[u][j].v[3]=polys[u][j].v[3].add(polys[u][j].v[3],g);
}
}
}
void display()
{
for(int i=0;i<steps-1;i++)
{
for(int j=0;j<segments;j++){
polys[i][j].draw();
}
}
}
Vector[] giveProfile(int par)
{
Vector[] profile=new Vector[4];
Vector add;
add =polys[crvMidPtId1][par].v[0].copy();
add.normalize();
add.mult(0.5);
profile[0]=root.add(add,root);
profile[1]=polys[crvMidPtId1][par].v[0].add(add,polys[crvMidPtId1][par].v[0]);
profile[2]=polys[crvMidPtId2][par].v[0].add(add,polys[crvMidPtId2][par].v[0]);
profile[3]=end.add(add,end);
return profile;
}
Vector[] giveProfile(){
Vector[] profile=new Vector[steps];
for (int i=0;i<steps;i++){
Vector off=new Vector(polys[i][1].v[0].x-bpX[i],polys[i][1].v[0].y-bpY[i],polys[i][1].v[0].z-bpZ[i]);
profile[i]=polys[i][1].v[0].add(polys[i][1].v[0],off);
}
return profile;
}
}
tube[] append(tube[] tarr, tube t)
{
tube[] tmp = new tube[tarr.length+1];
arraycopy(tarr,0,tmp,0,tarr.length);
tmp[tarr.length] = t;
return tmp;
}
static class Vector
{
float x,y,z;
Vector(float _x, float _y, float _z)
{
x=_x;
y=_y;
z=_z;
}
void setX(float x_) {
x = x_;
}
void setY(float y_) {
y = y_;
}
void setZ(float z_) {
z = z_;
}
void setXY(float x_, float y_) {
x = x_;
y = y_;
}
void setXYZ(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}
void setXYZ(Vector v) {
x = v.x;
y = v.y;
z = v.z;
}
float magnitude() {
return (float) Math.sqrt(x*x + y*y + z*z);
}
Vector copy() {
return new Vector(x,y,z);
}
Vector copy(Vector v) {
return new Vector(v.x, v.y,v.z);
}
void add(Vector v) {
x += v.x;
y += v.y;
z += v.z;
}
void sub(Vector v) {
x -= v.x;
y -= v.y;
z -= v.z;
}
void mult(float n) {
x *= n;
y *= n;
z *= n;
}
void div(float n) {
x /= n;
y /= n;
z /= n;
}
void normalize() {
float m = magnitude();
if (m > 0) {
div(m);
}
}
void limit(float max) {
if (magnitude() > max) {
normalize();
mult(max);
}
}
Vector limit(Vector v1,float max) {
if (v1.magnitude() > max) {
v1.normalize();
v1.mult(max);
}
return v1;
}
float heading2D() {
float angle = (float) Math.atan2(-y, x);
return -1*angle;
}
Vector add(Vector v1, Vector v2) {
Vector v = new Vector(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
return v;
}
Vector sub(Vector v1, Vector v2) {
Vector v = new Vector(v1.x – v2.x,v1.y – v2.y,v1.z – v2.z);
return v;
}
Vector div(Vector v1, float n) {
Vector v = new Vector(v1.x/n,v1.y/n,v1.z/n);
return v;
}
Vector mult(Vector v1, float n) {
Vector v = new Vector(v1.x*n,v1.y*n,v1.z*n);
return v;
}
float distance (Vector v1, Vector v2) {
float dx = v1.x – v2.x;
float dy = v1.y – v2.y;
float dz = v1.z – v2.z;
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
}
}
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
You must be logged in to post a comment.