title: Little problem while calculating vertex normals (OpenGL ES)
body: <p>I'm having a little problem with the vertex normal.</p>
  
  <p>The result seems a little strange. First a look at the images:</p>
  
  <p><strong>Original model in 3D software.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/DPiWh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DPiWh.png" alt=""></a>
  </p>
  
  <p><strong>Per-Fragment lighting, with mesh imported directly from an OBJ file (using normals from file).</strong></p>
  
  <p><a href="https://i.stack.imgur.com/zFLkZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zFLkZ.png" alt=""></a>
  </p>
  
  <p><strong>Per-Fragment lighting, calculating the normals with my own routine.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/931J5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/931J5.png" alt=""></a></p>
  
  <p>Did you notice?
  Using the normals from OBJ file everything is OK. But when I use my own routine to calculate the normals based on vertices something seems wrong.</p>
  
  <p>The normals seems strange exactly when the Z becomes negative (using a Right-Hand orientation). On the other parts of the object everything is OK, but just one imaginary line across the mesh seems strange. This problem persists on others meshes, always at the same place, the Z = 0.</p>
  
  <p>My routine to calculate the normals is that one, which everyone knows, assuming a triangle with vertices ABC:</p>
  
  <pre><code>vec3 normal = normalize(cross(C - A, B - A));
  </code></pre>
  
  <p>And then adding the normal result to an already calculated normal buffer.</p>
  
  <p>I've seen some guys saying about calculate the area of each triangle and multiply it by the normal or even checking against the dot product between the normal on the buffer and the new normal, but I've already tried those approachs and I caught just little changes, but still with the same problem.</p>
  
  <p>Did you seen this before? Do you know how to solve that?</p>
  
  <p>Here is some code:</p>
  
  <pre><code>// Looping at each triangle
  vec3 distBA = vec3Subtract(vB, vA);
  vec3 distCA = vec3Subtract(vC, vA);
  
  vec3 normal = vec3Cross(distBA, distCA);
  
  // Each normalBuffer represents one vertex.
  normalBuffer[i1] = vec3Add(normal, normalBuffer[i1]);
  normalBuffer[i2] = vec3Add(normal, normalBuffer[i2]);
  normalBuffer[i3] = vec3Add(normal, normalBuffer[i3]);
  
  // After pass through all faces/triangles.
  
  // Looping at each Vertex structure.
  normal = vec3Normalize(normalBuffer[i]);
  </code></pre>