Quantcast
Viewing all articles
Browse latest Browse all 40524

Adding ofMatrix4x4 transformations to a mesh and go back to the original position

Yes, definitely ofNode is what i was looking for, thank you @arturo for the suggestions . The cool thing is that when i set the parent, the transformations are relative to that parent.
I post here the code in the case someone will need it as reference. I've calculate the movement over the x-axis to make the junctions look a bit nicer.

// .h file
    int branchLenght = 80;
    int branchDimension = 20;
    int radiusSegments = 10;
    int heightSegments = 10;
    int capSegments = 10;
    float theta = 45;
    
    ofCylinderPrimitive root;
    ofVboMesh finalMesh;
// .cpp file 

    // root
    // http://www.slideshare.net/roxlu/openframeworks-007-3d
    root.set(branchDimension, branchLenght, radiusSegments, heightSegments, capSegments, false, OF_PRIMITIVE_TRIANGLES);
    root.setPosition(0, 0, 0);
    root.dolly(branchLenght/2);
    root.tilt(90);
    for(auto v: root.getMesh().getVertices()){
        finalMesh.addVertex(v * root.getGlobalTransformMatrix());
    }
    for(auto i: root.getMesh().getIndices()){
        finalMesh.addIndex(i);
    }

    //first branch
    ofCylinderPrimitive branch;
    branch.setParent(root);
    branch.set(branchDimension, branchLenght, radiusSegments, heightSegments, capSegments, false, OF_PRIMITIVE_TRIANGLES);
    branch.boom(branchLenght);
    branch.roll(theta);
    float mov_truck = sin(theta) * branchLenght/2;
    branch.truck(-mov_truck);
    
    for(auto i: branch.getMesh().getIndices()){
        finalMesh.addIndex(i + finalMesh.getNumVertices());
    }
    for(auto v: branch.getMesh().getVertices()){
        finalMesh.addVertex(v * branch.getGlobalTransformMatrix());
    }
    
    //second branch;
    ofCylinderPrimitive branch2;
    branch2.setParent(root);
    branch2.set(branchDimension, branchLenght, radiusSegments, heightSegments, capSegments, false, OF_PRIMITIVE_TRIANGLES);
    branch2.boom(branchLenght);
    branch2.roll(-theta);
    branch2.truck( (sin(theta) * branchLenght/2) );
    for(auto i: branch2.getMesh().getIndices()){
        finalMesh.addIndex(i + finalMesh.getNumVertices());
    }
    for(auto v: branch2.getMesh().getVertices()){
        finalMesh.addVertex(v * branch2.getGlobalTransformMatrix());
    }
    
    //third branch;
    ofCylinderPrimitive branch3;
    branch3.setParent(branch2);
    branch3.set(branchDimension, branchLenght, radiusSegments, heightSegments, capSegments, false, OF_PRIMITIVE_TRIANGLES);
    branch3.boom(branchLenght);
    for(auto i: branch3.getMesh().getIndices()){
        finalMesh.addIndex(i + finalMesh.getNumVertices());
    }
    for(auto v: branch3.getMesh().getVertices()){
        finalMesh.addVertex(v * branch3.getGlobalTransformMatrix());
    }

this is the result.

Image may be NSFW.
Clik here to view.


Viewing all articles
Browse latest Browse all 40524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>