Hello. I want to add a scene graph class to my 2d opengl engine, but I am stuck at the very begining. This is the scheme of my scene graph
and I am trying to render all the nodes, but children of node2 are not rendered, moreover the program says firstly there are no children of node2, but then it says it has 2 children nodes, but they are not rendered, untill I explicitly call Node2.render(). Please help!
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Node {
public:
string m_name;
Node() {};
Node(string& name) { m_name = name; };
virtual ~Node() {};
Node *m_parent;
list <Node> listChildren;
static int c;
virtual void setParent(Node* parent)
{
m_parent = parent;
m_parent->listChildren.push_back(*this);
}
virtual void render() {
cout << m_name << " is being rendered" << endl;
cout << m_name << " has " << this->listChildren.size() << " children" << endl;
list <Node>::iterator p = this->listChildren.begin();
while ( p != this->listChildren.end()){
c++;
cout << p->m_name << " is the child's node of " << m_name << " and will be processed" << endl;
p->render();
cout << "now getting next child of " << m_name << " or going further" << endl;
p++;
}
};
};
int Node::c;
int main ()
{
Node node1 (string("NODE1"));
Node node2 (string("NODE2"));
Node node3 (string("NODE3"));
Node node4 (string("NODE4"));
Node node5 (string("NODE5"));
Node node6 (string("NODE6"));
node2.setParent(&node1);
node3.setParent(&node1);
node4.setParent(&node1);
node5.setParent(&node2);
node6.setParent(&node2);
node1.c=0;
node1.render();
//node2.render();
cout << node1.c << " children rendered" << endl;
cout << "node 1, children " << node1.listChildren.size() << endl;
cout << "node 2, children " << node2.listChildren.size() << endl;
cout << "node 3, children " << node3.listChildren.size() << endl;
cout << "node 4, children " << node4.listChildren.size() << endl;
cout << "node 5, children " << node5.listChildren.size() << endl;
cout << "node 6, children " << node6.listChildren.size() << endl;
//node3.render();
getchar();
return 0;
}