hehe.. i'm plundering with the same thing.. faking strokeweight in 3d, right
using this i've gotten pretty far:
link Code:
/********************************************************/
/* Written by John Gilbertson, 19/04/05 ( www.hardcorepawn.com )
/* Dodgy hack for Z value, seems to work wrong way round for my maths. See note in code.
/********************************************************/
import processing.opengl.*;
tube[] T;
int detail = 14;
void setup()
{
size(800,600,OPENGL);
noStroke();
T=new tube[0];
float x = 0, y = 0, z = 0, r = 5, px, py, pz, pr;
for(int i=0;i<10;i++)
{
//makes a rather cubic set of tubes, instead of spherical...
px = x;
py = y;
pz = z;
pr = r;
x = random(-(height/2),height/2);
y = random(-(height/2),height/2);
z = random(-(height/2),height/2);
r = random(15);
T = append(T,new tube(new Vertex(px,py,pz), new Vertex(x,y,z), pr, r, detail, color(random(255),random(255),random(255)) ));
}
}
void draw()
{
background(255);
translate(width/2,height/2,-100);
rotateY((float)(mouseX/TWO_PI));
fill(0,0,255);
sphere(30);
for(int i=0;i<T.length;i++)
{
T[i].draw();
}
}
//simple holder for 3 values, saves keeping track of 3 times as many values.
class Vertex
{
float x,y,z;
Vertex(float _x, float _y, float _z)
{
x=_x;
y=_y;
z=_z;
}
}
//Same as above, but could potnetially in future have different methods.
class Vector
{
float x,y,z;
Vector(float _x, float _y, float _z)
{
x=_x;
y=_y;
z=_z;
}
}
//4 sided poly, again, saves us having to keep track of 12 floats (4 corners, 3 values per vertex...)
class Quad
{
Vertex[] v;
color col;
Quad(Vertex[] _v, color _col)
{
v=_v;
col=_col;
}
Quad(Vertex a, Vertex b, Vertex c, Vertex d, color _col)
{
v=new Vertex[4];
v[0]=a;
v[1]=b;
v[2]=c;
v[3]=d;
col=_col;
}
void draw()
{
beginShape(QUADS);
fill(col);
{
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();
}
}
//Simple tube, currently fixed radius, could be tapered fairly easily though.
//Provide 2 verticies for the middle of the ends of the tube, a radius, and a colour.
class tube
{
Vertex root, end;
Quad[] polys;
float r1,r2;
int detail;
color col;
tube(float x1, float y1, float z1, float x2, float y2, float z2, float _r1, float _r2, int _detail, color c)
{
new tube(new Vertex(x1,y1,z1),new Vertex(x2,y2,z2), _r1, _r2, _detail, c);
}
//calculate all the poly's now, instead of in draw(), MASSIVE speedup.
tube(Vertex _root, Vertex _end, float _r1, float _r2, int _detail, color c)
{
polys=new Quad[_detail];
root=_root;
end=_end;
r1=_r1;
r2=_r2;
detail=_detail;
col=c;
compile();
}
void setEndPoint()
{
}
void compile()
{
//set up some useful values for later.
float len=sqrt((end.x-root.x) * (end.x-root.x) + (end.y-root.y) * (end.y-root.y) + (end.z-root.z) * (end.z-root.z))/2;
Vector vec=new Vector((end.x-root.x),(end.y-root.y),(end.z-root.z));
Vertex mid=new Vertex((root.x+end.x)/2,(root.y+end.y)/2,((root.z+end.z)/2));
float heading=-atan2(vec.z,vec.x);
float pitch=atan2(vec.y,sqrt((vec.x*vec.x)+(vec.z*vec.z)));
//tube vertex coords worked out by first generating a horizontal tube, of the right length, and then
//rotating, and moving it until it's in the right place.
for(int j=0;j<detail;j++)
{
// rod is horizontal along X to start
Vertex[] a=new Vertex[4];
float inc = TWO_PI/detail;
a[0]=new Vertex(len,r1*cos(j*inc),r1*sin(j*inc));
a[1]=new Vertex(-len,r2*cos(j*inc),r2*sin(j*inc));
a[2]=new Vertex(-len,r2*cos((j+1)*inc),r2*sin((j+1)*inc));
a[3]=new Vertex(len,r1*cos((j+1)*inc),r1*sin((j+1)*inc));
//pitch, then heading
for(int i=0;i<a.length;i++)
{
float _x=a[i].x;
float _y=a[i].y;
a[i].x=_x*cos(pitch)+_y*cos(pitch+HALF_PI);
a[i].y=_x*sin(pitch)+_y*sin(pitch+HALF_PI);
}
for(int i=0;i<a.length;i++)
{
float _z=a[i].z;
float _x=a[i].x;
a[i].z=_z*cos(heading)+_x*cos(heading+HALF_PI);
a[i].x=_z*sin(heading)+_x*sin(heading+HALF_PI);
}
//move tube so that it goes from one end to the other.
for(int i=0;i<a.length;i++)
{
a[i].x+=mid.x;
a[i].y+=mid.y;
a[i].z+=mid.z;
}
polys[j]=new Quad(a[0],a[1],a[2],a[3],col);
}
}
//does exactly what it says.
void draw()
{
for(int i=0;i<polys.length;i++)
{
polys[i].draw();
}
}
}
// appends an array with a given tube
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;
}