Quantcast
Channel: openFrameworks - Latest posts
Viewing all articles
Browse latest Browse all 40524

Update Mesh slow-down

$
0
0

Hi guy, i'm new on OF, since yesterday exactly. :smile:

I don"t understand how i can refresh my mesh with no lag. I create just 1000 lines and my fps become low. It's my code?

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

	mesh.setMode(OF_PRIMITIVE_LINES);

	image.load("test.jpg");

	myPanel.setup("COLOR");
	myPanel.add(drawMyMesh.setup("drawMyMesh", 20, 0, 255));
	myPanel.add(actionRadius.setup("Action Radius", 15, 0, 30));

}

//--------------------------------------------------------------
void ofApp::update(){

	mesh.clear();

	float intensityThreshold = drawMyMesh;
	int w = image.getWidth();
	int h = image.getHeight();
	for (int x = 0; x < w; ++x) {
		for (int y = 0; y < h; ++y) {
			ofColor c = image.getColor(x, y);
			float intensity = c.getLightness();
			if (intensity <= intensityThreshold) {
				float saturation = c.getSaturation();
				float z = ofMap(saturation, 0, 255, -100, 100);
				ofVec3f pos(x * 5, y * 5, z);
				mesh.addVertex(pos);
				mesh.addColor(c);
			}
		}
	}

	float connectionDistance = actionRadius;
	int numVerts = mesh.getNumVertices();
	for (int a = 0; a < numVerts; ++a) {
		ofVec3f verta = mesh.getVertex(a);
		for (int b = a + 1; b < numVerts; ++b) {
			ofVec3f vertb = mesh.getVertex(b);
			float distance = verta.distance(vertb);
			if (distance <= connectionDistance) {
				mesh.addIndex(a);
				mesh.addIndex(b);
			}
		}
	}
	
}

//--------------------------------------------------------------
void ofApp::draw(){
	ofColor centerColor = ofColor(255, 255, 255);
	ofColor edgeColor = ofColor(125, 125, 125);
	ofBackgroundGradient(centerColor, edgeColor, OF_GRADIENT_CIRCULAR);

	easyCam.begin();
		ofPushMatrix();
			ofTranslate(-ofGetWidth() / 2, -ofGetHeight() / 2);
			mesh.draw();
		ofPopMatrix();
	easyCam.end();

	myPanel.draw();
}

Viewing all articles
Browse latest Browse all 40524

Trending Articles