Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PGraphics pg;
- PFont font;
- PImage earth;
- PShape globe;
- PShape grid;
- Quaternion qTo = new Quaternion();
- Quaternion qNow = new Quaternion();
- float globeSmoothing=.1;
- void setup() {
- size(800, 800, P3D);
- earth = loadImage("earth2k-grid.jpg");
- sphereDetail(128);
- globe = createShape(SPHERE, height/2);
- globe.setStroke(false);
- globe.setTexture(earth);
- font = loadFont("ArialMT-48.vlw");
- pg = createGraphics(405, 65, P3D);
- pg.beginDraw();
- pg.fill(255);
- pg.textFont(font);
- pg.text("Rickyboy II", 5, 48);
- pg.rect(260, 8, 50, 50);
- pg.ellipse(350, 33, 50, 50);
- pg.noFill();
- pg.stroke(255);
- pg.rect(3, 3, 400, 60);
- pg.endDraw();
- }
- void project(PVector m) {
- float r = height/2+5;
- int step = pg.width/20;
- grid = createShape();
- grid.beginShape(QUAD_STRIP);
- grid.stroke(255, 255, 0);
- for (int x=0; x<=pg.width; x+=step) {
- float xx = x-pg.width/2;
- float yy = -pg.height/2;
- grid.vertex(xx, yy, x, 0);
- xx = x-pg.width/2;
- yy = pg.height/2;
- grid.vertex(xx, yy, x, pg.width); //width ipv height huh?
- }
- grid.endShape();
- grid.setTexture(pg);
- for (int i=0; i<grid.getVertexCount(); i++) {
- PVector p = grid.getVertex(i);
- p.z = r;
- p.setMag(r);
- grid.setVertex(i, p);
- }
- }
- void draw() {
- project(toSphere(float(mouseX)/width-.5, float(mouseY)/height-.5));
- background(0);
- PVector m = toSphere(float(mouseX)/width-.5, float(mouseY)/height-.5);
- text(m.mag() + " " + m.x + "," + m.y + "," + m.z, 0, 40);
- ortho();
- pushMatrix();
- translate(height/2, height/2);
- qNow = Quaternion.slerp(globeSmoothing, qNow, qTo);
- PVector v = qNow.getAxis();
- rotate(qNow.getAngle(), v.x, v.y, v.z);
- rotateY(HALF_PI);
- shape(globe);
- popMatrix(); //einde bol
- pushMatrix();
- translate(height/2, height/2);
- //vertex
- //roteer met qNow
- //roteer vertex met lon lat
- //roteer vertex ?lokaal
- //onderstaande werkt nog niet met vertex
- float lat = 44;
- float lon = 5;
- PVector p = new PVector();
- p.x = cos(radians(lat)) * sin(radians(lon));
- p.y = sin(radians(-lat));
- p.z = cos(radians(-lat)) * cos(radians(lon));
- qNow.applyTo(p);
- float angle = atan2(p.y, p.x)-HALF_PI;
- rotate(qNow.getAngle(), v.x, v.y, v.z); //let op, dit is alleen maar de qNow rotatie
- //rotate(qNow.getAngle(), p.x, p.y, p.z);
- rotateY(radians(lon)); //lon
- rotateX(radians(lat)); //lat
- //rotate(angle); //user facing orientation: doesn't work because Zrotation of qNow should be subtracted first I think...
- rotate(mouseX/100.); //test...
- shape(grid, 0, 0);
- popMatrix();
- //image(pg, mouseX, mouseY);
- fill(255);
- noStroke();
- //float x = 0;
- //float y = 0;
- //float z = 0;
- //grid.setVertex(0,0,0);
- }
- void mouseDragged() {
- drag(pmouseX, pmouseY, mouseX, mouseY);
- }
- void keyPressed() {
- if (key==' ') qTo = new Quaternion();
- }
- void drag(float px, float py, float x, float y) {
- px = lerp(x, px, .5); //pull old position towards new position
- py = lerp(y, py, .5); //to slowdown speed to correct for f=.99
- float f = .99;
- float w = width;
- float h = height;
- float _px = map(px, w/2-h/2, w/2+h/2, -f, f);
- float _py = map(py, 0, h, -f, f);
- float _x = map(x, w/2-h/2, w/2+h/2, -f, f);
- float _y = map(y, 0, h, -f, f);
- PVector from = toSphere(_px, _py);
- PVector to = toSphere(_x, _y);
- PVector axis = from.cross(to);
- qTo.mult(new Quaternion(from.dot(to), axis.x, axis.y, axis.z)); //w,x,y,z
- qTo.normalize();
- }
- PVector toSphere(float x, float y) { //-0.5 ... +0.5
- PVector v = new PVector(x, y);
- if (v.mag()>1.0f) v.normalize();
- else v.z = sqrt(1.0 - v.mag());
- return v;
- }
- PVector toSphere(PVector v) { //-0.5 ... +0.5
- return toSphere(v.x, v.y);
- }
Add Comment
Please, Sign In to add comment