Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Normal Matrix

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

enter image description here

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?

like image 291
Ruuhkis Avatar asked Apr 10 '26 02:04

Ruuhkis


1 Answers

I don't where you could find a Matrix library in Java to help you with this.

But, as long as your modelView matrix does not contain non-uniform scales, you can safely use your modelView matrix instead of the normalMatrix.

This could help you get started and make sure that your problem is not hidden elsewhere.

like image 55
rotoglup Avatar answered Apr 12 '26 08:04

rotoglup