kylemcdonald / ofxCv

Alternative approach to interfacing with OpenCv from openFrameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

example-flow-distort doesn't compile on of 0.10.x due to ofVec2f to glm::vec3 conversion failing

sourya-sen opened this issue · comments

I did a hotfix (and can send a PR if you want) but I'm wondering if the examples should be updated to glm calls wherever ofVec*f is used.

The example can be run by updating the mesh.addVertex call from ofVec2f to ofVec3f and adding in a (dummy) z coordinate. Same in the update function - position needs to be a ofVec3f and the ofRectangle call needs to subtract a ofVec3f from the position, see below.

void ofApp::setup() {
	ofSetVerticalSync(true);
	ofSetFrameRate(120);
	cam.setup(320, 240);
	
	mesh.setMode(OF_PRIMITIVE_TRIANGLES);
	stepSize = 8;
	ySteps = cam.getHeight() / stepSize;
	xSteps = cam.getWidth() / stepSize;
	for(int y = 0; y < ySteps; y++) {
		for(int x = 0; x < xSteps; x++) {
			mesh.addVertex(ofVec3f(x * stepSize, y * stepSize, 0.0)); /
			mesh.addTexCoord(ofVec2f(x * stepSize, y * stepSize));
		}
	}
	for(int y = 0; y + 1 < ySteps; y++) {
		for(int x = 0; x + 1 < xSteps; x++) {
			int nw = y * xSteps + x;
			int ne = nw + 1;
			int sw = nw + xSteps;
			int se = sw + 1;
			mesh.addIndex(nw);
			mesh.addIndex(ne);
			mesh.addIndex(se);
			mesh.addIndex(nw);
			mesh.addIndex(se);
			mesh.addIndex(sw);
		}
	}
}

void ofApp::update() {
	cam.update();
	if(cam.isFrameNew()) {
		flow.setWindowSize(8);
		flow.calcOpticalFlow(cam);
		int i = 0;
		float distortionStrength = 4;
		for(int y = 1; y + 1 < ySteps; y++) {
			for(int x = 1; x + 1 < xSteps; x++) {
				int i = y * xSteps + x;
				ofVec3f position(x * stepSize, y * stepSize, 0.0);
				ofRectangle area(position - ofVec3f(stepSize, stepSize, 0.0) / 2, stepSize, stepSize);
				ofVec2f offset = flow.getAverageFlowInRegion(area);
				mesh.setVertex(i, position + distortionStrength * offset);
				i++;
			}
		}
	}
}