title: calculate normal per vertex OpenGL
body: <p>I am trying the calculate normal per vertex.
  But I do something wrong. When I run the code I see this:</p>
  
  <p><img src="https://i.stack.imgur.com/PZL9E.png" alt="enter image description here"></p>
  
  <p>Here Is my code, Note that vertex1 is vertex before the current vertex and vertex2 is vertex after the current vertex.</p>
  
  <pre><code>for (int j = 0; j &lt; meshes[t].face[i].numOfPoints; j++)
              {
                  if (normalSetChange)
                  {
                      vector3D vertex1, vertex2;
  
                      if ((j &lt; meshes[t].face[i].numOfPoints - 1) &amp;&amp; (j &gt; 0))
                      {
                          vertex1 = vertexes[meshes[t].face[i].vertex[j + 1]] - vertexes[meshes[t].face[i].vertex[j]];
                          vertex2 = vertexes[meshes[t].face[i].vertex[j - 1]] - vertexes[meshes[t].face[i].vertex[j]];
                      }
                      else if (j &lt; meshes[t].face[i].numOfPoints - 1)
                      {
                          vertex1 = vertexes[meshes[t].face[i].vertex[j + 1]] - vertexes[meshes[t].face[i].vertex[j]];
                          vertex2 = vertexes[meshes[t].face[i].vertex[meshes[t].face[i].numOfPoints - 1]] - vertexes[meshes[t].face[i].vertex[j]];
                      }
                      else if (j &gt; 0)
                      {
                          vertex1 = vertexes[meshes[t].face[i].vertex[0]] - vertexes[meshes[t].face[i].vertex[j]];
                          vertex2 = vertexes[meshes[t].face[i].vertex[j - 1]] - vertexes[meshes[t].face[i].vertex[j]];
                      }
  
                      normalSet = vector3D(vertex1.y * vertex2.z - vertex1.z * vertex2.y,
                                               vertex1.z * vertex2.x - vertex1.x * vertex2.z, 
                                               vertex1.x * vertex2.y - vertex1.y * vertex2.x);
  
                      normalLength = sqrt(normalSet.x * normalSet.x + normalSet.y * normalSet.y + normalSet.z * normalSet.z);
  
                      normalSet.x /= normalLength;
                      normalSet.y /= normalLength;
                      normalSet.z /= normalLength;
  
                      writePolygonLineVCN(PolygonLineVCN(vertexes[meshes[t].face[i].vertex[j]], vertexestexCoordinate[meshes[t].face[i].texCoordinate[j]], normalSet), newFile[workOnCPU]);
                  }
                  else
                      writePolygonLineVCN(PolygonLineVCN(vertexes[meshes[t].face[i].vertex[j]], vertexestexCoordinate[meshes[t].face[i].texCoordinate[j]], vertexesNormals[meshes[t].face[i].normal[j]]), newFile[workOnCPU]);
              }
  </code></pre>