iOS:据我所知,GLKit 矩阵乘法已损坏

发布于 2024-12-29 06:54:21 字数 2191 浏览 1 评论 0原文

这是源代码。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2025-01-05 06:54:21

嗯,我得到了与你的“结果”矩阵相同的结果。

我用笔和纸进行了矩阵乘法,遵循以下规则:

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:

enter image description here

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:

enter image description here

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:

enter image description here

小伙你站住 2025-01-05 06:54:21

关于这一点的更多细节。

OpenGL ES 假设矩阵具有列主序。所以 OpenGL ES 读取矩阵的元素顺序是:

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

当然 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:

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文