Normalizing Normals

so here is some code i stole borrowed from ClothPath for calculating normals



for(int i = 0; i<triangleCount; i++){
         
         i1 = indexBuffer.get();
         i2 = indexBuffer.get();
         i3 = indexBuffer.get();
         
         BufferUtils.populateFromBuffer(v1, vertexBuffer, i1);
         BufferUtils.populateFromBuffer(v2, vertexBuffer, i2);
         BufferUtils.populateFromBuffer(v3, vertexBuffer, i3);
         
         getTriangleNormal(v1,v2,v3,norm);
   
         BufferUtils.addInBuffer(norm, normalBuffer, i1);
         BufferUtils.addInBuffer(norm, normalBuffer, i2);
         BufferUtils.addInBuffer(norm, normalBuffer, i3);
      }
                //This iteration is here but i shouldn't have to call it... should i??
      for(int i = 0; i<getBatch(0).getVertexCount(); i++)
         BufferUtils.normalizeVector3(normalBuffer, i);



seems pretty straight forward, get the three vertecies of a triangle, calculate the triangle's normal and whala! you have the normal for the 3 verticies. But here comes the weird part.
getTriangleNormal() stores the normalized unit vector in norm. I checked it, if i add a System.out.println(norm.lengthSquared()); it usually gives a length 1 or something close.... But when i load the scene and check the normals (by pressing 'N' ) they seem to go to infinity... So, i have to make a another inorder to fix it, where i use bufferutils to normalize the enter buffer. But this shouldn't really be needed, should it? What am i doing wrong?

Debugger does assume your normals are "normalized".  If your mesh has a bounding volume, the volume is used to scale the normals relative to the total size of the mesh.  If it does not have a bounding volume, are you working at a small scale such that 1.0 units is really big in relation to your scene?




well i noticed that my bounding volume was incorrect wrong, so i fixed that. but the issue still remains, my normals still go off to infinity.

I'm pretty its not the debugger though, because the lightning doesn't even work, unless i call normalizeVector3.

When you say they go to infinity you mean the say pointing to the center of the screen? If so, most likely it means you have a zero normal (i.e. 0,0,0)

no they point off the screen up up and away… here is a picture…









also with the weird normals, it now renders weirdly, and the funny thing is… i don’t remember changing anything, but now there is a new side effect, where it renders with like black bars going through and some weird stuff happening… (hard to explain, screen shot didn’t capture it) but turning on debugging normals seemed to fix the issue, until i turned the debug off again… anyway, any one got any ideas?


I have two comments:



  • Problems that are fixed by turning on/off debugging are many times threading issues. Even when your application is not exactly multi-threaded, you have to interact with the GL thread, so it is still a possibility.

  • There has been some reports on other threads about strange things happening with LWJGL version incompatibilities. You should try updating your graphics drivers, redownloading jME or things like that.