Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Using ofNode to build an L-System

$
0
0

@edapx wrote:

Hello, I'm trying to use ofNode to make an L-System. I'm having problem getting the rotation of an ofNode respect to another properly.
That is my code

//App.h
ofVboMesh mesh;
std::deque<Branch>  bookmarks;

//Branch.h

class Branch: public ofNode{
    ofNode translation;

public:
    Branch(){
        ofNode::setParent(translation); // this calls the ofNode version of setParent
        // avoiding a recursive call when calling our own version
    }

    void preTranslate(ofVec3f t){
        translation.move(t);
    }

    void setParent(ofNode & node){
        translation.setParent(node);
    }
};

//ofApp.cpp
//--------------------------------------------------------------
void ofApp::setup(){
    mesh.setMode(OF_PRIMITIVE_POINTS);
    mesh.enableColors();

    ofNode root = ofNode();
    root.setPosition(450, 700, 0);
    auto branch = Branch();
    branch.setParent(root);

    string _instruction = "F+F+F";

    for (int i = 0; i < _instruction.length(); i++) {
        char c = _instruction[i];
        if (c == 'F') {
            if(mesh.getNumVertices() == 0){
                //add the first branch to the root
                branch.preTranslate(ofVec3f(0, -100, 0));
                mesh.addVertex(branch.getGlobalPosition());
                mesh.addColor(ofFloatColor(1.0, 1.0, 0.0));
            }else{
                //add a new branch
                auto newBranch = Branch();
                newBranch.setParent(branch);
                newBranch.move(ofVec3f(0, -100,0));
                mesh.addVertex(newBranch.getGlobalPosition());
                mesh.addColor(ofFloatColor(1.0, 1.0, 0.0));

                branch.preTranslate(ofVec3f(0, -100, 0));
            };
        }else if( c == 'G') {
            branch.move(ofVec3f(0, -100, 0));
        }else if (c == '+') {
            branch.roll(25.00);
        }
        else if (c == '-') {
            branch.roll(-25.00);
        }
        else if (c == '[') {
            bookmarks.push_back(branch);
        }
        else if (c == ']') {
            branch = bookmarks.back();
            bookmarks.pop_back();
        }
    }
}

What I would like is this:

What I'm aiming for is this:

Basically, i would like that the rotation of each node is relative to the previous node.
Someone has an idea about how to fix it? also suggestion for another approach are welcome.
The code is available here https://github.com/edap/testRotation

Posts: 3

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles