Metal Perspective Martrix issue

new to Metal,
following Janie Clayton's book. Ran into a problem creating a proper Perspective Projection Matrix.

I'm hoping someone with matrix experience will see this and the issue will jump out.

the matrix structures:
swift:
Code Block
struct Matrix4x4{
    var X : SIMD4<Float>
    var Y : SIMD4<Float>
    var Z : SIMD4<Float>
    var W : SIMD4<Float>


metal:
Code Block
float4x4 projectionMatrix;


the swift code that generates the projection matrix:
Code Block
static func perspectiveProjection(_ aspect : Float32, fieldOfView: Float32, near: Float32, far: Float32)->Matrix4x4{
        var mat = Matrix4x4()
        let zRange = far - near
        let fovRadians = fieldOfView * Float32(Double.pi / 180.0)
        let yScale = 1 / tan(fovRadians * 0.5)
        let xScale = yScale / aspect
        let zScale = -( far + near) / zRange
        let wzScale = -2 * far * near / zRange
        mat.X.x = xScale
        mat.Y.y = yScale
        mat.Z.z = zScale
        mat.Z.w = -1
        mat.W.z = wzScale
        mat.W.w = 0
        return mat
    }


how the shader applies the projection matrix :
Code Block
outVertex.position =  uniforms.projectionMatrix * props.modelMatrix * vert[vid].position;

the result here is just the clear color.

it seems that the issue is with wZScale. hard coding that to zero and the mat.W.w to 1.0, allows me to at least see my scene, skewed. messing around with those values, it seems like the objects are crushed and pushed through the camera, existing behind it.

I'm basically dead in the water here, typing word for word what is in the book. it's pretty darned frustrating. I'm just learning my way around matrices.


Replies

the net result was:
the book I was following made assumptions that turned out to be wrong. The code in the book could never work.
the Matrix4x4 structure in the book assumes a row layout in the the float4x4 used in Metal. It doesn't work like that. it uses a Column layout.

ie: the matrix I built from the tutorial assumed that each simd_float4 passed in was going to be a row. instead it was a column.
this screwed up everything.

but I also had issues with the matrix weirdly breaking my identity values.
that screwed up everything.

and my rotation function, was faulty.

For those of us following from home (or googling for answers and find this…) can you post the perspectiveProjection code that works? TIA!