Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "ofApp.h"
// Note:
// You need ofxGui
// You can download the texture here: https://www.dropbox.com/s/15lw3lh87yp11za/tex-bigger.jpg?dl=1
ofPolyline resampled;
ofPolyline points;
ofParameter<float> resampleLength;
ofParameter<bool> bDrawWireframe;
ofParameter<bool> bDrawExtra;
ofParameter<bool> bResample;
ofParameter<float> width;
ofTexture texture;
ofVboMesh mesh;
ofParameter<float> texScale;
ofParameter<bool> animateTex;
//--------------------------------------------------------------
void ofApp::setup() {
ofBackgroundHex(0x82DBCE);
ofSetFrameRate(60);
ofDisableArbTex();
if(ofLoadImage(texture, "tex-bigger.jpg")) {
cout << "Texture did loaded" << endl;
texture.setTextureWrap(GL_REPEAT, GL_REPEAT);
}
gui.setup("app", "app.xml", 10, 10);
gui.add(width.set("width", 42, 1, 256));
gui.add(resampleLength.set("Re-sample length (px)", 256, 1, 256));
gui.add(bDrawWireframe.set("Draw wireframe", true));
gui.add(bResample.set("Do Resample", true));
gui.add(bDrawExtra.set("Draw extras", false));
gui.add(texScale.set("Scale texture", 1, 0.01, 5.0));
gui.add(animateTex.set("Animate texture", false));
gui.loadFromFile("app.xml");
mesh.setUsage(GL_STATIC_DRAW);
mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
}
//--------------------------------------------------------------
void ofApp::draw() {
float t = ofGetElapsedTimef() * 3;
float textureWidth = texture.getWidth();
ofVec2f mouse(ofGetMouseX(), ofGetMouseY());
if (points.size() > 1) {
resampled = bResample ? points.getResampledBySpacing(resampleLength) : points;
mesh.clear();
for (int i=0; i<resampled.size(); i++) {
ofVec2f pt = resampled[i];
ofVec2f perp = resampled.getNormalAtIndex(i) * width;
/*
[3] ---- [2]
/
/
/
/
[1] ---- [0]
*/
float m = resampleLength * (resampled.size()-1);
float tw = (width*2) / textureWidth;
float th = ofMap(i, 0, resampled.size()-1, 0.0, m) / textureWidth;
if (i == 0) {
cout << tw << endl;
cout << th << endl;
}
tw *= 2 + texScale;
th *= 2 + texScale;
if (animateTex) {
th += t;
}
mesh.addVertex(pt - perp); mesh.addTexCoord(ofVec2f(0, th));
mesh.addVertex(pt + perp); mesh.addTexCoord(ofVec2f(tw, th));
}
ofSetColor(255);
texture.bind();
mesh.draw();
texture.unbind();
// draw the wireframe
if (bDrawWireframe) {
ofSetHexColor(0xFF1065);
mesh.clearColors();
mesh.drawWireframe();
}
// draw perps and points
if (bDrawExtra) {
for (int i=0; i<resampled.size(); i++) {
ofVec2f pt = resampled[i];
ofSetColor(255);
ofDrawCircle(pt, 3);
ofSetHexColor(0x202F3E);
ofDrawCircle(pt, 1);
ofVec2f perp = resampled.getNormalAtIndex(i) * width;
ofSetColor(0, 50);
ofDrawLine(pt - perp, pt + perp);
}
}
}
// draw the texture
ofSetColor(255);
ofPushMatrix();
ofTranslate(gui.getPosition().x + gui.getWidth() + 10, gui.getPosition().y);
texture.draw(0, 0, textureWidth/2, textureWidth/2);
ofSetColor(0);
ofDrawBitmapStringHighlight(ofToString(texture.getWidth(), 0)+"x"+ofToString(texture.getHeight(), 0), 10, 18);
ofPopMatrix();
// info
gui.draw();
ofSetColor(0);
ofDrawBitmapStringHighlight(ofToString(resampled.size()) + " pts\n"+ofToString(ofGetFrameRate(),0)+ " fps", ofGetWidth()-70, ofGetHeight()-30);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
if (key == 'c') {
points.clear();
}
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button) {
points.addVertex(ofVec2f(x, y));
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
points.addVertex(ofVec2f(x, y));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment