How to append rotated 3d meshes then export correctly aligned?


I’m building a 3d scene with multiple meshes of point clouds that I am aligning in view with rotation and x,y translation.

when I come to save the meshes to ply or obj i use mesh.append but this does not include any translation so the appended meshes are all centred around their own origin.

drawing works file like this using a vector of meshes, each having a mesh, z rotation and x,y translation.

void ofApp::draw() {
        for(int i = 0; i < streetview.size(); i++){            
            ofTranslate(streetview[i].getLon()*longOffset[i], streetview[i].getLat()*latOffset[i], 0);

void ofApp::exportOBJ(ofMesh &mesh){
    for(int i = 0; i < streetview.size(); i++){ //build new mesh to export    
    obj << "#vertices\n";    
    for(int i = 0 ; i < mesh.getNumVertices(); i++) {
        ofVec3f v = mesh.getVertex(i);
        obj << "v " + ofToString(v.x) + " " + ofToString(v.y) + " " + ofToString(v.z) + "\n";
    obj << "#faces\n";
    for(int i = 0 ; i < mesh.getNumIndices(); i += 3)
        obj << "f " + ofToString(mesh.getIndex(i)) + " " + ofToString(mesh.getIndex(i+1)) + " " + ofToString(mesh.getIndex(i+2)) + "\n";
    obj << "\n";
    cout << "wrote obj file"  << endl;

but when i export I cant apply the transformations -

does anyone know
a) a way to do the equivalent of pushmatrix // popmatrix and translate between each mesh.append when saving the file

b) a way to save the matrix view to a single file ?

hi dand
use of3dPrimitive rather than ofMesh, because of3dPrimitive can move like ofNode, like 3dPrimitive.setPosition(dx,dy,dz); , and when you need to output the mesh, just add dx,dy,dz to every vertex to the origin mesh.For to rotate, need some more calc.
that’s easy to can make ofMesh mesh; of3dPrimitive new3d= of3dPrimitive(mesh);

I did a project in which I used something like this:

void Thing::freeze() {
    // Apply pos to all vertices
    auto mat = pos.getGlobalTransformMatrix();
    for(auto & v : mesh.getVertices()) {
        v = (mat * glm::vec4(v, 1)).xyz();
    auto mat2 = pos.getOrientationQuat();
    for(auto & v : mesh.getNormals()) {
        v = (mat2 * glm::vec4(v, 1)).xyz();
    frozen = true;
void Thing::draw() {

In this project I have a vector of Thing, which has a mesh and a pos which is animated (translated, rotated and scaled). When the animation is done, I freeze the shape using that freeze method. It assumes using OF from master (OF 0.10) because of glm.

1 Like

@hamoid @ryanatltair thanks folks ! both of these are really useful - it’s amazing to see how often you can come back to code a week or two later and see it in a completely new light _ I think i see how to do it neatly now _ will post what happens when I get an hour to code it up -
BTW @hamoid - love the video pieces on will post this to my students if that’s ok?


1 Like