我知道NaN意味着不是一个数字,但我不明白我是如何得到错误的。我正在做的是引用我的形状对象的顶点的值,这些顶点来自我的形状类,并在我的渲染器类中使用它们。当我在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]));
logcat的
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。
答案 0 :(得分:2)
问题是在这种情况下使用Matrix.multiplyMM
没有意义。 multiplyMM
用于将两个矩阵相乘以得到矩阵乘积,而您想要做的是将矩阵(模型视图投影矩阵)乘以向量(Shape.vertices
中未转换的顶点)得到一个向量(你在shapeVerts
中转换的顶点)。
你只获得5个结果的原因是你将顶点数组视为4x4矩阵,而实际上它是一个顶点数组,每个顶点有3个元素。 4x4矩阵有16个元素,占用的空间与5 1/3顶点相同,因此您可以在达到未初始化数据并开始获取零(导致NaN)等之前读出5个(无效)值。 / p>
要解决此问题,请使用Matrix.multiplyMV将顶点坐标乘以模型视图投影矩阵,将偏移量传递给正确的顶点,如下所示:
Matrix.multiplyMV(shapeVerts, i, mvp, 0, Shape.vertices, i);
答案 1 :(得分:1)
当在java中将双精度数除以0.0时,我们会得到结果为NaN。
您可以参考以下Java文档
http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#NaN