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:
struct Matrix4x4{
var X : SIMD4<Float>
var Y : SIMD4<Float>
var Z : SIMD4<Float>
var W : SIMD4<Float>
metal:
float4x4 projectionMatrix;
the swift code that generates the projection matrix:
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 :
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.
2
0
1.8k