我正在尝试将cgmath库集成到我的第一个实验中glium,但我无法弄清楚如何将我的Matrix4对象传递给draw()调用.
我的uniforms目标是这样定义的:
let uniforms = uniform! {
matrix: cgmath::Matrix4::from_scale(0.1)
};
Run Code Online (Sandbox Code Playgroud)
这是我的draw电话:
target.draw(&vertex_buffer, &index_slice, &program, &uniforms, &Default::default())
.unwrap();
Run Code Online (Sandbox Code Playgroud)
无法使用该消息进行编译
error[E0277]: the trait bound `cgmath::Matrix4<{float}>: glium::uniforms::AsUniformValue` is not satisfied
Run Code Online (Sandbox Code Playgroud)
我是Rust的初学者,但我相信我自己也无法实现这个特性,因为它和它的Matrix4类型都与我的分开.
除了手动将矩阵转换为浮点数组数组之外,真的没有更好的选择吗?
我相信我自己也无法实现这个特性,因为它和它的
Matrix4类型都与我的箱子分开.
这是非常正确的.
除了手动将矩阵转换为浮点数组数组之外,真的没有更好的选择吗?
好吧,你不必手动做很多事情.
首先,注意Matrix4<S> 实现Into<[[S; 4]; 4]>是有用的(我不能直接链接到那个impl,所以你必须使用ctrl+ f).这意味着您可以轻松地将其转换Matrix4为glium接受的数组.不幸的是,into()只有在编译器确切知道要转换为何种类型时才有效.所以这是一个非工作和工作版本:
// Not working, the macro accepts many types, so the compiler can't be sure
let uniforms = uniform! {
matrix: cgmath::Matrix4::from_scale(0.1).into()
};
// Works, because we excplicitly mention the type
let matrix: [[f64; 4]; 4] = cgmath::Matrix::from_scale(0.1).into();
let uniforms = uniform! {
matrix: matrix,
};
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案可能仍然无法编写.当我使用cgmath和时glium,我创建了一个辅助特性来减少代码大小.这可能不是最好的解决方案,但它有效并且没有明显的缺点(AFAIK).
pub trait ToArr {
type Output;
fn to_arr(&self) -> Self::Output;
}
impl<T: BaseNum> ToArr for Matrix4<T> {
type Output = [[T; 4]; 4];
fn to_arr(&self) -> Self::Output {
(*self).into()
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这段代码能够解释自己.有了这个特性,你现在只需要use接近draw()电话的特性然后:
let uniforms = uniform! {
matrix: cgmath::Matrix4::from_scale(0.1).to_arr(),
// ^^^^^^^^^
};
Run Code Online (Sandbox Code Playgroud)