Skip to content

Instantly share code, notes, and snippets.

@companje
Last active May 4, 2019 14:54
Show Gist options
  • Select an option

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

Select an option

Save companje/44dd5f65acbf35af372ba72ebadb32a7 to your computer and use it in GitHub Desktop.
show latitude longitude mouse rotating globe 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();
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();
//2D
float lat = getLatitude(getCartesian(qTo));
float lon = getLongitude(getCartesian(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);
}
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() -.5);
to = toSphere(to / ofGetWidth() -.5);
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

companje commented Aug 2, 2018

Copy link
Copy Markdown
Author

screen shot 2018-08-03 at 00 10 12

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