简体   繁体   English

opengl矩阵数学乘法

[英]opengl matrix math multiplication

I am writing a simple c 4x4 matrix math library and wanted some feedback, especially from people with opengl experience. 我正在编写一个简单的c 4x4矩阵数学库,希望获得一些反馈,尤其是来自具有opengl经验的人。

Typically there's two ways to do matrix multiplication. 通常,有两种方法进行矩阵乘法。 I tested this code and it works, according to results from wolfram alpha but my main concern is that this matrix is in the right order. 根据Wolfram alpha的结果,我测试了此代码,并且该代码有效,但我主要担心的是,此矩阵的顺序正确。

My matrix is just an array of 16 doubles. 我的矩阵只是16个双打的数组。

The code to do the multiplication is below 下面是进行乘法的代码

out->m[0]  = ( a->m[0]  *  b->m[0]) + (a->m[1]  * b->m[4]) + (a->m[2]  *  b->m[8]) + (a->m[3]  * b->m[12] );
out->m[4]  = ( a->m[4]  *  b->m[0]) + (a->m[5]  * b->m[4]) + (a->m[6]  *  b->m[8]) + (a->m[7]  * b->m[12] );
out->m[8]  = ( a->m[8]  *  b->m[0]) + (a->m[9]  * b->m[4]) + (a->m[10] *  b->m[8]) + (a->m[11] * b->m[12] );
out->m[12] = ( a->m[12] *  b->m[0]) + (a->m[13] * b->m[4]) + (a->m[14] *  b->m[8]) + (a->m[15] * b->m[12] );

out->m[1]  = ( a->m[0]  *  b->m[1]) + (a->m[1]  * b->m[5]) + (a->m[2]  * b->m[9])  + (a->m[3]  * b->m[13] );
out->m[5]  = ( a->m[4]  *  b->m[1]) + (a->m[5]  * b->m[5]) + (a->m[6]  * b->m[9])  + (a->m[7]  * b->m[13] );
out->m[9]  = ( a->m[8]  *  b->m[1]) + (a->m[9]  * b->m[5]) + (a->m[10] * b->m[9])  + (a->m[11] * b->m[13] );
out->m[13] = ( a->m[12] *  b->m[1]) + (a->m[13] * b->m[5]) + (a->m[14] * b->m[9])  + (a->m[15] * b->m[13] );

out->m[2]  = ( a->m[0]  *  b->m[2]) + (a->m[1]  * b->m[6]) + (a->m[2]  * b->m[10]) + (a->m[3]  * b->m[14] );
out->m[6]  = ( a->m[4]  *  b->m[2]) + (a->m[5]  * b->m[6]) + (a->m[6]  * b->m[10]) + (a->m[7]  * b->m[14] );
out->m[10] = ( a->m[8]  *  b->m[2]) + (a->m[9]  * b->m[6]) + (a->m[10] * b->m[10]) + (a->m[11] * b->m[14] );
out->m[14] = ( a->m[12] *  b->m[2]) + (a->m[13] * b->m[6]) + (a->m[14] * b->m[10]) + (a->m[15] * b->m[14] );

out->m[3]  = ( a->m[0]  *  b->m[3]) + (a->m[1]  * b->m[7]) + (a->m[2]  * b->m[11]) + (a->m[3]  * b->m[15] );
out->m[7]  = ( a->m[4]  *  b->m[3]) + (a->m[5]  * b->m[7]) + (a->m[6]  * b->m[11]) + (a->m[7]  * b->m[15] );
out->m[11] = ( a->m[8]  *  b->m[3]) + (a->m[9]  * b->m[7]) + (a->m[10] * b->m[11]) + (a->m[11] * b->m[15] );
out->m[15] = ( a->m[12] *  b->m[3]) + (a->m[13] * b->m[7]) + (a->m[14] * b->m[11]) + (a->m[15] * b->m[15] );

I wanted to make sure that this will give me the correct results for setting up my transformation matrix. 我想确保这将为我设置转换矩阵提供正确的结果。

matrix m = 1,3,4,-1,5,6,7,-1,8,8,8,-1,0,0,0,1 which is arranged in memory like this: 矩阵m = 1,3,4,-1,5,6,7,-1,8,8,8,-1,0,0,0,1排列如下:

1,3,4,-1
5,6,7,-1
8,8,8,-1
0,0,0,1

which I think is the way opengl lays out it's matrix as 16 numbers. 我认为这是opengl以16个数字表示矩阵的方式。

using my code my answer comes out to be 使用我的代码我的答案出来是

[   48.000000   53.000000   57.000000   -9.000000   ]
[   91.000000   107.000000  118.000000  -19.000000  ]
[   112.000000  136.000000  152.000000  -25.000000  ]
[   0.000000    0.000000    0.000000    1.000000    ]

which is the transpose of wolfram alpha's answer. 这是Wolfram alpha答案的转置。

(48 | 91  | 112 | 0
 53 | 107 | 136 | 0
 57 | 118 | 152 | 0
 -9 | -19 | -25 | 1)

Typically it looks like this, vertex point v model, view, projection matrices 通常看起来像这样,顶点v模型,视图,投影矩阵

position = projection * view * model * v

I can't say you why your results differ but one help is, if you send the matrix into a GLSL uniform dMat4, you can use the build in transpose functionallity of OpenGL to get the right matrix alignment: 我不能说你为什么结果会有所不同,但是有一个帮助是,如果将矩阵发送到GLSL统一dMat4中,则可以使用OpenGL的内建转置功能来获得正确的矩阵对齐方式:

glUniformMatrix4fv( Uniform_Location, 1, GL_TRUE, MatrixPointer );

The third parameter means, if OpenGL should transpose the matrix before setting the uniform. 第三个参数表示,如果OpenGL在设置制服之前应该转置矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM