iOS: GLKit matrix multiplication is broken as far as I can tell

[亡魂溺海] 提交于 2019-12-11 08:18:38

问题


Here's the source code. ANSWER is the correct answer, RESULT is the actual result.

Am I blind, or is it calculating a -1 for entry 33 when it should be a 1?

Here's the code:

GLKMatrix4 a = GLKMatrix4Make(-1.000000, 0.000000, 0.000000, 0.000000,
        0.000000, 1.000000, 0.000000, 0.000000,
        0.000000, 0.000000, -1.000000, 0.000000,
        0.000000, 0.000000, 0.000000, 1.000000);

GLKMatrix4 b = GLKMatrix4Make(1.000000, 0.000000, 0.000000, 0.000000,
        0.000000, 1.000000, 0.000000, -1.000000,
        0.000000, 0.000000, 1.000000, -1.000000,
        0.000000, 0.000000, 0.000000, 1.000000);

GLKMatrix4 ANSWER = GLKMatrix4Make(-1.000000, 0.000000, 0.000000, 0.000000,
        0.000000, 1.000000, 0.000000, -1.000000,
        0.000000, 0.000000, -1.000000, 1.000000,
        0.000000, 0.000000, 0.000000, 1.000000);

NSLog(@"##################################################");
GLKMatrix4 RESULT = GLKMatrix4Multiply(a,b);
NSLog(@"Result:");
NSLog(@"    %f %f %f %f",RESULT.m00,RESULT.m01,RESULT.m02,RESULT.m03);
NSLog(@"    %f %f %f %f",RESULT.m10,RESULT.m11,RESULT.m12,RESULT.m13);
NSLog(@"    %f %f %f %f",RESULT.m20,RESULT.m21,RESULT.m22,RESULT.m23);
NSLog(@"    %f %f %f %f",RESULT.m30,RESULT.m31,RESULT.m32,RESULT.m33);
NSLog(@"Answer:");
NSLog(@"    %f %f %f %f",ANSWER.m00,ANSWER.m01,ANSWER.m02,ANSWER.m03);
NSLog(@"    %f %f %f %f",ANSWER.m10,ANSWER.m11,ANSWER.m12,ANSWER.m13);
NSLog(@"    %f %f %f %f",ANSWER.m20,ANSWER.m21,ANSWER.m22,ANSWER.m23);
NSLog(@"    %f %f %f %f",ANSWER.m30,ANSWER.m31,ANSWER.m32,ANSWER.m33);
NSLog(@"##################################################");

Here's the output:

##################################################
Result:
    -1.000000 0.000000 0.000000 0.000000
    0.000000 1.000000 0.000000 -1.000000
    0.000000 0.000000 -1.000000 -1.000000
    0.000000 0.000000 0.000000 1.000000
Answer:
    -1.000000 0.000000 0.000000 0.000000
    0.000000 1.000000 0.000000 -1.000000
    0.000000 0.000000 -1.000000 1.000000
    0.000000 0.000000 0.000000 1.000000
##################################################

I've spent the last 5 hours trying to debug some OpenGL code only to find this is the problem. I must be missing something, surely. Can anyone spot what's going on, or verify this shouldn't be happening?


回答1:


Um, I got the same result as your "RESULT" matrix.

I did the matrix multiplication with pen and paper, following these rules:

http://www.mathsisfun.com/algebra/matrix-multiplying.html

This is how I did it using your two matrix a and b:

OK, I think I know where you went wrong, you must have listed the numbers in the wrong position:

The way you list the number in code is horizontal e.g Coordinate(x,y,z) but in a Matrix, these numbers get moved to vertical positions.

So for example, [ X Y Z W ] should become vertically:

Now IF we were to list the your numbers in the wrong position and multiply Matrix a with Matrix b, we get the same answer as your "ANSWER" matrix:




回答2:


Little bit more details on this.

OpenGL ES suppose matrices have column major order. So element order how OpenGL ES reads matrix is:

m00 m10 m20 m30
m01 m11 m21 m31
m02 m12 m22 m32
m03 m13 m23 m33

Of course Apple's GLKit Math Utilities are also built according to this column majored order. And it may be little bit confusing while you realise it. :)



来源:https://stackoverflow.com/questions/9029796/ios-glkit-matrix-multiplication-is-broken-as-far-as-i-can-tell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!