Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ptsW, ptsH;
- PGraphics pgBump;
- PImage img;
- int numPointsW;
- int numPointsH_2pi;
- int numPointsH;
- float[] coorX;
- float[] coorY;
- float[] coorZ;
- float[] multXZ;
- float rot = 0, rotX = 0, rotY = 0;
- float drawRad = 10;
- int drawOp = 20;
- boolean keyDown = false;
- float lTween = 0.9;
- float uTween = 3.5;
- void setup() {
- size(640, 360, P3D);
- noStroke();
- loadBump();
- ptsW=120;
- ptsH=120;
- // Parameters below are the number of vertices around the width and height
- initializeSphere(ptsW, ptsH);
- }
- void loadBump() {
- try {
- img=loadImage("map.png");
- }
- finally {
- if (img == null) {
- img = createImage(width, height, RGB);
- for (int i = 0; i < img.pixels.length; i++) img.pixels[i] = color(130);
- }
- }
- pgBump = createGraphics(img.width, img.height, P2D);
- pgBump.beginDraw();
- pgBump.noStroke();
- pgBump.image(img, 0, 0);
- pgBump.endDraw();
- }
- void keyPressed() {
- keyDown = true;
- }
- void keyReleased() {
- keyDown = false;
- }
- void editBump() {
- pgBump.beginDraw();
- pgBump.image(img, 0, 0);
- if (mousePressed) {
- if (mouseButton == LEFT) pgBump.fill(255, drawOp);
- else pgBump.fill(0, drawOp);
- pgBump.ellipse(mouseX/(float)width * img.width, mouseY/(float)height * img.height, drawRad, drawRad);
- }
- pgBump.endDraw();
- img = pgBump.get();
- pgBump.beginDraw();
- pgBump.fill(255, 0, 0);
- pgBump.ellipse(mouseX/(float)width * img.width, mouseY/(float)height * img.height, drawRad, drawRad);
- pgBump.endDraw();
- }
- void hud() {
- camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);
- fill(255);
- int y = -5, yS = 15;
- text("brush radius(q/e):"+ drawRad + " draw alpha(1/2):" + drawOp, 10, y+=yS);
- text("lowerTween(3/4):"+ nf(lTween,0,2) + " upperTween(5/6):" + nf(uTween,0,2), 10, y+=yS);
- text("rotate wasd", 10, y+=yS);
- }
- void keyHandle() {
- if (keyDown) {
- if (key == 'a') rotX += 0.01;
- if (key == 'w') rotY += 0.01;
- if (key == 'd') rotX -= 0.01;
- if (key == 's') rotY -= 0.01;
- if (key == 'q') drawRad -= 0.2;
- if (key == 'e') drawRad += 0.2;
- if (key == '1') drawOp -= 1;
- if (key == '2') drawOp += 1;
- if (key == '3') lTween -= 0.01;
- if (key == '4') lTween += 0.01;
- if (key == '5') uTween -= 0.01;
- if (key == '6') uTween += 0.01;
- drawOp = constrain(drawOp, 1, 255);
- }
- }
- void draw() {
- keyHandle();
- editBump();
- background(0);
- hud();
- lights();
- camera(
- width/2+map(0, 0, width, -2*width, 2*width),
- height/2+map(0, 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);
- rotateX(rotX);
- rotateZ(rotY);
- textureBumpSphere(200, img, pgBump.get(), lTween, uTween);
- 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<numPointsW; i++) { // For all the points around the width
- float thetaW=i*2*PI/(numPointsW-1);
- 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 textureBumpSphere(float rx, PImage bump, PImage tex, float lowerTween, float upperTween) {
- // These are so we can map certain parts of the image on to the shape
- float changeU=tex.width/(float)(numPointsW); //-1
- float changeV=tex.height/(float)(numPointsH); //-1
- float u=0; // Width variable for the texture
- float v=0; // Height variable for the texture
- beginShape(TRIANGLE_STRIP);
- texture(tex);
- for (int i=0; i<(numPointsH-1); i++) { // For all the rings but top and bottom
- // Goes into the array here instead of loop to save time
- float coory=coorY[i];
- float cooryPlus=coorY[i+1];
- float multxz=multXZ[i];
- float multxzPlus=multXZ[i+1];
- float rad;
- for (int j=0; j<numPointsW; j++) { // For all the pts in the ring
- rad = rx * map( brightness(bump.get((int)u, (int)v)), 0, 255, lowerTween, upperTween);
- normal(-coorX[j]*multxz, -coory, -coorZ[j]*multxz);
- vertex(coorX[j]*multxz*rad, coory*rad, coorZ[j]*multxz*rad, u, v);
- rad = rx * map( brightness(bump.get((int)u, (int)(v+1.0*changeV))), 0, 255, lowerTween, upperTween);
- normal(-coorX[j]*multxzPlus, -cooryPlus, -coorZ[j]*multxzPlus);
- vertex(coorX[j]*multxzPlus*rad, cooryPlus*rad, coorZ[j]*multxzPlus*rad, u, v+changeV);
- u+=changeU;
- }
- v+=changeV;
- u=0;
- }
- endShape();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement