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
Out of the loop it is easier because i can recreate a new ofNode for every point, and the transformation are in the correct order (before the rotation and after the translation)
I think that the problem is in the copy of the ofNode. I’ve edited the previous code as follow, removing the pre-translation and using directly ofNode.
ofNode root = ofNode();
root.setPosition(450, 700, 0);
auto branch = ofNode();
branch.setParent(root);
bool branching = false;
string _instruction = "F+F+F";
mesh.clear();
for (int i = 0; i < _instruction.length(); i++) {
char c = _instruction[i];
if (c == 'F') {
branching = true;
}else if (c == '+') {
branch.roll(25.0);
}
if(branching){
if(mesh.getNumVertices() == 0){
//add the first branch to the root
branch.move(ofVec3f(0, -100, 0));
cout << branch.getGlobalPosition().y << endl;
mesh.addVertex(branch.getGlobalPosition());
mesh.addColor(ofFloatColor(1.0, 1.0, 0.0));
branching = false;
}else{
//add a new branch
auto oldBranch = ofNode(branch);
auto branch = ofNode();
branch.setParent(oldBranch);
branch.move(ofVec3f(0, -100,0));
mesh.addVertex(branch.getGlobalPosition());
mesh.addColor(ofFloatColor(1.0, 1.0, 0.0));
branching = false;
};
}
}
I think the problem is here
auto oldBranch = ofNode(branch);
auto branch = ofNode();
branch.setParent(oldBranch);
What I would like to do is that oldBranch save the position of branch accumulated until now, and auto branch = ofNode() create a new ofNode, re-assigning the value of branch. What is happening is that branch it is not re-assigned. It remains the same as at the beginning.
Some ideas?
if you want the rotation of a node to be relative to the previous one you can just rotate the previous one or add a third node that is the one which rotates and is parented to the previous one.
I think that with ofNode it should be possible to do inside a loop what is done in the snippet posted here Using ofNode to build an L-System
If it is not, it should be documented somewhere how to copy a ofNode.
I will experiment a bit again, and if it does not work I will simply use Vectors
Nope, it is still not working, I’ve a bad memory access. I’ve simplified and pushed the code, removing the part with the L-System grammar that is not the problem.
yes but when you do joinBranch = ofNode(); in the second iteration the previous joinBranch gets deleted and the so the parent for oldBranch is not valid anymore so the program crashes. this is not java things allocated in the stack get deleted right away. when you do joinBranch.setParent(oldBranch); you are passing a reference to the original but you are responsible to keep that parent alive for the same scope as the child.