title: Problems copying freetype bitmap to opengl texture
body: <p>I'm trying to generate all the bitmaps for characters from <code>' '</code> to <code>'~'</code> and add them to one long texture. I intend to position them in a fixed width texture but for now I just wanted to make sure the concept would work.</p>
  
  <p>But I am having problems. Instead of getting the expected texture I just get the below for a 16px font:
  <img src="https://i.stack.imgur.com/Ig0OE.png" alt="enter image description here"></p>
  
  <p>It looks to me as if I'm copying it wrong but no matter what I seem to do I seem to always get the same output.</p>
  
  <p>Finally, the code:</p>
  
  <p>Font.h</p>
  
  <pre><code>#ifndef _FONT_H_
  #define _FONT_H_
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;GL/gl.h&gt;
  
  #include &lt;ft2build.h&gt;
  #include FT_FREETYPE_H
  
  #include "vector2.h"
  #include "math_helper.h"
  
  class Font {
  public:
    Font(const std::string font_path, int size);
    ~Font();
  
    int get_texture() const { return texture_; }
  
  private:
    unsigned int width_, height_, texture_;
  
    static FT_Library LIBRARY;
    static const char START_CHAR = ' ';
    static const char END_CHAR   = '~';
  
    static void initialize_library();
  };
  
  #endif
  </code></pre>
  
  <p>Font.cpp</p>
  
  <pre><code>#include "font.h"
  
  FT_Library Font::LIBRARY = NULL;        
  
  Font::Font(const std::string font_path, int size)
    : width_(64), height_(2), texture_(0) {
    initialize_library();
  
    FT_Face face = NULL;
    int error = FT_New_Face(Font::LIBRARY, font_path.c_str(), 0, &amp;face);
  
    if(error == FT_Err_Unknown_File_Format) {
      std::cout &lt;&lt; "Error: Unknown file format for font \"" &lt;&lt; font_path &lt;&lt; "\"" &lt;&lt; std::endl;
    } else if(error) {
      std::cout &lt;&lt; "Error: Could not open font \"" &lt;&lt; font_path &lt;&lt; "\"" &lt;&lt; std::endl;
    }
  
    error = FT_Set_Pixel_Sizes(face, 0, size);
    if(error) {
      std::cout &lt;&lt; "Error: Could not set pixel size for font \"" &lt;&lt; font_path &lt;&lt; "\"" &lt;&lt; std::endl;
    }
  
    int num_chars = (END_CHAR - START_CHAR);
    width_ = to_nearest_pow2(num_chars * size);
    height_ = to_nearest_pow2(size);
    std::vector&lt;unsigned char&gt; buffer(width_ * height_, 0);
    Vector2 pen;
  
    for(char c = START_CHAR; c &lt;= END_CHAR; ++c) {
      error = FT_Load_Char(face, c, FT_LOAD_RENDER);
  
      if(error) {
        std::cout &lt;&lt; "Error: Could not load char \"" &lt;&lt; c &lt;&lt; "\"" &lt;&lt; std::endl;
        continue;
      }
  
      FT_Bitmap bmp = face-&gt;glyph-&gt;bitmap;
      int advance = (face-&gt;glyph-&gt;advance.x &gt;&gt; 6);
      int bitmapWidth = bmp.width; //I have tried pitch, same problem
      int bitmapHeight = bmp.rows;
  
      for(int h = 0; h &lt; bitmapHeight; ++h) {
        for(int w = 0; w &lt; bitmapWidth; ++w) {
          int index = h * bitmapWidth + pen.x;
  
          buffer[index + w] = bmp.buffer[w + bitmapWidth * h];
        }
      }
  
      pen.x += advance;
    }
  
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &amp;texture_);
    glBindTexture(GL_TEXTURE_2D, texture_);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width_, height_, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &amp;buffer[0]);
  
    FT_Done_Face(face);
  }
  
  Font::~Font() {
    if(texture_ != 0) {
      glDeleteTextures(1, &amp;texture_);
    }
  }
  
  void Font::initialize_library() {
    if(Font::LIBRARY == NULL) {
      if(FT_Init_FreeType(&amp;Font::LIBRARY) != 0) {
        std::cout &lt;&lt; "Error: Unable to initialize FreeType Library" &lt;&lt; std::endl;
        return;
      }
    }
  }
  </code></pre>
  
  <p>Vector2 is just a simple struct with an x and y field that are initialized to 0 on construction.</p>
  
  <p><code>to_nearest_pow2</code> is defined as:</p>
  
  <pre><code>template&lt;typename T&gt;
  T to_nearest_pow2(const T num) {
    T nearest = 2;
  
    while((nearest &lt;&lt;= 1) &lt; num);
  
    return nearest;
  }
  </code></pre>
  
  <p>Oh and here's how I am drawing it to screen (I am using orthographic projection)</p>
  
  <pre><code>  glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, test-&gt;get_texture());
    glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex2i(0, 0);
    glTexCoord2i(1, 0); glVertex2i(400, 0);
    glTexCoord2i(1, 1); glVertex2i(400, 400);
    glTexCoord2i(0, 1); glVertex2i(0, 400);
    glEnd();
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>I changed <code>index</code> to what was suggested by @shouston and now I get this output</p>
  
  <p><img src="https://i.stack.imgur.com/GEqW5.png" alt="enter image description here"></p>