## Notes on Oct 3 class

In order to do the homework that is due on Tuesday October 17 the following notes will be useful.

Also, there are a few slots available for CS graphics students interested in seeing a small talk given here at NYU by Jeffrey Katzenberg on Oct 30 at 8:30am. Please send me an email if you would like to attend this event.

### Maintaining a matrix stack

As we discussed in class, a matrix stack can be implemented as an array of matrices, with a stack pointer:
int topPointer = 0;
Matrix matrixStack[] = new Matrix[STACK_SIZE_LIMIT];
The push and pop methods are easy to implement:
void push() {
matrixStack[topPointer+1].copy(matriStack[topPointer]);
topPointer++;
// YOU SHOULD DO ERROR CHECKING HERE FOR STACK OVERFLOW
}

void pop() {
topPointer--;
// YOU SHOULD DO ERROR CHECKING HERE FOR STACK UNDERFLOW
}
You probably also want an access routine to make it easy to refer to the matrix that is currently on top of the stack:
// MATRIX STACK ACCESS ROUTINE FOR USE WITH translate(), rotateX(), ....

Matrix m() {
return matrixStack[topPointer];
}
At every animation frame you'll want to reset your matrix stack to a clean state:
void initializeMatrixStackForAnimationFrame() {
topPointer = 0;
m().identity();
}

### Perspective

Assuming your camera is at the origin [0,0,0], and aimed into negative z, then the perspective transformation is given by:
x → (-f / z) x
y → (-f / z) y
z → (-f / z)

We know that this is a linear transformation (ie: it preserves straight lines) because this perspective transformation can be effected by a 4×4 linear transformation matrix:

P   =
1 0 0 0
0 1 0 0
0 0 0 1
0 0 -1/f 0
This is because
P [x,y,z,1]T     (where the 'T' superscript denotes transpose, because this is a column vector)

evaluates to:
[x,y,1,-z/f]T.
Dividing through by the homogeneous coordinate (-z/f) gives
[(-f/z)x,(-f/z)y,(-f/z),1]T.

### Camera model

As we discussed at the very end of class, a "flying camera" can be implemented by:
1. translate and rotate camera matrix for this animation frame,
2. invert this matrix,
3. use this inverse matrix as the starting matrix transforming objects in your scene at this animation frame, rather than the identity matrix.
Here is an implementation of an matrix inversion method.