I'm trying to integrate the cgmath
library into my first experiments with glium
, but I can't figure out how to pass my Matrix4
object to the draw()
call.
My uniforms
object is defined thus:
let uniforms = uniform! {
matrix: cgmath::Matrix4::from_scale(0.1)
};
and this is my draw
call:
target.draw(&vertex_buffer, &index_slice, &program, &uniforms, &Default::default())
.unwrap();
which fails to compile with the message
error[E0277]: the trait bound `cgmath::Matrix4<{float}>: glium::uniforms::AsUniformValue` is not satisfied
I'm a total beginner with Rust, but I do believe I cannot implement this trait myself, as both it and the Matrix4
type are in a crate separate from mine.
Is there really no better option than to manually convert the matrix into an array of arrays of floats?
This is very true.
Well, you don't have to do a lot manually.
First, it's useful to note that
Matrix4<S>
implementsInto<[[S; 4]; 4]>
(I can't link to that impl directly, so you have to use ctrl+f). That means that you can easily convert aMatrix4
into an array which is accepted by glium. Unfortunately,into()
only works when the compiler knows exactly what type to convert to. So here is a non-working and a working version:But this solution might be still too much to write. When I worked with
cgmath
andglium
, I created a helper trait to reduce code size even more. This might not be the best solution, but it works and has no obvious downsides (AFAIK).I hope this code explains itself. With this trait, you now only have to
use
the trait near thedraw()
call and then: