4

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.

3
  • You might have to store the array returned by Mat4::to_cols_array in a local variable and pass in a pointer to this array. Commented Jun 7, 2021 at 15:44
  • Thanks this worked! I changed the transform argument to &transform.to_cols_array()[0] .Would you like to post as a formal answer, so that I can give you credit? Commented Jun 7, 2021 at 23:51
  • No worries! No need for that, please go ahead and accept your own answer. Commented Jun 8, 2021 at 16:13

1 Answer 1

5

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]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.