Guest User

Untitled

a guest
Sep 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. PGraphics pg;
  2. PFont font;
  3. PImage earth;
  4. PShape globe;
  5. PShape grid;
  6. Quaternion qTo = new Quaternion();
  7. Quaternion qNow = new Quaternion();
  8. float globeSmoothing=.1;
  9.  
  10. void setup() {
  11. size(800, 800, P3D);
  12. earth = loadImage("earth2k-grid.jpg");
  13. sphereDetail(128);
  14. globe = createShape(SPHERE, height/2);
  15. globe.setStroke(false);
  16. globe.setTexture(earth);
  17. font = loadFont("ArialMT-48.vlw");
  18.  
  19. pg = createGraphics(405, 65, P3D);
  20. pg.beginDraw();
  21. pg.fill(255);
  22. pg.textFont(font);
  23. pg.text("Rickyboy II", 5, 48);
  24. pg.rect(260, 8, 50, 50);
  25. pg.ellipse(350, 33, 50, 50);
  26. pg.noFill();
  27. pg.stroke(255);
  28. pg.rect(3, 3, 400, 60);
  29. pg.endDraw();
  30. }
  31.  
  32. void project(PVector m) {
  33. float r = height/2+5;
  34.  
  35. int step = pg.width/20;
  36. grid = createShape();
  37. grid.beginShape(QUAD_STRIP);
  38. grid.stroke(255, 255, 0);
  39. for (int x=0; x<=pg.width; x+=step) {
  40. float xx = x-pg.width/2;
  41. float yy = -pg.height/2;
  42. grid.vertex(xx, yy, x, 0);
  43.  
  44. xx = x-pg.width/2;
  45. yy = pg.height/2;
  46. grid.vertex(xx, yy, x, pg.width); //width ipv height huh?
  47. }
  48. grid.endShape();
  49. grid.setTexture(pg);
  50.  
  51.  
  52. for (int i=0; i<grid.getVertexCount(); i++) {
  53. PVector p = grid.getVertex(i);
  54. p.z = r;
  55. p.setMag(r);
  56. grid.setVertex(i, p);
  57. }
  58. }
  59.  
  60. void draw() {
  61. project(toSphere(float(mouseX)/width-.5, float(mouseY)/height-.5));
  62.  
  63. background(0);
  64.  
  65. PVector m = toSphere(float(mouseX)/width-.5, float(mouseY)/height-.5);
  66.  
  67. text(m.mag() + " " + m.x + "," + m.y + "," + m.z, 0, 40);
  68.  
  69. ortho();
  70. pushMatrix();
  71. translate(height/2, height/2);
  72. qNow = Quaternion.slerp(globeSmoothing, qNow, qTo);
  73. PVector v = qNow.getAxis();
  74. rotate(qNow.getAngle(), v.x, v.y, v.z);
  75. rotateY(HALF_PI);
  76. shape(globe);
  77. popMatrix(); //einde bol
  78.  
  79.  
  80. pushMatrix();
  81. translate(height/2, height/2);
  82.  
  83. //vertex
  84. //roteer met qNow
  85. //roteer vertex met lon lat
  86. //roteer vertex ?lokaal
  87.  
  88. //onderstaande werkt nog niet met vertex
  89.  
  90.  
  91.  
  92. float lat = 44;
  93. float lon = 5;
  94. PVector p = new PVector();
  95. p.x = cos(radians(lat)) * sin(radians(lon));
  96. p.y = sin(radians(-lat));
  97. p.z = cos(radians(-lat)) * cos(radians(lon));
  98. qNow.applyTo(p);
  99. float angle = atan2(p.y, p.x)-HALF_PI;
  100.  
  101. rotate(qNow.getAngle(), v.x, v.y, v.z); //let op, dit is alleen maar de qNow rotatie
  102. //rotate(qNow.getAngle(), p.x, p.y, p.z);
  103. rotateY(radians(lon)); //lon
  104. rotateX(radians(lat)); //lat
  105. //rotate(angle); //user facing orientation: doesn't work because Zrotation of qNow should be subtracted first I think...
  106. rotate(mouseX/100.); //test...
  107.  
  108.  
  109. shape(grid, 0, 0);
  110.  
  111. popMatrix();
  112.  
  113.  
  114.  
  115. //image(pg, mouseX, mouseY);
  116.  
  117. fill(255);
  118. noStroke();
  119.  
  120.  
  121.  
  122. //float x = 0;
  123. //float y = 0;
  124. //float z = 0;
  125.  
  126. //grid.setVertex(0,0,0);
  127. }
  128.  
  129. void mouseDragged() {
  130. drag(pmouseX, pmouseY, mouseX, mouseY);
  131. }
  132.  
  133. void keyPressed() {
  134. if (key==' ') qTo = new Quaternion();
  135. }
  136.  
  137. void drag(float px, float py, float x, float y) {
  138. px = lerp(x, px, .5); //pull old position towards new position
  139. py = lerp(y, py, .5); //to slowdown speed to correct for f=.99
  140. float f = .99;
  141. float w = width;
  142. float h = height;
  143. float _px = map(px, w/2-h/2, w/2+h/2, -f, f);
  144. float _py = map(py, 0, h, -f, f);
  145. float _x = map(x, w/2-h/2, w/2+h/2, -f, f);
  146. float _y = map(y, 0, h, -f, f);
  147. PVector from = toSphere(_px, _py);
  148. PVector to = toSphere(_x, _y);
  149. PVector axis = from.cross(to);
  150. qTo.mult(new Quaternion(from.dot(to), axis.x, axis.y, axis.z)); //w,x,y,z
  151. qTo.normalize();
  152. }
  153.  
  154. PVector toSphere(float x, float y) { //-0.5 ... +0.5
  155. PVector v = new PVector(x, y);
  156. if (v.mag()>1.0f) v.normalize();
  157. else v.z = sqrt(1.0 - v.mag());
  158. return v;
  159. }
  160.  
  161. PVector toSphere(PVector v) { //-0.5 ... +0.5
  162. return toSphere(v.x, v.y);
  163. }
Add Comment
Please, Sign In to add comment