Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a Mat4 as float pointer in rust 'glam' crate to Opengl call gl::UniformMatrix4fv?

Tags:

rust

opengl

I'm learning rust using opengl. I'm using the 'gl' crate for opengl specifically. I'm using the 'glam' crate for 3d math. The last argument of the function gl::UniformMatrix4fv() looks for is of type *const GLfloat. I've tried:

    let mut transform = Mat4::IDENTITY;
    transform = transform * Mat4::from_translation(Vec3::new(0.5, 0.5, 0.0));
    transform = transform * Mat4::from_axis_angle(Vec3::new(0.0, 0.0, 1.0), window.get_time());

    let transform_loc = gl::GetUniformLocation(shader.id, CString::new("transform").unwrap().as_ptr());
    gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform as *const f32);

However glam implements Mat4 as a struct. The above code panics with "non-primitive cast: glam::Mat4 as *const f32" error.

Without the casting the error is of "mismatched types. expected *-ptr, found 'glam::Mat4' expected raw pointer *const f32 found struct glam::Mat4"

Looking over the documentation I don't see any functions that will return a pointer to the Matrix data. Any help or turn to the proper direction will be greatly appreciated. If not possible, I will try a different crate.

like image 228
Joseph Vargas Avatar asked Oct 16 '25 11:10

Joseph Vargas


1 Answers

Credit to a comment by EvilTak.

glam has a method to_col_array() that returns the data in the Mat4.

issue solved by changing:

gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, transform as *const f32);

to

gl::UniformMatrix4fv(transform_loc, 1, gl::FALSE, &transform.to_cols_array()[0]);
like image 117
Joseph Vargas Avatar answered Oct 19 '25 00:10

Joseph Vargas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!