Ch 4: MathLibrary

I think I found a bug in init(eye:, center:, up:) of MathLibrary.swift. It says

let W = float4(w.x, w.y, x.z, 1)

but the third argument should be w.z rather than x.z.

This fix alone seems not address the look-at transformation, though…

Ken

@kwakita - thank you for reporting this. I’ll look into this in the next few days.

@kwakita - the lookAt matrix is back-to-front. It’s used only twice in the book I think, for shadows, and the code there compensates for it being back-to-front by negating the eye position before calling it.

Try this lookAt matrix:

init(eye: float3, center: float3, up: float3) {
  let z = normalize(center - eye)
  let x = normalize(cross(up, z))
  let y = cross(z, x)
    
  let X = float4(x.x, y.x, z.x, 0)
  let Y = float4(x.y, y.y, z.y, 0)
  let Z = float4(x.z, y.z, z.z, 0)
  let W = float4(-dot(x, eye), -dot(y, eye), -dot(z, eye), 1)
    
  self.init()
  columns = (X, Y, Z, W)
}

For further insight about viewMatrix and various camera matrices, check out: https://www.3dgep.com/understanding-the-view-matrix/ - although be aware that he is using a right handed coordinate system.

(And while it’s not relevant here, OpenGL uses NDC coordinates of (-1, 1) on the z axis, whereas Metal uses (0, 1) on the z axis.)

2 Likes

@caroline thanks for your support and for giving me a pointer to further note. I will try the code and come here back when I need more support.

ken