processing/processing-web-archive

The Texture Sphere Example-reducing to 2/3 of original code

heyilovepie opened this issue · 2 comments

The Texture Sphere at: http://processing.org/examples/texturesphere.html
can be reduced by a 3rd (55 lines of code).
Mine also added separate detail values for the width and height and separate values for the radii of all axes. It makes making a variety of shapes a lot easier. You can change the numPoints to make pyramids (3,3), cones(30,3), I really don't know what to call (30,3), diamond shaped "jewels" (4,4), cupcakes (30,5), and spheres (30,30). And then squish them or distort them by changing the radius.

float eyeX, eyeY, eyeZ;
int ptsW, ptsH;

PImage img;

int numPointsW;
int numPointsH_2pi;
int numPointsH;

float[] coorX;
float[] coorY;
float[] coorZ;
float[] multXZ;

void setup() {
size(500, 500, P3D);
background(0);
noStroke();
img=loadImage("texture_1.tif");
eyeX=width/2;
eyeY=height/2;
eyeZ=height/2/tan(PI*30.0 / 180.0);
ptsW=30;
ptsH=30;
initializeSphere(ptsW, ptsH); //parameters are the number of verticies around the width and height
}
void keyPressed(){
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;
initializeSphere(ptsW, ptsH); //parameters are the number of vertices around the width and height
}
void draw() {
background(0);
camera(eyeX+map(mouseX, 0, width, -width, width), eyeY+map(mouseY, 0, height, -height, height), eyeZ, width/2.0, height/2.0, 0, 0, 1, 0);

pushMatrix();
translate(width/2, height/2, 0);
textureSphere(150, 150, 150, img);
popMatrix();
}
void initializeSphere(int numPtsW, int numPtsH_2pi) {

//the number of points around the width and height
numPointsW=numPtsW;
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<numPointsW ;i++) { //for all the points around the width
float thetaW=i_2_PI/(numPointsW);
coorX[i]=sin(thetaW);
coorZ[i]=cos(thetaW);
}
for (int i=0; i<numPointsH; i++) { //for all points from top to bottom
if (int(numPointsH_2pi/2) != (float)numPointsH_2pi/2 && i==numPointsH-1) { //if the numPointsH_2pi is odd and it is at the last pt
float thetaH=(i-1)_2_PI/(numPointsH_2pi);
coorY[i]=cos(PI+thetaH);
multXZ[i]=0;
} else {
//the numPointsH_2pi and 2* below allows there to be a flat bottom if the numPointsH is odd
float thetaH=i_2_PI/(numPointsH_2pi);
//PI+ below makes the top always the point instead of the bottom.
coorY[i]=cos(PI+thetaH);
multXZ[i]=sin(thetaH);
}
}
}
void textureSphere(float rx, float ry, float rz, PImage t) {
//these are so we can map certain parts of the image on to the shape
float changeU=t.width/numPointsW;
float changeV=t.height/numPointsH;
float u=0; //width variable for the texture
float v=0; //height variable for the texture

beginShape(TRIANGLE_STRIP);
texture(t);
for (int i=0; i<numPointsH-1; i++) { //for all the rings
for (int j=0; j<numPointsW; j++) { //for all the pts in the ring
vertex(coorX[j]_multXZ[i]_rx, coorY[i]_ry, coorZ[j]_multXZ[i]_rz, u, v);
vertex(coorX[j]_multXZ[i+1]_rx, coorY[i+1]_ry, coorZ[j]_multXZ[i+1]_rz, u, v+changeV);
u+=changeU;
}
vertex(coorX[0]_multXZ[i]_rx, coorY[i]_ry, coorZ[0]_multXZ[i]_rz, u, v); //close off the ring
vertex(coorX[0]_multXZ[i+1]_rx, coorY[i+1]_ry, coorZ[0]_multXZ[i+1]_rz, u, v+changeV);
endShape();
v+=changeV;
u=0;
}
}

REAS commented

Thank you for the report. This repository is an archive and we're not actively fixing bugs here. Please post to processing/processing-docs. How did you find your way here?

REAS commented

Ah, you probably found your way here through the links at the top of the examples or reference on the website. I'm updating those now...