We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › 3D Struts - defined by endpoints
Page Index Toggle Pages: 1
3D Struts - defined by endpoints? (Read 1087 times)
3D Struts - defined by endpoints?
Apr 3rd, 2006, 11:25pm
 
Hello,

I was wondering if anyone had some code around for a 3d strut or line (or stick, rod, etc), which could be a box or a cylinder.  The thing I need, is to define this shape based on its arbitrary 3D endpoints, and not by the standard base/height method.

Yes I could make one by taking the linear segment endpoints, cross product them to make a small end plane, etc.  But I dont want to reinvent the wheel, if possible.

Hope this makes sense!  Thanks guys!
Re: 3D Struts - defined by endpoints?
Reply #1 - Apr 4th, 2006, 12:30am
 
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;
}

Re: 3D Struts - defined by endpoints?
Reply #2 - Apr 4th, 2006, 12:33am
 
The code above is what i have written this far..
I was thinking perhaps making a function which connects one tube to another, but it's a bit over my head right now Tongue
let me know if you can add anything

-seltar
Re: 3D Struts - defined by endpoints?
Reply #3 - Apr 4th, 2006, 1:14am
 
Alright, nice work!  That looks like pretty well developed code, just like what I was talking about. I'll check it out!  

Yes one reason I want to do this is to get variable 'line' widths in 3D.  I want to port a program of mine from a couple years ago::  http://www.spacefillingcurve.net/programming/plantforms/


I have usually used linewidth() in opengl, but mostly out of time/performance concerns.  But with the fast machines of today I can probably rewrite some of my apps to use real geometry instead of just fat lines.  Should look cool...

Thanks man!  I'll keep you posted.
Re: 3D Struts - defined by endpoints?
Reply #4 - Apr 4th, 2006, 12:38pm
 
Nice to know that code I wrote is still being useful Smiley
Re: 3D Struts - defined by endpoints?
Reply #5 - Apr 4th, 2006, 1:13pm
 
I know Grin thanks for sharing! Smiley
Page Index Toggle Pages: 1