簡體   English   中英

如何在glium中使用cgmath :: Matrix作為統一參數?

[英]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,  
};

但是這個解決方案可能仍然無法編寫。 當我使用cgmathglium ,我創建了一個輔助特性來減少代碼大小。 這可能不是最好的解決方案,但它有效並且沒有明顯的缺點(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM