/** * Texture Sphere * by Gillian Ramsay * * Rewritten by Gillian Ramsay to better display the poles. * Previous version by Mike 'Flux' Chang (and cleaned up by Aaron Koblin). * Original based on code by Toxi. * * A 3D textured sphere with simple rotation control. */ int ptsW, ptsH; PImage img; int numPointsW; int numPointsH_2pi; int numPointsH; float[] coorX; float[] coorY; float[] coorZ; float[] multXZ; void setup() { size(640, 360, P3D); background(0); noStroke(); img=loadImage("world32k.jpg"); ptsW=30; ptsH=30; // Parameters below are the number of vertices around the width and height initializeSphere(ptsW, ptsH); } // Use arrow keys to change detail settings void keyPressed() { if (keyCode == ENTER) saveFrame(); if (keyCode == UP) ptsH++; if (keyCode == DOWN) ptsH--; if (keyCode == LEFT) ptsW--; if (keyCode == RIGHT) ptsW++; if (ptsW == 0) ptsW = 1; if (ptsH == 0) ptsH = 2; // Parameters below are the number of vertices around the width and height initializeSphere(ptsW, ptsH); } void draw() { background(0); camera(width/2+map(mouseX, 0, width, -2*width, 2*width), height/2+map(mouseY, 0, height, -height, height), height/2/tan(PI*30.0 / 180.0), width, height/2.0, 0, 0, 1, 0); pushMatrix(); translate(width/2, height/2, 0); textureSphere(200, 200, 200, img); popMatrix(); } void initializeSphere(int numPtsW, int numPtsH_2pi) { // The number of points around the width and height numPointsW=numPtsW+1; numPointsH_2pi=numPtsH_2pi; // How many actual pts around the sphere (not just from top to bottom) numPointsH=ceil((float)numPointsH_2pi/2)+1; // How many pts from top to bottom (abs(....) b/c of the possibility of an odd numPointsH_2pi) coorX=new float[numPointsW]; // All the x-coor in a horizontal circle radius 1 coorY=new float[numPointsH]; // All the y-coor in a vertical circle radius 1 coorZ=new float[numPointsW]; // All the z-coor in a horizontal circle radius 1 multXZ=new float[numPointsH]; // The radius of each horizontal circle (that you will multiply with coorX and coorZ) for (int i=0; i