You weren't using popMatrix() and pushMatrix();
You intended to start from 0,0,0 and jump out to a point on the sphere. But because you didn't clear the translation from memory and put yourself back at 0,0,0 on the next iteration you proceeded onwards from the edge of the sphere to some ungodly co-ords.
The following code works, it looks quite pretty, well done.
Quote:
Circle c;
void setup() {
framerate(40);
size(300,300,P3D);
background(255);
c = new Circle();
c.p = 75;
}
void draw() {
translate(width/2,height/2,100);
for (float p = 0; p<TWO_PI; p += PI/10) {
c.phi = p;
for (float t = 0; t<TWO_PI; t += PI/10) {
c.theta = t;
c.display();
}
}
}
class Circle {
float p,theta,phi;
float x,y,z;
Circle() {
p = 0;
theta = 0;
phi = 0;
x = 0;
y = 0;
z = 0;
}
void display() {
SphericalToCartesian();
noStroke();
fill(50);
pushMatrix();
translate(x,y,z);
sphere(3);
popMatrix();
}
void SphericalToCartesian() {
z = p*sin(phi)*cos(theta);
x = p*sin(phi)*sin(theta);
y = p*cos(phi);
}
}
A cheap fix for the extra spheres at the polar caps would be to reduce the iterations. Another method might be to look at the source code for Processing's sphere() method. I tried looking myself on dev.processing.org and I've no idea where they keep the bloody thing. Good luck.