#matrix array initialisation

1 messages · Page 1 of 1 (latest)

proven spindle
#

doing openGL for uniformMatrix4fv it wants []const [4][4]f32 how tf do i make one. Like i have made a helper function

pub fn creatOrthoMat(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) [4][4]f32 {
    var arr: [16]f32 = @splat(0);
    arr[0] = @divExact(2.0, (right - left));
    arr[5] = @divExact(2.0, (top - bottom));
    arr[10] = @divExact(-2.0, (far - near));
    arr[12] = @divExact(-(right + left), (right - left));
    arr[13] = @divExact(-(top + bottom), (top - bottom));
    arr[14] = @divExact(-(far + near), (far - near));
    arr[15] = 1.0;
    const mat: [4][4]f32 = @ptrCast(arr);
    return mat;
}```
but i dont know how i make the final outer const array
fading rapids
#

What's the equivalent type in C?

proven spindle
fading rapids
#

you may have to create a jagged array to begin with (I have no clue if we can @ptrCast between contiguous and jagged arrays), and as per zgl source:

pub fn uniformMatrix4fv(location: ?u32, transpose: bool, items: []const [4][4]f32) void {
    if (location) |loc| {
        binding.uniformMatrix4fv(
            @as(types.Int, @intCast(loc)),
            cs2gl(items.len),
            b2gl(transpose),

            @as(*const f32, @ptrCast(items.ptr)),
        );
        checkError();
    }
}

that []const is merely getting cast into a *const anyways, which just means you need to return a 1-element comptime-size-known owning slice

GitHub

Zig OpenGL Wrapper. Contribute to ziglibs/zgl development by creating an account on GitHub.

#

so I'm guessing something like

pub fn creatOrthoMat(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) []const [4][4]f32 {
    var arr: [16]f32 = @splat(0);
    arr[0] = @divExact(2.0, (right - left));
    arr[5] = @divExact(2.0, (top - bottom));
    arr[10] = @divExact(-2.0, (far - near));
    arr[12] = @divExact(-(right + left), (right - left));
    arr[13] = @divExact(-(top + bottom), (top - bottom));
    arr[14] = @divExact(-(far + near), (far - near));
    arr[15] = 1.0;
    // const mat: [4][4]f32 = // decide if column- or row-major order for [16] -> [4][4];
    return &[_]{mat};
}

EDIT needs a damn type in there

#

Keep in mind that []T is a slice of T, while [_]T is an array of T where the compiler is figuring out its length at comptime

proven spindle
#

return &[_]{mat}; doesn't work, expected type expression at the {

fading rapids
#

rip [_]const [4][4]f32{mat}; mind the space

proven spindle
#

i think i got it working

pub fn creatOrthoMat(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) []const [4][4]f32 {
    var arr: [16]f32 = @splat(0);
    arr[0] = @divExact(2.0, (right - left));
    arr[5] = @divExact(2.0, (top - bottom));
    arr[10] = @divExact(-2.0, (far - near));
    arr[12] = @divExact(-(right + left), (right - left));
    arr[13] = @divExact(-(top + bottom), (top - bottom));
    arr[14] = @divExact(-(far + near), (far - near));
    arr[15] = 1.0;
    const mat: []const [4][4]f32 = @ptrCast(&arr);
    return mat;
}```
#

no more compile errors

#

but now i messed up my shaders 😭

fading rapids
#

please check if the @ptrCast even does what you think it does

#

as I said, you might need to make that jagged array from scratch, instead of using a [16]f32

proven spindle
fading rapids
#

again, are you sure about that?

#

afaik OpenGL works with column-major matrices

#

have you taken that into account in your calling code?

proven spindle
#

@fading rapids you made me fully doubt myself
my final result

pub fn creatOrthoMat2(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) [4][4]f32 {
    const m = [4][4]f32{
        [4]f32{ 2.0 / (right - left), 0, 0, 0 }, //
        [4]f32{ 0, 2.0 / (top - bottom), 0, 0 }, //
        [4]f32{ 0, 0, 2.0 / (far - near), 0 }, //
        [4]f32{ -(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1 }, //
    };
    return m;
}```
and the call site
```rust
const ortho = mat_math.creatOrthoMat2(0, @floatFromInt(screen_width), @floatFromInt(screen_height), 0, -1, 1);
        const proj_loc = zgl.getUniformLocation(prog, "projection");

        const matrix = [1][4][4]f32{ortho};
        zgl.uniformMatrix4fv(proj_loc, false, &matrix);
#

i know i am taking a reference to something on the stack

fading rapids