Skip to content

Instantly share code, notes, and snippets.

@companje
Created July 31, 2018 21:38
Show Gist options
  • Select an option

  • Save companje/ffde37e06a60ee7bbd5f592f81e7db54 to your computer and use it in GitHub Desktop.

Select an option

Save companje/ffde37e06a60ee7bbd5f592f81e7db54 to your computer and use it in GitHub Desktop.
Latitude Longitude experiment in openFrameworks
#include "ofMain.h"
class ofApp : public ofBaseApp {
public:
ofImage earth;
ofQuaternion qTo;
void setup() {
ofBackground(0);
ofSetFrameRate(30);
ofDisableArbTex();
ofEnableDepthTest();
earth.load("earth.jpg");
}
void draw() {
ofSetupScreenOrtho(ofGetWidth(),ofGetHeight(),-1000,1000);
//3D
ofPushMatrix(); //qTo
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
float angle;
ofPoint axis;
qTo.getRotate(angle,axis);
ofRotate(angle, axis.x, axis.y, axis.z);
earth.bind();
ofPushMatrix();
ofFill();
ofSetColor(255);
ofRotateY(180);
ofDrawSphere(250);
ofPopMatrix();
earth.unbind();
//red dot
ofSetColor(255,0,0); //red
ofDrawSphere(getCartesian(qTo)*250,5);
ofPopMatrix(); //qTo
//2D
float lat = getLatitude(qTo);
float lon = getLongitude(qTo);
float x = ofMap(lon,-180,180,0,256);
float y = ofMap(lat,90,-90,0,128);
ofSetColor(255);
ofDrawBitmapString("lat: " + ofToString(lat,2),10,ofGetHeight()-40);
ofDrawBitmapString("lon: " + ofToString(lon,2),10,ofGetHeight()-20);
ofDisableDepthTest();
ofSetColor(255);
earth.draw(0,0,256,128);
ofFill();
ofSetColor(255,0,0); //red
ofDrawCircle(x,y,2);
ofEnableDepthTest();
}
float getLatitude(ofPoint c) { //cartesian
return ofRadToDeg(-asin(c.y));
}
float getLongitude(ofPoint c) { //cartesian
return ofWrapDegrees(ofRadToDeg(-atan2(c.z,c.x))+90, -180, 180);
}
float getLatitude(ofQuaternion q) {
return getLatitude(getCartesian(q));
}
float getLongitude(ofQuaternion q) {
return getLongitude(getCartesian(q));
}
ofVec3f getCartesian(ofQuaternion q, ofVec3f v = ofVec3f(0,0,1)) {
ofMatrix4x4 m;
q.get(m);
return m*v;
}
void mouseDragged(int x, int y, int button){
ofPoint from(ofGetPreviousMouseX(), ofGetPreviousMouseY());
ofPoint to(ofGetMouseX(), ofGetMouseY());
from = toSphere(from / ofGetWidth() - 0.5f);
to = toSphere(to / ofGetWidth() - 0.5f);
ofPoint axis = from.getCrossed(to);
qTo *= ofQuaternion(axis.x,axis.y,axis.z,from.dot(to));
}
ofPoint toSphere(ofPoint v) { //-0.5 ... +0.5
float mag = v.x*v.x + v.y*v.y;
if (mag>1.0f) v.normalize();
else v.z = sqrt(1.0f - mag);
return v;
}
};
int main() {
ofSetupOpenGL(500,500,OF_WINDOW);
ofRunApp(new ofApp());
}
@companje

Copy link
Copy Markdown
Author

screen shot 2018-07-31 at 23 39 00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment