Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Last active August 25, 2017 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanderlin/f2281b1dcfe3de42e5ca6c8b82405b85 to your computer and use it in GitHub Desktop.
Save vanderlin/f2281b1dcfe3de42e5ca6c8b82405b85 to your computer and use it in GitHub Desktop.
#include "ofApp.h"
// Note:
// You need ofxGui
ofPolyline resampled;
ofPolyline points;
ofParameter<float> resampleLength;
ofParameter<bool> bDrawWireframe;
ofParameter<bool> bDrawExtra;
ofParameter<float> width;
ofVboMesh mesh;
//--------------------------------------------------------------
void ofApp::setup() {
ofBackgroundHex(0x82DBCE);
ofSetFrameRate(60);
gui.setup("app", "app.xml", 10, 10);
gui.add(width.set("width", 25, 1, 150));
gui.add(resampleLength.set("Re-sample length (px)", 33, 1, 50));
gui.add(bDrawWireframe.set("Draw wireframe", false));
gui.add(bDrawExtra.set("Draw extras", false));
gui.loadFromFile("app.xml");
mesh.setUsage(GL_STATIC_DRAW);
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
}
//--------------------------------------------------------------
void ofApp::draw() {
ofVec2f mouse(ofGetMouseX(), ofGetMouseY());
if (points.size() > 1) {
resampled = points.getResampledBySpacing(resampleLength);
mesh.clear();
for (int i=1; i<resampled.size(); i++) {
ofVec2f pt0 = resampled[i-1];
ofVec2f perp0 = resampled.getNormalAtIndex(i-1) * width;
ofVec2f pt1 = resampled[i];
ofVec2f perp1 = resampled.getNormalAtIndex(i) * width;
ofFloatColor color;
if (i%2) {
color.set(0.93, 0.79, 0.23, 1.00);
} else {
color.set(0.91, 0.27, 0.20, 1.00);
}
mesh.addVertex(pt0 - perp0); mesh.addColor(color);
mesh.addVertex(pt0 + perp0); mesh.addColor(color);
mesh.addVertex(pt1 - perp1); mesh.addColor(color);
mesh.addVertex(pt1 - perp1); mesh.addColor(color);
mesh.addVertex(pt1 + perp1); mesh.addColor(color);
mesh.addVertex(pt0 + perp0); mesh.addColor(color);
}
mesh.draw();
if (bDrawWireframe) {
ofSetHexColor(0x202F3E);
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);
}
}
}
gui.draw();
ofSetColor(0);
ofDrawBitmapStringHighlight(ofToString(resampled.size()) + " pts\n"+ofToString(ofGetFrameRate(),0)+ " fps", ofGetWidth()-70, ofGetHeight()-20);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
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