title: obj file - averaging normals
body: <p>I have a obj file which stores data this way:</p>
  
  <pre><code>v value1 value2 value3  
  f value1 value2 value3
  </code></pre>
  
  <p>First I calculate a normal for the face and then assign for each vertex of that face:</p>
  
  <pre><code>for(int i = 0; i &lt; verticesInd.size(); i+=3)
  {
      glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(vertices[verticesInd[i + 1]]) - glm::vec3(vertices[verticesInd[i]]), glm::vec3(vertices[verticesInd[i + 2]]) - glm::vec3(vertices[verticesInd[i]])));
      out_Normals[i] = normal;
      out_Normals[i + 1] = normal;
      out_Normals[i + 2] = normal;
  }
  </code></pre>
  
  <p>For achieve a flat shading I can duplicate vertices:</p>
  
  <pre><code>for(int i = 0; i &lt; verticesInd.size(); i++)
  {
      out_Vertices.push_back(vertices[verticesInd[i]]);
  } 
  </code></pre>
  
  <p>and then draw the object using glDrawArrays:</p>
  
  <pre><code>glDrawArrays(GL_TRIANGLES, 0, out_Vertices.size());
  </code></pre>
  
  <p>For achieve a smooth shading I need to average normals for each vertex but I have no idea how to find adjacent faces.</p>
  
  <p><strong>Edit1:</strong> I didn't notice a single s parameter before f:</p>
  
  <pre><code>v value1 value2 value3
  s 1  
  f value1 value2 value3
  </code></pre>
  
  <p><strong>Edit2:</strong> Normals averaging</p>
  
  <pre><code>glm::vec3 tNormal;
  for(int i = 0; i &lt; vertices.size(); i++)
  {
      for(int  j = 0; j &lt; verticesInd.size(); j++)
      {
          if(verticesInd[j] == i)
          {
              tNormal += faceNormals[j / 3];
          }
      }
      aNormals.push_back(glm::normalize(tNormal));
      tNormal = glm::vec3(0,0,0);
  }
  </code></pre>
  
  <p><strong>Edit 3</strong> Face normals:</p>
  
  <pre><code>for(int i = 0; i &lt; verticesInd.size(); i+=3)
  {
      glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(vertices[verticesInd[i + 1]]) - glm::vec3(vertices[verticesInd[i]]), glm::vec3(vertices[verticesInd[i + 2]]) - glm::vec3(vertices[verticesInd[i]])));
      faceNormals.push_back(normal);
  }
  </code></pre>