Help With Final!- Changing Textures on 3D Sphere
in
Programming Questions
•
10 months ago
Hi all, first time posting here and relatively new to Processing.
So I'm attempting to create a project in which I have a 3D sphere, and the texture of the sphere changes whenever a key is pressed. The sphere is able to be fully rotated. I know to make some sort of array in order to store the images, yet I do not know how to fully execute the change in textures. Would anyone be able to help me out on this? My code is based on this example:
http://www.processing.org/discourse/beta/num_1274893475.html
Here's what I have so far in terms of code:
//Image Array
Photo[] photoarray;
photoarray[0] = loadImage("0.jpg");
photoarray[1] = loadImage("1.jpg");
photoarray[2] = loadImage("2.jpg");
photoarray[3] = loadImage("3.jpg");
photoarray[4] = loadImage("4.jpg");
photoarray[5] = loadImage("5.jpg";
photoarray[6] = loadImage("6.jpg");
photoarray[7] = loadImage("7.jpg");
photoarray[8] = loadImage("8.jpg");
photoarray[9] = loadImage("9.jpg");
// Sphere Variables
float R = 250;
int xDetail = 40;
int yDetail = 30;
float[] xGrid = new float[xDetail+1];
float[] yGrid = new float[yDetail+1];
float[][][] allPoints = new float[xDetail+1][yDetail+1][3];
// Rotation Variables
float camDistance = -50;
float rotationX = 100;
float rotationY = 170;
float velocityX = 0;
float velocityY = 0;
// Texture
PImage texmap;
//Image Array
////////////////////////////////////////////////////////////////////////
void setup(){
size(700, 700, P3D);
noStroke();
}
////////////////////////////////////////////////////////////////////////
void draw(){
background(0);
translate(width/2.0, height/2.0, camDistance);
rotateX( radians(-rotationX) );
rotateZ( radians(270 - rotationY) );
drawSphere(texmap);
texmap = photoarray[i];
photoarray[i] = loadImage(i+".jpg");
// Implements mouse control (interaction will be inverse when sphere is upside down)
rotationX += velocityX;
rotationY += velocityY;
velocityX *= 0.95;
velocityY *= 0.95;
if(mousePressed){
velocityX += (mouseY-pmouseY) * 0.01;
velocityY -= (mouseX-pmouseX) * 0.01;
}
}
////////////////////////////////////////////////////////////////////////
void keyPressed() {
for (int i =0; i< 10; i++) {
}
if (i > 9) {
int i = 0;
}
}
void setupSphere(float R, int xDetail, int yDetail){
// Create a 2D grid of standardized mercator coordinates
for(int i = 0; i <= xDetail; i++){
xGrid[i]= i / (float) xDetail;
}
for(int i = 0; i <= yDetail; i++){
yGrid[i]= i / (float) yDetail;
}
textureMode(NORMALIZED);
// Transform the 2D grid into a grid of points on the sphere, using the inverse mercator projection
for(int i = 0; i <= xDetail; i++){
for(int j = 0; j <= yDetail; j++){
allPoints[i][j] = mercatorPoint(R, xGrid[i], yGrid[j]);
}
}
}
////////////////////////////////////////////////////////////////////////
float[] mercatorPoint(float R, float x, float y){
float[] thisPoint = new float[3];
float phi = x*2*PI;
float theta = PI - y*PI;
thisPoint[0] = R*sin(theta)*cos(phi);
thisPoint[1] = R*sin(theta)*sin(phi);
thisPoint[2] = R*cos(theta);
return thisPoint;
}
////////////////////////////////////////////////////////////////////////
void drawSphere(PImage Map){
for(int j = 0; j < yDetail; j++){
beginShape(TRIANGLE_STRIP);
texture(Map);
for(int i = 0; i <= xDetail; i++){
vertex(allPoints[i][j+1][0], allPoints[i][j+1][1], allPoints[i][j+1][2], xGrid[i], yGrid[j+1]);
vertex(allPoints[i][j][0], allPoints[i][j][1], allPoints[i][j][2], xGrid[i], yGrid[j]);
}
endShape(CLOSE);
}
}
class Photo {
Photo(String num) {
texmap = loadImage(num);
}
void drawPhoto() {
image(pic,0,0);
}
}
1