Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to implement a scene graph for my 2D game in XNA, and I was looking for a way to draw every object while saving only the local transformation of the object. This seems a bit like glPushMatrix. If such a function exists I can just push a matrix for every node and then call the draw function for all its children.

The whole idea is to avoid passing parent transformations to children. Is there anyway to do so?

share|improve this question
    
Using the matrix stack, you also pass parent's transformation to its children, using a global, rather than a function parameter. If you prefer the global, simply implement your own matrix stack, thats not too hard. –  Maik Semder Aug 14 '11 at 12:19
    
Btw the answer is no, there is no matrix stack in XNA as a quick google search shows "XNA push matrix" –  Maik Semder Aug 14 '11 at 12:28
    
@maik: fair point, but using global variable reduces faction call parameters, and I don't like functions with too many parameters! –  Ali.S Aug 14 '11 at 13:11
    
There's several ways to reduce the number of parameters to a function. The simple but suboptimal one is to rely on global variables. A bit more elegant is to pack related parameters in an aggregate type like a struct you pass in. If the state is rather persistent and the same across several invocations, one approach is to make the state part of the class the method is a member of, as all non-static methods in a class have access to the data members of the class. –  Lars Viklund Aug 14 '11 at 13:45
    
openGL doesn't even use a matrix stack anymore, it's deprecated. –  Patrick Hughes Aug 14 '11 at 18:28
add comment

1 Answer 1

up vote 5 down vote accepted

XNA (nor D3D) does not expose any kind of API-side matrix stack.

There is not that much of a difference between a scene graph visitor that uses the OpenGL matrix stacks and a visitor that maintains its own private stacks.

class GLStyleVisitor
{
    public void visit(GroupNode node)
    {
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix(glGetMatrix() * node.mtx);
        foreach (Node child in node.children)
        {
            visit(child);
        }
        glPopMatrix();
    }
}

The traditional visitor shown above doesn't have to be changed much to bundle its own stack or any other context along when visiting the graph. At any place inside the visitor, you have access to the state you need for passing into whoever needs it.

class PersonalVisitor
{
    Stack<Matrix> transforms;

    public PersonalVisitor()
    {
        transforms.push(Matrix.Identity());
    }

    public void visit(GroupNode node)
    {
        transforms.push(transforms.top() * node.mtx);
        foreach (Node child in node.children)
        {
            Visit(child);
        }
        transforms.pop();
    }
}

Note that class names and methods are not overly accurate, as I am not fluent in C# or XNA.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.