title: glVertexAttribPointer trouble (OpenGL 3.x forward-compatible context)
body: <p>By modern standards, the preferred way of rendering polygon meshes seems to involve the use of <a href="https://web.archive.org/web/20160818093156/https://www.opengl.org/wiki/Vertex_Buffer_Object" rel="nofollow noreferrer">Vertex Buffer Objects</a> in conjunction with index buffers (ultimately drawn by calls to <code>glDrawElements()</code>), which is exactly why I'm trying to wrap my head around these concepts. Also, I insist on using <code>glVertexAttribPointer()</code> instead of the <a href="https://web.archive.org/web/20160701022445/https://www.opengl.org/wiki/Deprecation" rel="nofollow noreferrer">deprecated</a> <code>glVertexPointer()</code>, <code>glNormalPointer()</code> etc.</p>
  <p>I'm using a custom, <em>binary</em> 3d file format (a Wavefront .OBJ derivative), the contents of which can be more or less directly <code>memcpy()</code>'d to a vertex array. Here's how I've declared my <code>vertex</code> structure:</p>
  <pre><code>   typedef struct vertex_ {
  
              float vx,vy,vz;
              float nx,ny,nz;
              float padding[2];  // align to 32 bytes 
     
     } vertex; 
  </code></pre>
  <p>The <code>loadBObj()</code> function returns an <em>index buffer</em> (implemented as a simple array of <code>unsigned short int</code>s), and fills up the vertex array with the associated vertex/normal data (the models used all have been exported to have <em>per-vertex-normals</em> for a smoother shading result).</p>
  <pre><code>indices = loadBObj(&quot;pharaoh.bobj&quot;, false, &amp;VBOid);
  </code></pre>
  <p>The loading code itself is <em>verified to work appropriately.</em></p>
  <p>Now, what's also being done in <code>loadBObj()</code>is a new VBO is being created like so:</p>
  <pre><code>    glGenBuffers(1, VBOid);
      glBindBuffer(GL_ARRAY_BUFFER, *VBOid);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertex)*vcount, &amp;vertarray[0].vx, GL_STATIC_DRAW);
  </code></pre>
  <p>After<code>loadBObj()</code> is called, an <em>index buffer object</em> (probably shouldn't be referred to as such though) is created as follows:</p>
  <pre><code>    glGenBuffers(1, &amp;IBOid);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOid);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short int)*qfcount*4, indices, GL_STATIC_DRAW);
  </code></pre>
  <p>OK. When it comes to actually rendering the mesh, I've used the following code:</p>
  <pre><code>    #define BUFFER_OFFSET(i) ((char *)NULL + (i))
      ...
      glBindBuffer(GL_ARRAY_BUFFER, VBOid);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), BUFFER_OFFSET(0));
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), BUFFER_OFFSET(3*sizeof(float)));
  
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOid);
      glDrawElements(GL_QUADS, qfcount*4, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
  </code></pre>
  <p>which results in an absolutely correct geometry, but the shading is not quite right:</p>
  <ul>
  <li>Here's an image of the model, rendered in <strong>immediate</strong> mode:</li>
  </ul>
  <p><a href="http://i44.tinypic.com/i36zcg.png" rel="nofollow noreferrer">img http://i44.tinypic.com/i36zcg.png</a></p>
  <ul>
  <li>here's one produced by the program described above:</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/IZx5G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IZx5G.png" alt="img2" /></a></p>
  
  <p>Is there something funny going on in my VBO handling?</p>