go-gl / mathgl

A pure Go 3D math library.
BSD 3-Clause "New" or "Revised" License
554 stars 65 forks source link

Matrix stack #25

Closed UserAB1236872 closed 10 years ago

UserAB1236872 commented 10 years ago

Both OpenGL's old matrix model and GLM provide matrix stacks. I think this is worthing putting in a new package. A Matrix stack is, in essence, a fully-persistent data structure that keeps track of matrix mutations and allows you to roll back and arbitrary number of operations. This is extremely useful for, e.g., scenegraphs.. A sketch of push, for instance:

type MatrixStack []Mat4

func (ms *MatrixStack) Push(m Mat4) error {
    if len(*ms) == 0 {
        return errors.New("Matrix stack is empty, create a new stack or use MatrixStack.Load to add the first matrix")
    }

    prev := (*ms)[len(*ms)-1]
    (*ms) = append(*ms, prev.Mul4(m))

   return nil
}

The biggest question is whether the stack should be Mat4 only (I believe OpenGL took this approach?) or whether there should be support for Mat3 and Mat2 as well. I suspect the basic (push/pop/peek/load) implementation will be fewer than 200 lines per matrix type, so it may be worth just doing all 3.

UserAB1236872 commented 10 years ago

I'll note that I somehow got off my gourd and totally misremembered how the matrix stack works. The previous implementation has been named the TransformStack, and the new MatrixStack should work like GL's which is an explicit state COPYING stack.