zig-gamedev: zmath translation produces weirdly rotated translation matrix which causes a translation in the vertex shader to fail

When directly passing the translation matrix generated by this code into a vertex shader

pub fn translation(x: f32, y: f32, z: f32) Mat {
    return .{
        f32x4(1.0, 0.0, 0.0, 0.0),
        f32x4(0.0, 1.0, 0.0, 0.0),
        f32x4(0.0, 0.0, 1.0, 0.0),
        f32x4(x, y, z, 1.0),
    };
}

and then multiplying it with the position unexpected behavior occurs.

By switching the translation matrix with (Taken from https://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/index.htm)

const mat = Mat{
    f32x4(1.0, 0.0, 0.0, x),
    f32x4(0.0, 1.0, 0.0, y),
    f32x4(0.0, 0.0, 1.0, z),
    f32x4(0, 0, 0, 1.0),
};

the translation works as expected.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Ay, yeah setting the near plane to 0 fixes the issue. I am new to this. Thank you.

Will defo take a look at render doc tho.

I guess that makes sense. Suppose they didn’t say if they were using DirectX or OpenGL there, just odd though that row major matrices would work natively in OpenGL which is fundamentally column major, but not DirectX without transposition despite zmath having the same convention as DirectXMath (row major)…

Either way I think maybe adding a comment to say how to use with APIs like OpenGL/DirectX might help still rather than “Can be used with any graphics API.” because even I get all this stuff mixed up and I’ve been doing graphics programming for like 10 years now (admittedly mostly OpenGL/Vulkan though so my brain is mostly in column major land), so other people probably will too.

@KekOnTheWorld I think I spotted the issue. You are setting the near to 1.0 here: zm.orthographicRh(camera.width, camera.height, 1, 10) did you mean 0.0? Note that when you mat[2][3] = 0.0; this is effectively setting near to 0.0

If that’s not it then some useful things to note would be:

  • What graphics API are you using?
  • Do you have backface culling on?
  • Are your vertices CW or CCW wound?
  • Do you want Y-up or Y-down?
  • Are you using pre or post multiplication in your shader?

A graphics debugger like RenderDoc can be useful in these kinds of situations.