Created
October 30, 2017 07:22
-
-
Save kylemcdonald/7597c7e0b793df0f77d497cac6fde8b5 to your computer and use it in GitHub Desktop.
Unwarp an equirectangular projection into a perspective projection using openFrameworks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofMain.h" | |
class ofApp : public ofBaseApp { | |
public: | |
ofImage img; | |
ofEasyCam cam; | |
ofSpherePrimitive sphere; | |
float zoom = 0.5; | |
float xoff = 0; | |
float yoff = 0; | |
void setup() { | |
sphere.set(1000, 128); | |
img.load("pano.jpg"); | |
} | |
void update() { | |
ofTextureData& tdata = img.getTexture().getTextureData(); | |
bool normalized = (tdata.textureTarget == GL_TEXTURE_RECTANGLE_ARB); | |
float w = tdata.tex_t * (1/zoom); | |
float h = tdata.tex_u * (1/zoom); | |
if(w > 2 * h) { | |
h = w / 2; | |
} else { | |
w = h * 2; | |
} | |
float x = (xoff * w + tdata.tex_t - w) / 2; | |
float y = (yoff * h + tdata.tex_u - h) / 2; | |
sphere.mapTexCoords(x, y, x+w, y+h); | |
} | |
void draw() { | |
ofEnableDepthTest(); | |
cam.begin(); | |
img.getTexture().bind(); | |
sphere.draw(); | |
img.getTexture().unbind(); | |
cam.end(); | |
} | |
void keyPressed(int key) { | |
if(key == '=') { | |
cam.setFov(cam.getFov() - 1); | |
} | |
if(key == '-') { | |
cam.setFov(cam.getFov() + 1); | |
} | |
if(key == '[') { | |
zoom *= .99; | |
} | |
if(key == ']') { | |
zoom /= .99; | |
} | |
if(key == OF_KEY_LEFT) { | |
xoff -= 0.01; | |
} | |
if(key == OF_KEY_RIGHT) { | |
xoff += 0.01; | |
} | |
if(key == OF_KEY_UP) { | |
yoff -= 0.01; | |
} | |
if(key == OF_KEY_DOWN) { | |
yoff += 0.01; | |
} | |
} | |
}; | |
int main() { | |
ofSetupOpenGL(1280, 720, OF_WINDOW); | |
ofRunApp(new ofApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment