package graphics; public class MatrixStack { private static final int STACK_SIZE = 100; private int sp = -1; private Matrix3D[] stack; //start off the matrix stack with a single identity matrix public MatrixStack() { stack = new Matrix3D[STACK_SIZE]; sp = 0; stack[sp] = new Matrix3D(); stack[sp].identity(); } public int size() { return sp+1; } public boolean empty() { return (size() == 0); } public void push() { if (sp >= STACK_SIZE) { System.err.println("Cannot push onto stack - stack is full!"); return; } //copy from the top of the stack and push Matrix3D m = new Matrix3D(); m.copy(stack[sp]); sp++; stack[sp] = m; } public Matrix3D pop() { if (empty()) { System.err.println("Cannot pop stack - stack is empty!"); return null; } sp--; return stack[sp+1]; } //return matrix at the top of the stack public Matrix3D peek() { return stack[sp]; } }