openFrameworks icon indicating copy to clipboard operation
openFrameworks copied to clipboard

[idea] ofNodeMesh

Open dimitre opened this issue 9 months ago • 3 comments

I'm working on an idea to mix an ofMesh with ofNode, so we can have a matrix transformation to draw a mesh, or a way of getting transformed (like baked transformations).

I think it has great potential, works like a 3dPrimitives but only extending two already existing objects, useful for working with meshes intersections.

EDIT: updated code, working great.



class ofNodeMesh : public ofNode, ofMesh {
public:
	
	void setMesh(const ofMesh & m) {
		(*(ofMesh*)this) = m;
	}
	void setNode(const ofNode & n) {
		(*(ofNode*)this) = n;
	}
	
	void operator=(const ofNode & n) {
		(*(ofNode*)this) = n;
	}
	void operator=(const ofMesh & m) {
		(*(ofMesh*)this) = m;
	}

	void draw() {
		ofPushMatrix();
		ofMultMatrix((*(ofNode*)this).getLocalTransformMatrix());
		(*(ofMesh*)this).draw();
		ofPopMatrix();
	}
	
	ofMesh getTransformedMesh() {
		ofMesh transform = (*(ofMesh*)this);
		for (auto & v : transform.getVertices()) {
			v = (*(ofNode*)this).getLocalTransformMatrix() * glm::vec4{ v, 1.0f };
		}
		return transform;
	}
};

dimitre avatar Mar 23 '25 20:03 dimitre

ok solved by inverting multiplication order

			v =  (*(ofNode*)this).getLocalTransformMatrix() * tmp;

dimitre avatar Mar 23 '25 20:03 dimitre

Example usage: I need to make some meshes with absolute position to intersect polygons using ofxCorkCsg so I can populate them like this:

	int nSlices = 20;
	for (int a=0; a<nSlices; a++) {
		float z = ofMap(a, 0, nSlices-1, -150, 150);
		ofNodeMesh m;
		m = ofMesh::box(300, 400, 6);
		m.setPosition(0, 0, z);
		slices.emplace_back(m.getTransformedMesh());
	}
Image

dimitre avatar Mar 25 '25 16:03 dimitre