I've been trying to create simple diffuse lighting shader for Android using OpenGL ES 2.0 and getting knowledge from OpenGL 4.0 Shading language cookbook but it doesn't tell much about normal matrix and I am pretty sure the problem comes from it because the "model" I've been using is working perfectly in WebGL where I can use that nice glMatrix lib that I can't find for Java.
I am not sure how can I get normal matrix from model view matrix but I read its just transpose of inverted 3x3 piece of modelview matrix, too bad Android Matrix class only lets you work with 4x4 matrices(right?) so I've been splitting the matrix up in the shader which is probably where I go wrong.
So what I do is simply this:
float[] nMatrix = new float[4 * 4];
Matrix.invertM(nMatrix, 0, mvMatrix, 0);
Matrix.transposeM(nMatrix, 0, nMatrix, 0);
glUniformMatrix4fv(shader.getUniformPointer("nMatrix"), 1, false, nMatrix, 0);
and then at my vertex shader I do this:
tNorm = normalize(mat3(nMatrix) * vNormal).xyz;
and the rest of the code is basically from the book and the results are below
As you can see some sides of the cube are compeltely dark, and I am sure I got all the normals even thought I don't know any Android GL debugger, but if you know one, feel free to tell me about it.
So the question is, how can I properly get the normal matrix from my modelview matrix?