openFrameworks
openFrameworks copied to clipboard
[idea] ofNodeMesh
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;
}
};
ok solved by inverting multiplication order
v = (*(ofNode*)this).getLocalTransformMatrix() * tmp;
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());
}