Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int ptsW, ptsH;
  2.  
  3. PGraphics pgBump;
  4. PImage img;
  5.  
  6. int numPointsW;
  7. int numPointsH_2pi;
  8. int numPointsH;
  9.  
  10. float[] coorX;
  11. float[] coorY;
  12. float[] coorZ;
  13. float[] multXZ;
  14.  
  15. float rot = 0, rotX = 0, rotY = 0;
  16. float drawRad = 10;
  17. int drawOp  = 20;
  18.  
  19. boolean keyDown = false;
  20.  
  21.   float lTween = 0.9;
  22.   float uTween = 3.5;
  23.  
  24. void setup() {
  25.   size(640, 360, P3D);
  26.  
  27.   noStroke();
  28.  
  29.   loadBump();
  30.  
  31.   ptsW=120;
  32.   ptsH=120;
  33.   // Parameters below are the number of vertices around the width and height
  34.   initializeSphere(ptsW, ptsH);
  35. }
  36.  
  37. void loadBump() {
  38.   try {
  39.     img=loadImage("map.png");
  40.   }
  41.   finally {
  42.     if (img == null) {
  43.       img = createImage(width, height, RGB);
  44.       for (int i = 0; i < img.pixels.length; i++) img.pixels[i] = color(130);
  45.     }
  46.   }
  47.   pgBump = createGraphics(img.width, img.height, P2D);
  48.   pgBump.beginDraw();
  49.   pgBump.noStroke();
  50.   pgBump.image(img, 0, 0);
  51.   pgBump.endDraw();
  52. }
  53.  
  54. void keyPressed() {
  55.   keyDown = true;
  56. }
  57. void keyReleased() {
  58.   keyDown = false;
  59. }
  60.  
  61. void editBump() {
  62.   pgBump.beginDraw();
  63.   pgBump.image(img, 0, 0);
  64.   if (mousePressed) {
  65.     if (mouseButton == LEFT)  pgBump.fill(255, drawOp);
  66.     else pgBump.fill(0, drawOp);
  67.     pgBump.ellipse(mouseX/(float)width * img.width, mouseY/(float)height * img.height, drawRad, drawRad);
  68.   }
  69.   pgBump.endDraw();
  70.   img = pgBump.get();
  71.   pgBump.beginDraw();
  72.  
  73.   pgBump.fill(255, 0, 0);
  74.   pgBump.ellipse(mouseX/(float)width * img.width, mouseY/(float)height * img.height, drawRad, drawRad);
  75.   pgBump.endDraw();
  76. }
  77.  
  78. void hud() {
  79.   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);
  80.   fill(255);
  81.   int y = -5, yS = 15;
  82.   text("brush radius(q/e):"+ drawRad + " draw alpha(1/2):" + drawOp, 10, y+=yS);
  83.   text("lowerTween(3/4):"+ nf(lTween,0,2) + " upperTween(5/6):" + nf(uTween,0,2), 10, y+=yS);
  84.  
  85.   text("rotate wasd", 10, y+=yS);
  86. }
  87.  
  88. void keyHandle() {
  89.   if (keyDown) {
  90.     if (key == 'a') rotX += 0.01;
  91.     if (key == 'w') rotY += 0.01;
  92.     if (key == 'd') rotX -= 0.01;
  93.     if (key == 's') rotY -= 0.01;
  94.     if (key == 'q') drawRad -= 0.2;
  95.     if (key == 'e') drawRad += 0.2;
  96.     if (key == '1') drawOp -= 1;
  97.     if (key == '2') drawOp += 1;
  98.     if (key == '3') lTween -= 0.01;
  99.     if (key == '4') lTween += 0.01;
  100.     if (key == '5') uTween -= 0.01;
  101.     if (key == '6') uTween += 0.01;
  102.     drawOp = constrain(drawOp, 1, 255);
  103.   }
  104. }
  105.  
  106. void draw() {
  107.   keyHandle();
  108.   editBump();
  109.  
  110.   background(0);
  111.   hud();
  112.   lights();
  113.   camera(
  114.     width/2+map(0, 0, width, -2*width, 2*width),
  115.     height/2+map(0, 0, height, -height, height),
  116.     height/2/tan(PI*30.0 / 180.0),
  117.     width, height/2.0, 0,
  118.     0, 1, 0);
  119.   pushMatrix();
  120.   translate(width/2, height/2, 0);
  121.   rotateX(rotX);
  122.   rotateZ(rotY);
  123.  
  124.  
  125.   textureBumpSphere(200, img, pgBump.get(), lTween, uTween);
  126.   popMatrix();
  127. }
  128.  
  129. void initializeSphere(int numPtsW, int numPtsH_2pi) {
  130.   // The number of points around the width and height
  131.   numPointsW=numPtsW+1;
  132.   numPointsH_2pi=numPtsH_2pi;  // How many actual pts around the sphere (not just from top to bottom)
  133.   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)
  134.  
  135.   coorX=new float[numPointsW];   // All the x-coor in a horizontal circle radius 1
  136.   coorY=new float[numPointsH];   // All the y-coor in a vertical circle radius 1
  137.   coorZ=new float[numPointsW];   // All the z-coor in a horizontal circle radius 1
  138.   multXZ=new float[numPointsH];  // The radius of each horizontal circle (that you will multiply with coorX and coorZ)
  139.  
  140.   for (int i=0; i<numPointsW; i++) {  // For all the points around the width
  141.     float thetaW=i*2*PI/(numPointsW-1);
  142.     coorX[i]=sin(thetaW);
  143.     coorZ[i]=cos(thetaW);
  144.   }
  145.   for (int i=0; i<numPointsH; i++) {  // For all points from top to bottom
  146.     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
  147.       float thetaH=(i-1)*2*PI/(numPointsH_2pi);
  148.       coorY[i]=cos(PI+thetaH);
  149.       multXZ[i]=0;
  150.     } else {
  151.       //The numPointsH_2pi and 2 below allows there to be a flat bottom if the numPointsH is odd
  152.       float thetaH=i*2*PI/(numPointsH_2pi);
  153.  
  154.       //PI+ below makes the top always the point instead of the bottom.
  155.       coorY[i]=cos(PI+thetaH);
  156.       multXZ[i]=sin(thetaH);
  157.     }
  158.   }
  159. }
  160.  
  161. void textureBumpSphere(float rx, PImage bump, PImage tex, float lowerTween, float upperTween) {
  162.   // These are so we can map certain parts of the image on to the shape
  163.   float changeU=tex.width/(float)(numPointsW); //-1
  164.   float changeV=tex.height/(float)(numPointsH); //-1
  165.   float u=0;  // Width variable for the texture
  166.   float v=0;  // Height variable for the texture
  167.  
  168.  
  169.   beginShape(TRIANGLE_STRIP);
  170.   texture(tex);
  171.   for (int i=0; i<(numPointsH-1); i++) {  // For all the rings but top and bottom
  172.     // Goes into the array here instead of loop to save time
  173.     float coory=coorY[i];
  174.     float cooryPlus=coorY[i+1];
  175.     float multxz=multXZ[i];
  176.     float multxzPlus=multXZ[i+1];
  177.     float rad;
  178.     for (int j=0; j<numPointsW; j++) { // For all the pts in the ring
  179.       rad = rx *  map( brightness(bump.get((int)u, (int)v)), 0, 255, lowerTween, upperTween);
  180.       normal(-coorX[j]*multxz, -coory, -coorZ[j]*multxz);
  181.       vertex(coorX[j]*multxz*rad, coory*rad, coorZ[j]*multxz*rad, u, v);
  182.  
  183.       rad = rx *  map( brightness(bump.get((int)u, (int)(v+1.0*changeV))), 0, 255, lowerTween, upperTween);
  184.       normal(-coorX[j]*multxzPlus, -cooryPlus, -coorZ[j]*multxzPlus);
  185.       vertex(coorX[j]*multxzPlus*rad, cooryPlus*rad, coorZ[j]*multxzPlus*rad, u, v+changeV);
  186.       u+=changeU;
  187.     }
  188.     v+=changeV;
  189.     u=0;
  190.   }
  191.   endShape();
  192. }
Advertisement
RAW Paste Data Copied
Advertisement