iOS:据我所知,GLKit 矩阵乘法已损坏
这是源代码。 ANSWER
是正确答案,RESULT
是实际结果。
我是盲目的,还是它为条目 33
计算了 -1
,而它应该是 1
?
这是代码:
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(@"##################################################");
这是输出:
##################################################
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
##################################################
我花了过去 5 个小时尝试调试一些 OpenGL 代码,结果发现这就是问题所在。我肯定错过了一些东西。任何人都可以发现发生了什么,或者验证这不应该发生吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我得到了与你的“结果”矩阵相同的结果。
我用笔和纸进行了矩阵乘法,遵循以下规则:
http://www.mathsisfun.com/algebra/matrix-multiplying.html mathsisfun.com/algebra/matrix-multiplying.html
这就是我使用你的两个矩阵 a 和 b 所做的:
好吧,我想我知道你哪里出了问题,你一定把数字列在了错误的位置:
方式您在代码中列出的数字是水平的,例如坐标(x,y,z),但在矩阵中,这些数字被移动到垂直位置。
例如,[ XYZW ] 应该变成垂直的:
现在 IF 我们是将您的数字列出在错误的位置并将矩阵 a 与矩阵 b 相乘,我们得到与您的“答案”矩阵相同的答案:
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:
关于这一点的更多细节。
OpenGL ES 假设矩阵具有列主序。所以 OpenGL ES 读取矩阵的元素顺序是:
当然 Apple 的 GLKit Math Utilities 也是根据此列专业顺序构建的。当你意识到这一点时,可能会有点困惑。 :)
Little bit more details on this.
OpenGL ES suppose matrices have column major order. So element order how OpenGL ES reads matrix is:
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. :)