Problem with DirectX scene-graph
- by Alex
I'm trying to implement a basic scene graph in DirectX using C++. I am using a left child-right sibling binary tree to do this.
I'm having trouble updating each node's world transformation relative to its parent (and its parent's parent etc.). I'm struggling to get it to work recursively, though I can get it to work like this:
for(int i = 0; i < NUM_OBJECTS; i++)
{
// Initialize to identity matrix.
D3DXMatrixIdentity(&mObject[i].toWorldXForm);
int k = i;
while( k != -1 )
{
mObject[i].toWorldXForm *= mObject[k].toParentXForm;
k = mObject[k].parent;
}
}
toWorldXForm is the object's world transform and toParentXForm is the object's transform relative to the parent. I want to do this using a method within my object class (the code above is in my main class).
This is what I've tried but it doesn't work (only works with nodes 1 generation away from the root)
if (this->sibling != NULL)
this->sibling->update(toParentXForm);
D3DXMatrixIdentity(&toWorldXForm);
this->toWorldXForm *= this->toParentXForm;
this->toWorldXForm *= toParentXForm;
toParentXForm *= this->toParentXForm;
if (this->child != NULL)
this->child->update(toParentXForm);
Sorry if I've not been clear, please tell me if there's anything else you need to know. I've no doubt it's merely a silly mistake on my part, hopefully an outside view will be able to spot the problem.