title: Mapping a texture to an imported object Opengl
body: <p>I'm having trouble mapping a texture to an object imported into Opengl. I found a tutorial to import an obj file, but I can't figure out mapping a texture. </p>
  
  <p>This is the code I have written:</p>
  
  <pre><code>    class Model
      {
      public:
          Model();
          Model(string modelFilename, string textureFilename);
  
          void LoadTexture(string fileName);
          void LoadObj(const string&amp; filename, vector&lt;glm::vec4&gt; &amp;vertices, vector&lt;glm::vec3&gt; &amp;normals, vector&lt;GLushort&gt; &amp;elements);
          void Draw();
      private:
          vector&lt;glm::vec4&gt; m_Vertices;
          vector&lt;glm::vec3&gt; m_Normals;
          vector&lt;GLushort&gt; m_Elements;
  
          string m_ModelFilename;
          int m_Texture;
          string m_TextureName;
      };
  
  Model::Model(string modelFilename, string textureFilename)
  {
      m_ModelFilename = modelFilename;
      m_TextureName = textureFilename;
  
      LoadObj(m_ModelFilename, m_Vertices, m_Normals, m_Elements);
      LoadTexture(m_TextureName);
  }
  
  void Model::LoadTexture(string TextureName)         
  {
     // Local storage for bmp image data.
     BitMapFile *image[1];
  
          string filename = TextureName;
          filename.append(".bmp");
  
         // Load the texture.
         image[0] = getBMPData(filename);
  
         // Bind grass image to texture index[i]. 
         glBindTexture(GL_TEXTURE_2D, m_Texture); 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image[0]-&gt;sizeX, image[0]-&gt;sizeY, 0, 
                      GL_RGB, GL_UNSIGNED_BYTE, image[0]-&gt;data);
  }
  
  void Model::LoadObj(const string&amp; filename, vector&lt;glm::vec4&gt; &amp;vertices, vector&lt;glm::vec3&gt; &amp;normals, vector&lt;GLushort&gt; &amp;elements) {
    ifstream in(filename, ios::in);
    if (!in) { cerr &lt;&lt; "Cannot open " &lt;&lt; filename &lt;&lt; endl; exit(1); }
  
    string line;
    while (getline(in, line)) {
      if (line.substr(0,2) == "v ") {
        istringstream s(line.substr(2));
        glm::vec4 v; s &gt;&gt; v.x; s &gt;&gt; v.y; s &gt;&gt; v.z; v.w = 1.0f;
        vertices.push_back(v);
      }  else if (line.substr(0,2) == "f ") {
        istringstream s(line.substr(2));
        GLushort a,b,c;
        s &gt;&gt; a; s &gt;&gt; b; s &gt;&gt; c;
        a--; b--; c--;
        elements.push_back(a); elements.push_back(b); elements.push_back(c);
      }
      else if (line[0] == '#') { /* ignoring this line */}
     // else { /* ignoring this line */ }
    }
  
    normals.resize(vertices.size(), glm::vec3(0.0, 0.0, 0.0));
    for (int i = 0; i &lt; elements.size(); i+=3) {
      GLushort ia = elements[i];
      GLushort ib = elements[i+1];
      GLushort ic = elements[i+2];
      glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(vertices[ib]) - glm::vec3(vertices[ia]), glm::vec3(vertices[ic]) - glm::vec3(vertices[ia])));
      normals[ia] = normals[ib] = normals[ic] = normal;
    }
  }
  
  void Model::Draw()
  {
          glEnable(GL_TEXTURE_2D);
          glBindTexture(GL_TEXTURE_2D, m_Texture);
  
          for (int i = 0; i &lt; m_Elements.size(); i+=3) 
          {
              GLushort ia = m_Elements[i];
              GLushort ib = m_Elements[i+1];
              GLushort ic = m_Elements[i+2];
  
              glBegin(GL_TRIANGLES);
                  glBegin(GL_TRIANGLES);
                  glNormal3f(m_Normals[ia].x,m_Normals[ia].y,m_Normals[ia].z);//
                  glNormal3f(m_Normals[ib].x,m_Normals[ib].y,m_Normals[ib].z);//
                  glNormal3f(m_Normals[ic].x,m_Normals[ic].y,m_Normals[ic].z);//
                  glTexCoord2f(0.0, 0.0);glVertex3f(m_Vertices[ia].x,m_Vertices[ia].y,m_Vertices[ia].z);
                  glTexCoord2f(1.0, 1.0);glVertex3f(m_Vertices[ib].x,m_Vertices[ib].y,m_Vertices[ib].z);
                  glTexCoord2f(0.0, 1.0);glVertex3f(m_Vertices[ic].x,m_Vertices[ic].y,m_Vertices[ic].z);
              glEnd();
              glEnd();
          }
          glDisable(GL_TEXTURE_2D);
  }
  
  Model model;
  
  void setup(void) 
  {
      glClearColor(1.0, 1.0, 1.0, 0.0); 
  
      model = Model("monkey.obj", "launch");
  
     // Specify how texture values combine with current surface color values.
     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
  
     glEnable(GL_BLEND); // Enable blending.
  
     // Cull back faces.
     glEnable(GL_CULL_FACE);
     glCullFace(GL_BACK);
  
     // stops GL_QUAD from showing faces by priority
      glEnable(GL_DEPTH_TEST);
  
  }
  
  // Drawing routine.
  void drawScene(void)
  {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();
  
     model.Draw();
  
     glutSwapBuffers();
  }
  </code></pre>
  
  <p>When i run the program this is what happens: <a href="http://imgur.com/okMul77" rel="nofollow">http://imgur.com/okMul77</a>
  It seems to be mapping the entire image to each triangle, but I don't know how to fix it. Anyone have any ideas? Thank you in advance</p>