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

Recalculate normals after transformation

$
0
0

Well, I still have some problems. When adding another node, setting the previous as parent an performing some transformations, I've strange value in the normals of the second node(e.g. their length it is not 1, as showed in the image). I'm calculating them in the same way as in the previous mesh, the root, where it works.

    // root
    root.set(branchDimension, branchLenght, radiusSegments, heightSegments, capSegments, false, OF_PRIMITIVE_TRIANGLES);
    root.setPosition(0, 0, 0);
    root.dolly(branchLenght/2);
    root.tilt(90);
    ofMatrix4x4 normalMatrix = root.getGlobalTransformMatrix().getInverse();
    for(auto i: root.getMesh().getIndices()){
        finalMesh.addIndex(i);
    }
    for(auto v: root.getMesh().getVertices()){
        finalMesh.addVertex(v * root.getGlobalTransformMatrix());
    }
    for(auto i: root.getMesh().getNormals()){
        ofVec3f normalVector = (normalMatrix * i);
        // this always print 1
        //cout << normalVector.length() << endl;
        finalMesh.addNormal(normalVector);
    }

    //first branch
    ofCylinderPrimitive branch;
    branch.setParent(root);
    branch.set(branchDimension, branchLenght, radiusSegments, heightSegments, capSegments, false, OF_PRIMITIVE_TRIANGLES);
    branch.boom(branchLenght);
    branch.roll(theta);
    //compensation x-axis
    float mov_truck = sin(theta) * branchLenght/2;
    branch.truck(-mov_truck);
    ofMatrix4x4 branchNormalMatrix = branch.getGlobalTransformMatrix().getInverse();
    for(auto i: branch.getMesh().getIndices()){
        finalMesh.addIndex(i + finalMesh.getNumVertices());
    }
    for(auto v: branch.getMesh().getVertices()){
        finalMesh.addVertex(v * branch.getGlobalTransformMatrix());
    }
    for(auto i: branch.getMesh().getNormals()){
        ofVec3f normalVector = (branchNormalMatrix * i);
        //the length of these normal should be 1, but it is not
        //cout << normalVector.length() << endl;
        finalMesh.addNormal(normalVector);
    }
    cout << finalMesh.getNumNormals() << endl;
    cout << finalMesh.getNumVertices() << endl;

The method that I use to draw the normals is invariated. What am I doing wrong?


Viewing all articles
Browse latest Browse all 40524

Trending Articles



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