title: Md2 model vertices loading issue
body: <p>I'm trying to load an MD2 model but I can't seem to get the vertices to draw correctly. I'm not loading UVs or normals at the moment just want to see the model appear correctly in a single frame then take it from there.</p>
  
  <p>Here's my md2 structures (mostly taken from <a href="http://tfc.duke.free.fr/coding/md2-specs-en.html" rel="nofollow noreferrer">here</a>):</p>
  
  <pre><code>struct v3
  {
      union
      {
          struct
          {
              union { float x; float r; };
              union { float y; float g; };
              union { float z; float b; };
          };
          float At[3];
      };
  };
  
  struct md2_header
  {
      unsigned int Magic;
      unsigned int Version;
  
      unsigned int TextureWidth;
      unsigned int TextureHeight;
  
      unsigned int FrameSize;
  
      unsigned int NumTextures;
      unsigned int NumVertices;
      unsigned int NumUVs;
      unsigned int NumTrigs;
      unsigned int NumGLCommands;
      unsigned int NumFrames;
  
      unsigned int OffsetTextures;
      unsigned int OffsetUVs;
      unsigned int OffsetTrigs;
      unsigned int OffsetFrames;
      unsigned int OffsetGLCommands;
      unsigned int OffsetEnd;
  };
  
  struct md2_vertex
  {
      unsigned char At[3];
      unsigned char NormalIndex;
  };
  
  struct md2_frame
  {
      float       Scale[3];
      float       Translate[3];
      char        Name[16];
      md2_vertex  *Vertices;
  };
  
  struct md2_skin
  {
      char Name[64];
  };
  
  struct md2_uv
  {
      unsigned short u;
      unsigend short v;
  }
  
  struct md2_triangle
  {
      unsigned short Vertices[3];
      unsigned short UVs[3];
  };
  
  struct md2_model
  {
      md2_header Header;
      md2_uv *UVs;
      md2_triangle *Triangles;
      md2_frame *Frames;
      md2_skin *Skins;
      int *GLCommands;
      unsigned int Texture;
      unsigned int VAO, VBO;
  };
  </code></pre>
  
  <p>And here's my simple loading function:</p>
  
  <pre><code>void MD2LoadModel (char *FilePath, md2_model *Model)
  {
      FILE *File = fopen (FilePath, "rb");
      if (!File)
      {
          fprintf (stderr, "Error: couldn't open \"%s\"!\n", FilePath);
          return;
      }
  
  #define FREAD(Dest, Type, Count)\
      fread(Dest, sizeof(Type), Count, File)
  
  #define FSEEK(Offset)\
      fseek(File, Offset, SEEK_SET)
  
  #define ALLOC(Type, Count)\
      (Type *)malloc(sizeof(Type) * Count)
  
      /* Read Header */
      FREAD(&amp;Model-&gt;Header, md2_header, 1);
  
      if ((Model-&gt;Header.Magic != 844121161) ||
          (Model-&gt;Header.Version != 8))
      {
          fprintf (stderr, "Error: bad md2 Version or identifier\n");
          fclose (File);
          return;
      }
  
      /* Memory allocations */
      Model-&gt;Skins = ALLOC(md2_skin, Model-&gt;Header.NumTextures);
      Model-&gt;UVs = ALLOC(md2_uv, Model-&gt;Header.NumUVs);
      Model-&gt;Triangles = ALLOC(md2_triangle, Model-&gt;Header.NumTrigs);
      Model-&gt;Frames = ALLOC(md2_frame, Model-&gt;Header.NumFrames);
      Model-&gt;GLCommands = ALLOC(int, Model-&gt;Header.NumGLCommands);
  
      /* Read model data */
      FSEEK(Model-&gt;Header.OffsetTextures);
      FREAD(Model-&gt;Skins, md2_skin, Model-&gt;Header.NumTextures);
  
      FSEEK(Model-&gt;Header.OffsetUVs);
      FREAD(Model-&gt;UVs, md2_uv, Model-&gt;Header.NumUVs);
  
      FSEEK(Model-&gt;Header.OffsetTrigs);
      FREAD(Model-&gt;Triangles, md2_triangle, Model-&gt;Header.NumTrigs);
  
      FSEEK(Model-&gt;Header.OffsetGLCommands);
      FREAD(Model-&gt;GLCommands, int, Model-&gt;Header.NumGLCommands);
  
      /* Read frames */
      FSEEK(Model-&gt;Header.OffsetFrames);
      for (int i = 0; i &lt; Model-&gt;Header.NumFrames; i++)
      {
          /* Memory allocation for vertices of this frame */
          Model-&gt;Frames[i].Vertices = (md2_vertex *)
              malloc(sizeof(md2_vertex) * Model-&gt;Header.NumVertices);
  
          /* Read frame data */
          FREAD(&amp;Model-&gt;Frames[i].Scale, v3, 1);
          FREAD(&amp;Model-&gt;Frames[i].Translate, v3, 1);
          FREAD(Model-&gt;Frames[i].Name, char, 16);
          FREAD(Model-&gt;Frames[i].Vertices, md2_vertex, Model-&gt;Header.NumVertices);
      }
  
      v3 *Vertices = ALLOC(v3, Model-&gt;Header.NumVertices);
  
      md2_frame *Frame = &amp;Model-&gt;Frames[0];
      For(u32, i, Model-&gt;Header.NumVertices)
      {
          Vertices[i] = V3(
              (Frame-&gt;Vertices[i].At[0] * Frame-&gt;Scale[0]) + Frame-&gt;Translate[0],
              (Frame-&gt;Vertices[i].At[1] * Frame-&gt;Scale[1]) + Frame-&gt;Translate[1],
              (Frame-&gt;Vertices[i].At[2] * Frame-&gt;Scale[2]) + Frame-&gt;Translate[2]);
      }
  
      glGenBuffers(1, &amp;Model-&gt;VBO);
      glBindBuffer(GL_ARRAY_BUFFER, Model-&gt;VBO);
      glBufferData(GL_ARRAY_BUFFER, Model-&gt;Header.NumVertices * sizeof(v3), Vertices, GL_STATIC_DRAW);
  
      glGenVertexArrays(1, &amp;Model-&gt;VAO);
      glBindVertexArray(Model-&gt;VAO);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
      glBindVertexArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  
      fclose (File);
      free(Vertices);
  
  #undef FSEEK
  #undef FREAD
  #undef ALLOC
  }
  </code></pre>
  
  <p>Only passing the vertices data. Which, from my understanding <code>Header-&gt;NumVertices</code> is the number of vertices in each frame. So I'm taking an arbitrary frame (frame 0 in this case) and reading its uncompressed vertices data into <code>Vertices</code>. </p>
  
  <p>Now I read in a book that Quake had their y and z axes flipped, but that still didn't change much.</p>
  
  <p>Here's how I'm drawing the model:</p>
  
  <pre><code>GLuint Shader = Data-&gt;Shaders.Md2Test;
  ShaderUse(Shader);
  ShaderSetM4(Shader, "view", &amp;WorldToView);
  ShaderSetM4(Shader, "projection", &amp;ViewToProjection);
  
  glBindVertexArray(DrFreak.VAO);
  {
      ModelToWorld = m4_Identity;
      ShaderSetM4(Shader, "model", &amp;ModelToWorld);
      glDrawArrays(GL_TRIANGLES, 0, DrFreak.Header.NumVertices);
  }
  glBindVertexArray(0);
  </code></pre>
  
  <p>The matrices are calculated in a CameraUpdate function which I can verify is working correctly because everything else in the scene render properly except the MD2 model. See:</p>
  
  <p><a href="https://i.stack.imgur.com/yTAWu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yTAWu.png" alt="enter image description here"></a></p>
  
  <p>Everything in yellow is supposed to be the MD2 model.</p>
  
  <p>Here are my shaders (pretty much the same shaders for the crates and planes except there's only one 'in' variable, the position and no UVs):</p>
  
  <pre><code>#version 330 core
  layout (location = 0) in vec3 position;
  
  uniform mat4 model;
  uniform mat4 view;
  uniform mat4 projection;
  
  void main()
  {
      gl_Position = projection * view * model * vec4(position, 1.0f);
  }
  
  #version 330 core
  out vec4 color;
  void main()
  {             
      color = vec4(1, 1, 0, 1);
  }
  </code></pre>
  
  <p>I've been stuck here for a couple of days. I stepped into the loading code and I seem to be getting valid values. I'm not sure what's the issue. What am I doing wrong/missing?</p>
  
  <p>Any help is appreciated.</p>