title: Trouble adding normals to .obj mesh
body: <p>I've created a shape in Blender (I've made sure to triangulate faces and add normals) which I've exported in .obj format to use in an openframeworks project. I've also wrote a small class to parse this .obj file. The shape is drawing perfectly, but I can't seem to get the normals to apply correctly.</p>
  
  <p>The export only contains vertices, normals, and faces. As dictated by the .obj format: I add vertices to a mesh and use the supplied indices to draw faces. The normals, and indices for normals, are supplied in a similar fashion. Indicated by the image below, I'm not applying them correctly.</p>
  
  <p><img src="https://i.stack.imgur.com/ol6iJ.jpg" alt="enter image description here"></p>
  
  <p>Here's my model loader script:</p>
  
  <p>modelLoader.h</p>
  
  <pre><code>#ifndef _WAVEFRONTLOADER
  #define _WAVEFRONTLOADER
  
  #include "ofMain.h"
  
  class waveFrontLoader {
  
  public:
  
  ofMesh mesh;
  
  waveFrontLoader();
  ~waveFrontLoader();
  
  void loadFile(char *fileName);
  ofMesh generateMesh();
  
  private:
  
  typedef struct
  {
      ofIndexType v1,v2,v3;
      ofIndexType vn1,vn2,vn3;
  }
  Index;
  
  std::vector&lt;ofVec3f&gt; vertices;
  std::vector&lt;ofVec3f&gt; normals;
  std::vector&lt;Index&gt; indices;
  
  void parseLine(char *line);
  void parseVertex(char *line);
  void parseNormal(char *line);
  void parseFace(char *line);
  };
  
  #endif
  </code></pre>
  
  <p>modelLoader.cpp</p>
  
  <pre><code>#include "waveFrontLoader.h"
  
  waveFrontLoader::waveFrontLoader()
  {
  
  }
  
  void waveFrontLoader::loadFile(char *fileName)
  {
  ifstream file;
  char line[255];
  
  //open file in openframeworks data folder
  file.open(ofToDataPath(fileName).c_str());
  
  if (file.is_open())
  {
      while (file.getline(line,255))
      {
          parseLine(line);
      }
  }
  }
  
  void waveFrontLoader::parseLine(char *line)
  {
  //If empty, don't do anything with it
  if(!strlen(line))
  {
      return;
  }
  
  //get line type identifier from char string
  char *lineType = strtok(strdup(line), " ");
  
  //parse line depending on type
  if (!strcmp(lineType, "v")) // Vertices
  {
      parseVertex(line);
  }
  else if (!strcmp(lineType, "vn")) // Normals
  {
      parseNormal(line);
  }
  else if (!strcmp(lineType, "f")) // Indices (Faces)
  {
      parseFace(line);
  }
  }
  
  void waveFrontLoader::parseVertex(char *line)
  {
  float x;
  float y;
  float z;
  
  vertices.push_back(ofVec3f(x,y,z));
  
  //get coordinates from vertex line and assign
  sscanf(line, "v %f %f %f", &amp;vertices.back().x, &amp;vertices.back().y, &amp;vertices.back().z);
  }
  
  void waveFrontLoader::parseNormal(char *line)
  {
  float x;
  float y;
  float z;
  
  normals.push_back(ofVec3f(x,y,z));
  
  //get coordinates from normal line and assign
  sscanf(line, "vn %f %f %f", &amp;normals.back().x, &amp;normals.back().y, &amp;normals.back().z);
  }
  
  void waveFrontLoader::parseFace(char *line)
  {
  indices.push_back(Index());
  
  //get vertex and normal indices
  sscanf(line, "f %d//%d %d//%d %d//%d",
         &amp;indices.back().v1,
         &amp;indices.back().vn1,
         &amp;indices.back().v2,
         &amp;indices.back().vn2,
         &amp;indices.back().v3,
         &amp;indices.back().vn3);
  }
  
  ofMesh waveFrontLoader::generateMesh()
  {
  //add vertices to mesh
  for (std::vector&lt;ofVec3f&gt;::iterator i = vertices.begin(); i != vertices.end(); ++i)
  {
      mesh.addVertex(*i);
  }
  
  //add indices to mesh
  for (std::vector&lt;Index&gt;::iterator i = indices.begin(); i != indices.end(); ++i)
  {
      // -1 to count from 0
      mesh.addIndex((i-&gt;v1) - 1);
      mesh.addIndex((i-&gt;v2) - 1);
      mesh.addIndex((i-&gt;v3) - 1);
  
      mesh.addNormal(normals[(i-&gt;vn1) - 1]);
      mesh.addNormal(normals[(i-&gt;vn2) - 1]);
      mesh.addNormal(normals[(i-&gt;vn3) - 1]);
  
  }
  
  return mesh;
  }
  
  waveFrontLoader::~waveFrontLoader()
  {
  
  }
  </code></pre>
  
  <p>I've tried adding normals like this as well (makes sense since it's one normal per face):</p>
  
  <pre><code>mesh.addNormal(normals[(i-&gt;vn1) - 1]);
  </code></pre>
  
  <p>I've also tried adding normals only once per two triangles being drawn, and tried adding indices and normals before the indices. Neither of those worked either.</p>
  
  <p>testApp.h</p>
  
  <pre><code>#pragma once
  
  #include "ofMain.h"
  #include "waveFrontLoader.h"
  
  class testApp : public ofBaseApp{
  
  public:
      void setup();
      void update();
      void draw();
      void exit();
  
      waveFrontLoader *objectLoader;
      ofMesh mesh;
      ofEasyCam camera;
      ofLight light;
  };
  </code></pre>
  
  <p>testApp.cpp</p>
  
  <pre><code>#include "testApp.h"
  
  //--------------------------------------------------------------
  void testApp::setup()
  {
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  
  ofBackground(10, 10, 10);
  
  camera.setDistance(10);
  light.setPosition(10,30,-25);
  
  objectLoader = new waveFrontLoader();
  objectLoader-&gt;loadFile("test.obj");
  mesh = objectLoader-&gt;generateMesh();
  }
  
  //--------------------------------------------------------------
  void testApp::update()
  {
  
  }
  
  //--------------------------------------------------------------
  void testApp::draw()
  {
  camera.begin();
  light.enable();
  mesh.draw();
  light.disable();
  camera.end();
  }
  
  void testApp::exit()
  {
  delete objectLoader;
  }
  </code></pre>