2017-12-02 23 views
-2

私は、float [4] [4]* =からなる単純な構造体 "mat4"を4x4行列に乗算する機能を持っています。これは次のように&「RHS」のconst MAT4を取りますC++で4x4浮動行列を乗算する方法は?

this->m[0][0] = this->m[0][0] * rhs[0][0] + this->m[0][1] * rhs[1][0] + this->m[0][2] * rhs[2][0] + this->m[0][3] * rhs[3][0]; 
this->m[0][1] = this->m[0][0] * rhs[0][1] + this->m[0][1] * rhs[1][1] + this->m[0][2] * rhs[2][1] + this->m[0][3] * rhs[3][1]; 
this->m[0][2] = this->m[0][0] * rhs[0][2] + this->m[0][1] * rhs[1][2] + this->m[0][2] * rhs[2][2] + this->m[0][3] * rhs[3][2]; 
this->m[0][3] = this->m[0][0] * rhs[0][3] + this->m[0][1] * rhs[1][3] + this->m[0][2] * rhs[2][3] + this->m[0][3] * rhs[3][3]; 

this->m[1][0] = this->m[1][0] * rhs[0][0] + this->m[1][1] * rhs[1][0] + this->m[1][2] * rhs[2][0] + this->m[1][3] * rhs[3][0]; 
this->m[1][1] = this->m[1][0] * rhs[0][1] + this->m[1][1] * rhs[1][1] + this->m[1][2] * rhs[2][1] + this->m[1][3] * rhs[3][1]; 
this->m[1][2] = this->m[1][0] * rhs[0][2] + this->m[1][1] * rhs[1][2] + this->m[1][2] * rhs[2][2] + this->m[1][3] * rhs[3][2]; 
this->m[1][3] = this->m[1][0] * rhs[0][3] + this->m[1][1] * rhs[1][3] + this->m[1][2] * rhs[2][3] + this->m[1][3] * rhs[3][3]; 

this->m[2][0] = this->m[2][0] * rhs[0][0] + this->m[2][1] * rhs[1][0] + this->m[2][2] * rhs[2][0] + this->m[2][3] * rhs[3][0]; 
this->m[2][1] = this->m[2][0] * rhs[0][1] + this->m[2][1] * rhs[1][1] + this->m[2][2] * rhs[2][1] + this->m[2][3] * rhs[3][1]; 
this->m[2][2] = this->m[2][0] * rhs[0][2] + this->m[2][1] * rhs[1][2] + this->m[2][2] * rhs[2][2] + this->m[2][3] * rhs[3][2]; 
this->m[2][3] = this->m[2][0] * rhs[0][3] + this->m[2][1] * rhs[1][3] + this->m[2][2] * rhs[2][3] + this->m[2][3] * rhs[3][3]; 

this->m[3][0] = this->m[3][0] * rhs[0][0] + this->m[3][1] * rhs[1][0] + this->m[3][2] * rhs[2][0] + this->m[3][3] * rhs[3][0]; 
this->m[3][1] = this->m[3][0] * rhs[0][1] + this->m[3][1] * rhs[1][1] + this->m[3][2] * rhs[2][1] + this->m[3][3] * rhs[3][1]; 
this->m[3][2] = this->m[3][0] * rhs[0][2] + this->m[3][1] * rhs[1][2] + this->m[3][2] * rhs[2][2] + this->m[3][3] * rhs[3][2]; 
this->m[3][3] = this->m[3][0] * rhs[0][3] + this->m[3][1] * rhs[1][3] + this->m[3][2] * rhs[2][3] + this->m[3][3] * rhs[3][3]; 

私はちょうどそれが正しいかではなかったかどうかの確認を取得したい - 私は掛けたときに二つの行列Cで++(投影*ビュー行列)と結果の行列をシェーダに渡すと、画面に何も表示されません。

しかし、私は別にシェーダ投影&ビュー行列を与え、GLSLに彼らにを掛けた場合 - それはすべての素晴らしい作品、結果が予想されます。

行列乗算関数に何か問題がありますか?

+2

現場での乗算は行わないでください。正しく動作しません。 – Sneftel

+0

[4x4均質変換行列の理解](https://stackoverflow.com/a/28084380/2521214)を参照し、コード内で 'matrix_mul'を探します(ただし行列は1D配列のOpenGLスタイルの方法でエンコードされます)。また、他のセルの計算に必要な値を上書きするため、@Sneftelが示唆するように(行と列、または行と転置された行で)ドットプロダクトを利用することはできません...実際の行をバッファリングすることができますしかし、全体の行列の代わりに.... – Spektre

答えて

0

this->m[0][1] = this->m[0][0] * rhs[0][1] + this->m[0][1] * rhs[1][1] + this->m[0][2] * rhs[2][1] + this->m[0][3] * rhs[3][1]; 

はrhsで乗算してはいけません[1] [0] .. RHS [1] [3]?あなたはこの行を歩いていくうちに、あなたはrhsの列を踏んでいません。

+0

ねえ、ありがとう、ええ..代わりにループの代わりに、より少ないコードとより信頼性の高い。乾杯 – oglglslnoob