[英]How can I use a cgmath::Matrix as a uniform parameter in glium?
我正在嘗試將cgmath
庫集成到我的第一個glium
實驗中,但我無法弄清楚如何將我的Matrix4
對象傳遞給draw()
調用。
我的uniforms
對象是這樣定義的:
let uniforms = uniform! {
matrix: cgmath::Matrix4::from_scale(0.1)
};
這是我的draw
電話:
target.draw(&vertex_buffer, &index_slice, &program, &uniforms, &Default::default())
.unwrap();
無法使用該消息進行編譯
error[E0277]: the trait bound `cgmath::Matrix4<{float}>: glium::uniforms::AsUniformValue` is not satisfied
我是Rust的初學者,但我確實認為自己無法實現這一特性,因為它和Matrix4
類型都與我的分開。
除了手動將矩陣轉換為浮點數組數組之外,真的沒有更好的選擇嗎?
我確實認為自己無法實現這一特性,因為它和
Matrix4
類型都與我的箱子分開。
這是非常正確的。
除了手動將矩陣轉換為浮點數組數組之外,真的沒有更好的選擇嗎?
好吧,你不必手動做很多事情。
首先,注意Matrix4<S>
實現Into<[[S; 4]; 4]>
Into<[[S; 4]; 4]>
Into<[[S; 4]; 4]>
(我無法直接鏈接到那個impl,所以你必須使用ctrl + f )。 這意味着您可以輕松地將Matrix4
轉換為Matrix4
接受的數組。 不幸的是, 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,
};
但是這個解決方案可能仍然無法編寫。 當我使用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()
}
}
我希望這段代碼能夠解釋自己。 有了這個特性,你現在只需要在draw()
調用附近use
特征,然后:
let uniforms = uniform! {
matrix: cgmath::Matrix4::from_scale(0.1).to_arr(),
// ^^^^^^^^^
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.