matrix.multiply returning Nan

473 views Asked by At

I know that NaN means Not a number but I dont understand how Im getting the error. What im doing is referencing the values of the vertices of my shape object that are from my shape class and using them here in my renderer class. When I print out the values for the vertices on the logcat I see that it returns the first 5 vertices and then every other vertice is NaN. If you look at my logcat is shows that before the call to matrix.multiplyMM() the vertices (vertex1) are printed fine. But after the call, the vertices (shapeverts) start becoming 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

Edit I updated the code and the logcat. Interestly the w component starts off as 12 after the Matrix.multiply but then becomes 0.

2

There are 2 answers

0
samgak On BEST ANSWER

The problem is that it doesn't make sense to use Matrix.multiplyMM in this case. multiplyMM is for multiplying two matrices together to get a matrix product, whereas what you want to do is multiply a matrix (the model view projection matrix) by a vector (your untransformed vertex in Shape.vertices) to get a vector (your transformed vertex in shapeVerts).

The reason you are getting only 5 results is that you are treating your vertex array as if it was a 4x4 matrix when it is actually an array of vertices with 3 elements each. A 4x4 matrix has 16 elements, which takes up the same amount of space as 5 1/3 vertices, so you can read out 5 (invalid) values before you reach uninitialized data and start getting zeros (which leads to NaNs) etc.

To fix the problem, use Matrix.multiplyMV to multiply your vertex co-ordinate by the model view projection matrix, passing in an offset to the correct vertex, like this:

Matrix.multiplyMV(shapeVerts, i, mvp, 0, Shape.vertices, i);
1
Kalyan Chavali On

When dividing doubles in java which are 0.0, we would get result as NaN.

You can refer to the below Java documentation

http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#NaN