Post

Replies

Boosts

Views

Activity

Reply to View and Projection Matrix in Metal for a First Person Camera
Fixed! The problem seemed to be that I was utilising the "To-NDC" conversion from the Calculating Primitive Visibility Using Depth Testing Sample. This bit: out.position = vector_float4(0.0, 0.0, 0.0, 1.0); out.position.xy = pixelSpacePosition / (viewportSize / 2.0); But that is already being taken care of by the Projection Matrix (not entirely sure on that, correct me if I'm wrong), so I basically transformed the X- and Y-Coordinates twice to Normalised Device Space which, I guess, messed it up. The correct Vertex Shader Function would be: vertex transformed_data vertex_shader(uint vertex_id [[vertex_id]],                                       constant _vertex *vertices [[buffer(0)]],                                       constant _uniforms& uniforms [[buffer(1)]]) {     transformed_data output;     float3 dir = {0, 0, -1};     float3 inEye = float3{ 0, 0, 1 }; // position     float3 inTo = inEye + dir; // position + direction     float3 inUp = float3{ 0, 1, 0};          float3 z = normalize(inTo - inEye);     float3 x = normalize(cross(inUp, z));     float3 y = cross(z, x);     float3 t = (float3) { -dot(x, inEye), -dot(y, inEye), -dot(z, inEye) };     float4x4 viewm = float4x4(float4 { x.x, y.x, z.x, 0 },                               float4 { x.y, y.y, z.y, 0 },                               float4 { x.z, y.z, z.z, 0 },                               float4 { t.x, t.y, t.z, 1 });     float _nearPlane = 0.1f;     float _farPlane = 100.0f;     float _aspectRatio = uniforms.viewport_size.x / uniforms.viewport_size.y;     float va_tan = 1.0f / tan(0.6f * 3.14f * 0.5f);     float ys = va_tan;     float xs = ys / _aspectRatio;     float zs = _farPlane / (_farPlane - _nearPlane);     float4x4 projectionm = float4x4((float4){ xs,  0,  0, 0},                                     (float4){  0, ys,  0, 0},                                     (float4){  0,  0, zs, 1},                                     (float4){  0,  0, -_nearPlane * zs, 0 } );     output.position = projectionm * viewm * float4(vertices[vertex_id].position,1);     output.color = vertices[vertex_id].color;     return output; } Of course you can calculate the View Matrix and Projection Matrix and CPU and send it to the GPU, which has many advantages. I solely chose this setup to eliminate as much error potential as possible and to allow easy debugging. If you still have any resources or sample code of working FPS Cameras in Metal please post them. There are basically none on the internet.
Aug ’22