gpt4 book ai didi

java - matrix.multiply 返回 Nan

转载 作者:行者123 更新时间:2023-11-29 05:04:44 25 4
gpt4 key购买 nike

我知道 NaN 的意思不是数字,但我不明白我是如何得到这个错误的。我正在做的是引用来 self 的形状类的我的形状对象的顶点值,并在我的渲染器类中使用它们。当我在 logcat 上打印出顶点的值时,我看到它返回前 5 个顶点,然后所有其他顶点都是 NaN。如果您查看我的 logcat,则表明在调用 matrix.multiplyMM() 之前,顶点 (vertex1) 打印良好。但是在调用之后,顶点(shapeverts)开始变成 NaN。

for (int i = 0; i < Shape.vertices.length; i += Shape.COORDS_PER_VERTEX) {//only checking one vertex
Log.i("vertex1", String.valueOf(Shape.vertices[i] + ", " + Shape.vertices[i+1] + ", " + Shape.vertices[i+2] + ", " + Shape.vertices[i+3]));

Matrix.multiplyMM(shapeVerts, 0, mvp, 0, Shape.vertices, 0);//vertices multiplied by model view projection matrix

Log.i("shapevertBf", String.valueOf(shapeVerts[i] + ", " + shapeVerts[i+1] + ", " + shapeVerts[i+2] + ", " + shapeVerts[i+3]));

shapeVerts[i] = shapeVerts[i] / shapeVerts[i + 3]; //clip.x divided by clip.w
shapeVerts[i + 1] = shapeVerts[i + 1] / shapeVerts[i + 3];//clip.y divided by clip.w
shapeVerts[i + 2] = shapeVerts[i + 2] / shapeVerts[i + 3];//clip.y divided by clip.w

Log.i("shapevertAf", String.valueOf(shapeVerts[i] + ", " + shapeVerts[i+1] + ", " + shapeVerts[i+2] + ", " + shapeVerts[i+3]));

日志猫

  06-10 13:59:27.983  13121-13189/com.example.james.rollingsphere I/vertex1﹕ 0.0, 0.0, 9.0, 1.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 19.5, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ 0.0, 0.0, 1.625, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/vertex1﹕ 0.0, 0.0, 9.0, 1.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 19.5, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ 0.0, 0.0, 1.625, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/vertex1﹕ 0.0, 0.0, 9.0, 1.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 19.5, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ 0.0, 0.0, 1.625, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/vertex1﹕ 0.0, 0.0, 9.0, 1.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 19.5, 12.0
06-10 13:59:27.993 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ 0.0, 0.0, 1.625, 12.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/vertex1﹕ 0.0, 0.0, 9.0, 1.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 0.0, 0.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ NaN, NaN, NaN, 0.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/vertex1﹕ 5.290067, 0.0, 7.281153, 1.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 0.0, 0.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ NaN, NaN, NaN, 0.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/vertex1﹕ 1.634721, 5.031153, 7.281153, 1.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 0.0, 0.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ NaN, NaN, NaN, 0.0
06-10 13:59:28.003 13121-13189/com.example.james.rollingsphere I/vertex1﹕ -4.279755, 3.109423, 7.281153, 1.0
06-10 13:59:28.013 13121-13189/com.example.james.rollingsphere I/shapevertBf﹕ 0.0, 0.0, 0.0, 0.0
06-10 13:59:28.013 13121-13189/com.example.james.rollingsphere I/shapevertAf﹕ NaN, NaN, NaN, 0.0

编辑我更新了代码和 logcat。有趣的是,w 分量在 Matrix.multiply 之后从 12 开始,但随后变为 0。

最佳答案

问题是在这种情况下使用 Matrix.multiplyMM 没有意义。 multiplyMM 用于将两个矩阵相乘以获得矩阵乘积,而您想要做的是将一个矩阵(模型 View 投影矩阵)乘以一个 vector (您在 Shape.js 中未转换的顶点)。 vertices) 得到一个 vector (你在 shapeVerts 中转换的顶点)。

您只获得 5 个结果的原因是您将顶点数组视为一个 4x4 矩阵,而实际上它是一个顶点数组,每个顶点数组包含 3 个元素。一个 4x4 矩阵有 16 个元素,占用的空间与 5 1/3 顶点相同,因此您可以在到达未初始化数据并开始获取零(导致 NaN)等之前读出 5 个(无效)值。

要解决此问题,请使用 Matrix.multiplyMV将您的顶点坐标乘以模型 View 投影矩阵,将偏移量传递到正确的顶点,如下所示:

Matrix.multiplyMV(shapeVerts, i, mvp, 0, Shape.vertices, i);

关于java - matrix.multiply 返回 Nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30756639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com