title: Fastest way to load arrays of vertices and face indices into OpenGL-ES?
body: <p>I'm trying to load .obj files that I've formatted into:</p>
  
  <pre><code>vertexX vertexY vertexZ normalX normalY normalZ
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>index1 index2 index3
  </code></pre>
  
  <p>format into vector and vector arrays, which I then directly render in Opengl-ES. My problem is, when I try to load the model into the arrays, it takes about 40 seconds to load them in. I'm not sure why it's going so slow, I've seen others code load the same model in just a few seconds. Any suggestions? My code for loading the file is below:</p>
  
  <pre><code>-(void)loadModel:(NSString*)filePath
  {
      try {
  
      ifstream objFile([filePath UTF8String]);
      objFile &gt;&gt; numVertices;
      objFile.ignore(128, '\n');
      vertices.resize(numVertices*6);
      VertexNormal* vertex = (VertexNormal*) &amp;vertices[0];
      svec3* faceDef;
  
      while (objFile) {
          char c = objFile.get();
  
          switch (c) {
              case 'v':
              {
                  objFile &gt;&gt; vertex-&gt;vertices.x &gt;&gt; vertex-&gt;vertices.y &gt;&gt; vertex-&gt;vertices.z
                  &gt;&gt; vertex-&gt;normals.x &gt;&gt; vertex-&gt;normals.y &gt;&gt; vertex-&gt;normals.z;
                  vertex++;
                  break;
              }
              case 'f':
              {
                  objFile &gt;&gt; faceDef-&gt;x &gt;&gt; faceDef-&gt;y &gt;&gt; faceDef-&gt;z;
                  faceDef++;
                  break;
              }
              case '#':
              {
                  part newPart;
                  partList.push_back(newPart);
                  numObjects++;
                  objFile.ignore(128, '\n');
                  int numFaces;
                  objFile &gt;&gt; numFaces;
                  partList[partList.size()-1].faces.resize(numFaces*3);
                  partList[partList.size()-1].numFaces = numFaces;
                  faceDef = (svec3*) &amp;partList[partList.size()-1].faces[0];
                  break;
              }
              default:
                  break;
          }
          objFile.ignore(128, '\n');
      }
      objFile.close();
      free(objFile);
      } catch (NSException *ex) {
          NSLog(@"%@", [ex reason]);
      }
  }
  </code></pre>
  
  <p>One line of thought I had was to serialize the arrays into a binary file, then just deserialize them straight into my program. Haven't figured out how to do that yet though, but maybe something along those lines might be a solution.</p>