I thought that the first thing I should try out with this fantastic shiny BETA version should be the funky OpenGL integration.
And I'd just like to say, it's great.
I've not played with it that much, but it's looking good already.
I knocked this together this evening, in a couple of hours, partly to test out the OpenGL, and partly to get my brain back into working iwth 3D maths etc, and let me tell you I found out I was *really* rusty.
But here's the demo, can't put it up as an applet, sinc ethe OpenGL stuff won't work via a browser, so you'll have ot copy and paste the code into Processing.
EDIT: added spaces to code to try to fix problems caused by long lines. /EDIT
Code:
import processing.opengl.*;
/********************************************************/
/* 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.
/********************************************************/
//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 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 r;
color col;
//calculate all the poly's now, instead of in draw(), MASSIVE speedup.
tube(Vertex _root, Vertex _end, float _r,color c)
{
polys=new Quad[8];
root=_root;
end=_end;
r=_r;
col=c;
//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));
len/=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);
//HACK.. but seems necessary.. probably balances bug in code below....
heading=0-heading;
//ENDHACK
float pitch=atan2(vec.y,sqrt((vec.x*vec.x)+(vec.z*vec.z)));
//can probably merge all 4 stages into one.
//they'll be some really long lines though.
//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<8;j++)
{
// rod is horizontal along X to start
Vertex[] a=new Vertex[4];
a[0]=new Vertex(len,r*cos(j*(PI/4)),r*sin(j*PI/4));
a[1]=new Vertex(-len,r*cos(j*(PI/4)),r*sin(j*PI/4));
a[2]=new Vertex(-len,r*cos((j+1)*(PI/4)),r*sin((j+1)*PI/4));
a[3]=new Vertex(len,r*cos((j+1)*(PI/4)),r*sin((j+1)*PI/4));
//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();
}
}
}
tube[] T;
float a;
void setup()
{
size(800,600,OPENGL);
noStroke();
T=new tube[200];
for(int i=0;i<200;i++)
{
//makes a rather cubic set of tubes, instead of spherical...
T[i]=new tube(new Vertex(0,0,0),new Vertex(random(-(height/2),height/2), random(-(height/2),height/2), random(-(height/2),height/2)), random(15), color(random(255),random(255),random(255)) );
}
a=0;
}
void draw()
{
background(255);
a+=0.01;
translate(width/2,height/2,-100);
spotLight(255,255,255,200,0,200,-1,0,-1,PI/16.0,100);
rotateY(a);
fill(0,0,255);
sphere(30);
for(int i=0;i<T.length;i++)
{
T[i].draw();
}
}