Draw a sphere: Difference between revisions
Content added Content deleted
Line 3,318: | Line 3,318: | ||
// draw sphere |
// draw sphere |
||
sphere(200); |
sphere(200); |
||
}</lang> |
|||
A sphere build from triangles (to for instance allow distortion) |
|||
<lang>float rotX, rotY; |
|||
PVector[][] sphere; |
|||
int detail = 50; |
|||
void setup() { |
|||
size(600, 600, P3D); |
|||
background(50, 50, 200); |
|||
fill(255, 0, 200); |
|||
stroke(0, 40); |
|||
sphere = new PVector[detail+1][detail+1]; |
|||
} |
|||
void draw() { |
|||
pointLight(255, 255, 255, -width, -height, 2*height); |
|||
translate(width/2, height/2); |
|||
rotateX(rotX); |
|||
rotateY(rotY); |
|||
float r = 200; |
|||
for (int i = 0; i <= detail; i++) { |
|||
float rows = map(i, 0, detail, 0, PI); |
|||
for (int j = 0; j <= detail; j++) { |
|||
float columns = map(j, 0, detail, 0, TWO_PI); |
|||
float x = r * sin(rows) * cos(columns); |
|||
float y = r * sin(rows) * sin(columns); |
|||
float z = r * cos(rows); |
|||
sphere[i][j] = new PVector(x, y, z); |
|||
} |
|||
} |
|||
for (int i = 0; i < detail; i++) { |
|||
beginShape(TRIANGLE_STRIP); |
|||
for (int j = 0; j <= detail; j++) { |
|||
PVector v1 = sphere[i][j]; |
|||
vertex(v1.x, v1.y, v1.z); |
|||
PVector v2 = sphere[i+1][j]; |
|||
vertex(v2.x, v2.y, v2.z); |
|||
} |
|||
endShape(); |
|||
} |
|||
} |
|||
void mouseDragged() { |
|||
rotY -= (mouseX - pmouseX) * 0.01; |
|||
rotX -= (mouseY - pmouseY) * 0.01; |
|||
}</lang> |
}</lang> |
||