title: Exception at glDrawElements
body: <p>I'm trying to load a model into my project and I get an exception at <code>glDrawElements</code>.
  I read the model file (.nfg), and retain the vertices and indices into vectors, and I use Vertex Buffer Object to bind my model.</p>
  
  <p>I tried <a href="https://stackoverflow.com/questions/4342943/opengl-3-x-access-violation-when-using-vertex-buffer-object-and-gldrawelements">this</a>:
  I modified the fourth parameter from <code>(GLvoid*)(sizeof(Vector3) * x)</code>
  to <code>(GLvoid*)(offset(Vertex, attribute))</code>, but didn't do anything (in the link, the problem was that he was sending memory address in the 4th parameter, and I thought maybe I was sending the wrong parameter to the wrong attribute, which still, would be a problem when actually showing the model).</p>
  
  <p>I'm using OpenGL ES 2.0 and I'm not doing this project for neither Android or iOS; currently working in Visual Studio 2013 on Windows 8.1</p>
  
  <p>The model loader:</p>
  
  <pre><code>void loadModelNfg(const std::string &amp;filename, 
                      GLuint &amp;vbo, GLuint &amp;ibo, GLuint &amp;num, Shaders shaders){
  
      // put here the verteces and indices from the file
      std::vector&lt;Vertex&gt; vertices;
      std::vector&lt;GLushort&gt; indices;
  
      _loadModelNfg(filename, vertices, indices);
      std::cout &lt;&lt; "Mesh Loader: loaded file: " &lt;&lt; filename &lt;&lt; "\n";
  
      // creates OpenGL objects necessary for drawing
      GLuint gl_vertex_buffer_object, gl_index_buffer_object;
  
      // vertex buffer object -&gt; object in which to keep the vertices
      glGenBuffers(1, &amp;gl_vertex_buffer_object);
      glBindBuffer(GL_ARRAY_BUFFER, gl_vertex_buffer_object);
  
      glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), 
                      &amp;vertices[0], GL_STATIC_DRAW);
  
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  
      // index buffer object -&gt; object in which to keep the indices
      glGenBuffers(1, &amp;gl_index_buffer_object);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_index_buffer_object);
  
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), 
                      &amp;indices[0], GL_STATIC_DRAW);
  
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  
      vbo = gl_vertex_buffer_object;
      ibo = gl_index_buffer_object;
      num = indices.size();
  }
  </code></pre>
  
  <p>Calling the previous function:</p>
  
  <pre><code>// for now, global variables:
  GLuint vbo, ibo, num;
  Shader myShaders;
  
  int Init ( ESContext* esContext ) {
      glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
  
      // this one works: tried with a triangle
      int ret = myShaders.Init("../Resources/Shaders/TriangleShaderVS.vs", 
                      "../Resources/Shaders/TriangleShaderFS.fs");
      if (ret == 0)
          loadModelNfg("../../ResourcesPacket/Models/Bila.nfg", vbo, ibo, num, myShaders);
  
      return ret;
  }
  </code></pre>
  
  <p>Drawing the model:</p>
  
  <pre><code>void Draw(ESContext* esContext) {
      Matrix world;
      world.SetIdentity();
      Matrix view = c.getView();
      Matrix persp = c.getPerspective();
      Matrix trans = world * view *persp;
  
      glClear(GL_COLOR_BUFFER_BIT);
      glUseProgram(myShaders.program);
  
      glBindBuffer(GL_ARRAY_BUFFER, vbo);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  
      if (myShaders.positionAttribute != -1) {
              glEnableVertexAttribArray(myShaders.positionAttribute);
              glVertexAttribPointer(myShaders.positionAttribute, 3, GL_FLOAT, 
                      GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, pos)));
      }
      if (myShaders.normalAttribute != -1) {
              glEnableVertexAttribArray(myShaders.normalAttribute);
              glVertexAttribPointer(myShaders.normalAttribute, 3, GL_FLOAT, 
                      GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, norm)));
      }
      if (myShaders.binormalAttribute != -1) {
              glEnableVertexAttribArray(myShaders.binormalAttribute);
              glVertexAttribPointer(myShaders.binormalAttribute, 3, GL_FLOAT, 
                      GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, binorm)));
      }
      if (myShaders.tangentAttribute != -1) {
              glEnableVertexAttribArray(myShaders.tangentAttribute);
              glVertexAttribPointer(myShaders.tangentAttribute, 3, GL_FLOAT, 
                      GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, tgt)));
      }
      if (myShaders.texcoordAttribute != -1) {
              glEnableVertexAttribArray(myShaders.texcoordAttribute);
              glVertexAttribPointer(myShaders.texcoordAttribute, 2, GL_FLOAT, 
                      GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, uv)));
      }
      if (myShaders.colorAttribute != -1) {
              glEnableVertexAttribArray(myShaders.colorAttribute);
              glVertexAttribPointer(myShaders.colorAttribute, 3, GL_FLOAT, 
                      GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, col)));
      }
      if (myShaders.MVPuniform != -1) {
              glUniformMatrix4fv(myShaders.MVPuniform, 1, GL_FALSE, (GLfloat*) trans.m);
      }
  
      // HERE GETS EXCEPTION
      glDrawElements(GL_TRIANGLES, num, GL_UNSIGNED_SHORT, (GLvoid*) 0);
  
      eglSwapBuffers (esContext-&gt;eglDisplay, esContext-&gt;eglSurface);
  }
  </code></pre>
  
  <p>I am not sure that I am correctly binding the buffers in the loadModelNfg() function.</p>
  
  <p>From what can this problem come and how can it be resolved?</p>
  
  <p>EDIT:</p>
  
  <pre><code>GL_VENDOR:   Imagination Technologies (Host GL: 'Intel'); 
  GL_RENDERER: PowerVR PVRVFrame 4.2SGX 530 (Host 'Intel(R) HD Graphics 400'); 
  GL_VERSION:  OpenGL ES 2.0 (SDK build: 2.04.24.0809)
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>I surrounded the function with try-catch statement, but it still breaks when calling it:</p>
  
  <pre><code>try {
      glDrawElements(GL_TRIANGLES, num, GL_UNSIGNED_SHORT, (GLvoid*)0);
  }
  catch (const std::exception&amp; e) {
      std::cout &lt;&lt; e.what() &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>I forgot to mention that the project/solution builds successful (after cleaning, or by rebuild).</p>