Created
November 18, 2012 13:57
-
-
Save companje/4105405 to your computer and use it in GitHub Desktop.
QuadWarping with openFrameworks
This file contains 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" | |
#include "ofAppGlutWindow.h" | |
class testApp : public ofBaseApp { | |
public: | |
ofImage img; | |
ofPoint corners[4]; | |
int selectedCorner; | |
void setup() { | |
img.loadImage("testbeeld.png"); | |
ofSetWindowShape(img.width+60, img.height+60); | |
selectedCorner = -1; | |
//set corners | |
corners[0].set(0,0); | |
corners[1].set(img.width,0); | |
corners[2].set(img.width,img.height); | |
corners[3].set(0,img.height); | |
} | |
void draw() { | |
ofTranslate(30, 30); | |
ofxQuadWarp(img, corners[0], corners[1], corners[2], corners[3], 40, 40); | |
for (int i=0; i<4; i++) { | |
ofCircle(corners[i],10); | |
} | |
} | |
void mousePressed(int x, int y, int button) { | |
selectedCorner = -1; | |
for (int i=0; i<4; i++) { | |
if (ofDist(corners[i].x, corners[i].y, x-30, y-30)<10) { | |
selectedCorner = i; | |
} | |
} | |
} | |
void mouseDragged(int x, int y, int button) { | |
corners[selectedCorner].set(x-30,y-30); | |
} | |
void mouseReleased(int x, int y, int button) { | |
selectedCorner = -1; | |
} | |
ofPoint ofxLerp(ofPoint start, ofPoint end, float amt) { | |
return start + amt * (end - start); | |
} | |
int ofxIndex(float x, float y, float w) { | |
return y*w+x; | |
} | |
void ofxQuadWarp(ofBaseHasTexture &tex, ofPoint lt, ofPoint rt, ofPoint rb, ofPoint lb, int rows, int cols) { | |
float tw = tex.getTextureReference().getWidth(); | |
float th = tex.getTextureReference().getHeight(); | |
ofMesh mesh; | |
for (int x=0; x<=cols; x++) { | |
float f = float(x)/cols; | |
ofPoint vTop(ofxLerp(lt,rt,f)); | |
ofPoint vBottom(ofxLerp(lb,rb,f)); | |
ofPoint tTop(ofxLerp(ofPoint(0,0),ofPoint(tw,0),f)); | |
ofPoint tBottom(ofxLerp(ofPoint(0,th),ofPoint(tw,th),f)); | |
for (int y=0; y<=rows; y++) { | |
float f = float(y)/rows; | |
ofPoint v = ofxLerp(vTop,vBottom,f); | |
mesh.addVertex(v); | |
mesh.addTexCoord(ofxLerp(tTop,tBottom,f)); | |
} | |
} | |
for (float y=0; y<rows; y++) { | |
for (float x=0; x<cols; x++) { | |
mesh.addTriangle(ofxIndex(x,y,cols+1), ofxIndex(x+1,y,cols+1), ofxIndex(x,y+1,cols+1)); | |
mesh.addTriangle(ofxIndex(x+1,y,cols+1), ofxIndex(x+1,y+1,cols+1), ofxIndex(x,y+1,cols+1)); | |
} | |
} | |
tex.getTextureReference().bind(); | |
mesh.draw(); | |
tex.getTextureReference().unbind(); | |
mesh.drawVertices(); | |
} | |
}; | |
int main () { | |
ofSetupOpenGL(new ofAppGlutWindow, 800, 600, OF_WINDOW); | |
ofRunApp(new testApp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment