title: How to draw data from OBJ File
body: <p>I am using Xcode. I was able to load the data from an OBJ file. Whenever I try to use these data to draw my model. it gives me Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT). Can someone please tell me what is wrong with my code????</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;GLUT/glut.h&gt;
  
  using namespace std;
  
  // Model Structure
  struct Model {
  int vertices;
  int positions;
  int texels;
  int normals;
  int faces;
  };
  
  static float SpinX = 0.0f, SpinZ = 0.0f;
  static int MousePosX = 0, MousePosY = 0;
  
  string fullpath = "/Users/Desktop/cube.obj";
  
  int p = 0;
  int t = 0;
  int n = 0;
  int f = 0;
  
  
  Model getOBJinfo (string fp){
  Model model = {0};
  
  ifstream inOBJ;
  inOBJ.open(fp);
  if(!inOBJ.good()){
      cerr &lt;&lt; "ERROR OPENING OBJ FILE" &lt;&lt; endl;
      exit(1);
  }
  
  while(!inOBJ.eof()){
      string line;
      getline(inOBJ, line);
      string type = line.substr(0, 2);
  
      if(type.compare("v ") == 0)
          model.positions++;
      else if (type.compare("vt") == 0)
          model.texels++;
      else if (type.compare("vn") == 0)
          model.normals++;
      else if (type.compare("f ") == 0)
          model.faces++;
  }
  
  model.vertices = model.faces*3;
  
  inOBJ.close();
  
  return model;
  }
  
  Model model = getOBJinfo(fullpath);
  
  void extractOBJdata(string fp, float positions[][3], float texels[][2], float normals[][3], int faces[][9]){
  
  ifstream inOBJ;
  inOBJ.open(fp);
  if(!inOBJ.good()){
      cerr &lt;&lt; "ERROR OPENING OBJ FILE" &lt;&lt; endl;
      exit(1);
  }
  
  while(!inOBJ.eof()){
      string line;
      getline(inOBJ, line);
      string type = line.substr(0, 2);
  
      if(type.compare("v ") == 0){
          char* l = new char[line.size()+1];
          memcpy(l, line.c_str(), line.size()+1);
  
          strtok(l, " ");
          for (int i = 0; i &lt; 3 ; i++)
              positions[p][i] = atof(strtok(NULL, " "));
  
          delete [] l;
          p++;
      }
  
      else if (type.compare("vt") == 0){
          char* l = new char[line.size()+1];
          memcpy(l, line.c_str(), line.size()+1);
  
          strtok(l, " ");
          for (int i = 0; i &lt; 2 ; i++)
              texels[t][i] = atof(strtok(NULL, " "));
  
          delete [] l;
          t++;
      }
  
      else if (type.compare("vn") == 0){
          char* l = new char[line.size()+1];
          memcpy(l, line.c_str(), line.size()+1);
  
          strtok(l, " ");
          for (int i = 0; i &lt; 3 ; i++)
              normals[n][i] = atof(strtok(NULL, " "));
  
          delete [] l;
          n++;
      }
  
      /*else if (type.compare("f ") == 0){
          char* l = new char[line.size()+1];
          memcpy(l, line.c_str(), line.size()+1);
  
          strtok(l, " ");
          for (int i = 0; i &lt; 9 ; i++)
              faces[f][i] = atof(strtok(NULL, " /"));
  
          delete [] l;
          f++;
      }*/
  }
  
  inOBJ.close();
  }
  
  void init(void){
  
  glEnable(GL_DEPTH_TEST);
  
  }
  
  void mydraw(void){
  
  float positions[model.positions][3];            // XYZ
  float texels[model.texels][2];                  // UV
  float normals[model.normals][3];                // XYZ
  int faces[model.faces][9];                      // PTN PTN PTN
  
  extractOBJdata(fullpath, positions, texels, normals, faces);
  
  glBegin(GL_TRIANGLES);
  
  glVertex3f(positions[0][0], positions[0][1], positions[0][2]);
  glVertex3f(positions[1][0], positions[1][1], positions[1][2]);
  glVertex3f(positions[2][0], positions[2][1], positions[2][2]);
  glVertex3f(positions[3][0], positions[3][1], positions[3][2]);
  glVertex3f(positions[4][0], positions[4][1], positions[4][2]);
  glVertex3f(positions[5][0], positions[5][1], positions[5][2]);
  
  
  /*for (int i = 0; i &lt; t; i++){
  
  }
  
  for (int i = 0; i &lt; n; i++){
      glNormal3f(normals[i][0], normals[i][1], normals[i][2]);
  }
  
  for (int i = 0; i &lt; f; i++){
  
  }*/
  
  glEnd();
  
  glFlush();
  }
  
  void display(void){
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  gluLookAt(0.0f, 0.0f, 10.f,         // Eye Position
            0.0f, 0.0f, 0.0f,         // Center of interest
            0.0f, 1.0f, 0.0f);        //Camera Up Vector
  
  glRotatef(SpinZ, 1, 0, 0);
  glRotatef(SpinX, 0, 0, 1);
  
  mydraw();
  
  glutSwapBuffers();
  }
  
  void reshape(int width, int height){
  double aspect;
  
  glViewport(0, 0, width, height);
  
  aspect = (double)width / (double) height;
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  
  if(aspect &lt; 1.0) glOrtho(-4., 4., -4./aspect, 4./aspect, 1., 10);
  else glOrtho(-4.*aspect, 4.*aspect, -4., 4., 1., 10);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  gluLookAt(0., 0., 5., 0., 0., 0., 0., 1., 0.);
  }
  
  void mousepress(int button, int state, int x, int y){
  switch (state){
      case GLUT_DOWN: {MousePosX = x; MousePosY = y; break;}
  }
  }
  
  void mousemotion(int x, int y){
  SpinX += (float)(x-MousePosX)*0.1f;
  SpinZ += (float)(y-MousePosY)*0.1f;
  MousePosX = x;
  MousePosY = y;
  }
  
  void keypress(unsigned char key, int x, int y){
  
  }
  
  int main(int argc, char **argv) {
  cout &lt;&lt; "Model Info" &lt;&lt; endl;
  cout &lt;&lt; "----------" &lt;&lt; endl;
  cout &lt;&lt; "Positions: " &lt;&lt; model.positions &lt;&lt; endl;
  cout &lt;&lt; "Texels: " &lt;&lt; model.texels &lt;&lt; endl;
  cout &lt;&lt; "Normals: " &lt;&lt; model.normals &lt;&lt; endl;
  cout &lt;&lt; "Faces: " &lt;&lt; model.faces &lt;&lt; endl;
  cout &lt;&lt; "Vertices: " &lt;&lt; model.vertices &lt;&lt; endl;
  
  /*float positions[model.positions][3];            // XYZ
  float texels[model.texels][2];                  // UV
  float normals[model.normals][3];                // XYZ
  int faces[model.faces][9];                      // PTN PTN PTN
  
  extractOBJdata(fullpath, positions, texels, normals, faces);
  cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
  cout &lt;&lt; "Model Data" &lt;&lt; endl;
  cout &lt;&lt; "----------" &lt;&lt; endl;
  cout &lt;&lt; "P1: " &lt;&lt; positions[0][0] &lt;&lt; "x " &lt;&lt; positions[0][1] &lt;&lt; "y " &lt;&lt; positions[0][2] &lt;&lt; "z" &lt;&lt; endl;
  cout &lt;&lt; "T1: " &lt;&lt; texels[0][0] &lt;&lt; "u " &lt;&lt; texels[0][1] &lt;&lt; "v" &lt;&lt; endl;
  cout &lt;&lt; "N1: " &lt;&lt; normals[0][0] &lt;&lt; "x " &lt;&lt; normals[0][1] &lt;&lt; "y " &lt;&lt; normals[0][2] &lt;&lt; "z" &lt;&lt; endl;
  cout &lt;&lt; "F1v1: " &lt;&lt; faces[0][0] &lt;&lt; "p " &lt;&lt; faces[0][1] &lt;&lt; "t " &lt;&lt; faces[0][2] &lt;&lt; "n" &lt;&lt; endl;*/
  
  glutInit(&amp;argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("OBJ File");
  
  glutDisplayFunc(display);
  
  glutReshapeFunc(reshape);
  
  glutKeyboardFunc(keypress);
  
  glutMouseFunc(mousepress);
  
  init();
  
  glutMainLoop();
  return 0;
  }
  </code></pre>