rustgd / cgmath

A linear algebra and mathematics library for computer graphics.
https://docs.rs/cgmath
Apache License 2.0
1.12k stars 155 forks source link

Help converting `Matrix4<f32>` to `&[f32]` #542

Closed junglie85 closed 2 years ago

junglie85 commented 2 years ago

Glow has a uniform_matrix_4_f32_slice method for uploading a mat4 uniform. It expects a&[f32] whilst Matrix4<f32> only has an Into implementation for [[f32; 4]; 4].

I've tried to construct using slice_from_raw_parts but end up with something that is 64 elements long (I'd have expected 16, although when multiplied by the size of an f32 it would be 64):

slice_from_raw_parts(
    (matrix as *const Matrix4<f32>) as *const f32,
    std::mem::size_of::<Matrix4<f32>>(),
);

This is clearly more of a "how do you do in Rust" question, but, how do I get a reference to the Matrix4<f32> as a &[f32]?

junglie85 commented 2 years ago

I solved it - I should have passed in the number of elements, not bytes.

let data = (&array4x4(*matrix) as *const [[f32; 4]; 4]) as *const f32;
let raw = slice::from_raw_parts(data, 16);