将freetype位图复制到opengl纹理的问题

时间:2013-03-04 19:28:49

标签: c++ opengl freetype

我正在尝试为' ''~'中的字符生成所有位图,并将它们添加到一个长纹理中。我打算将它们定位在固定宽度的纹理中,但是现在我只是想确保这个概念能够起作用。

但我遇到了问题。而不是获得预期的纹理我只是获得16px字体的下面: enter image description here

在我看来,好像我正在复制它,但无论我做什么,我似乎总是得到相同的输出。

最后,代码:

Font.h

#ifndef _FONT_H_
#define _FONT_H_

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <GL/gl.h>

#include <ft2build.h>
#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

Font.cpp

#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, &face);

  if(error == FT_Err_Unknown_File_Format) {
    std::cout << "Error: Unknown file format for font \"" << font_path << "\"" << std::endl;
  } else if(error) {
    std::cout << "Error: Could not open font \"" << font_path << "\"" << std::endl;
  }

  error = FT_Set_Pixel_Sizes(face, 0, size);
  if(error) {
    std::cout << "Error: Could not set pixel size for font \"" << font_path << "\"" << std::endl;
  }

  int num_chars = (END_CHAR - START_CHAR);
  width_ = to_nearest_pow2(num_chars * size);
  height_ = to_nearest_pow2(size);
  std::vector<unsigned char> buffer(width_ * height_, 0);
  Vector2 pen;

  for(char c = START_CHAR; c <= END_CHAR; ++c) {
    error = FT_Load_Char(face, c, FT_LOAD_RENDER);

    if(error) {
      std::cout << "Error: Could not load char \"" << c << "\"" << std::endl;
      continue;
    }

    FT_Bitmap bmp = face->glyph->bitmap;
    int advance = (face->glyph->advance.x >> 6);
    int bitmapWidth = bmp.width; //I have tried pitch, same problem
    int bitmapHeight = bmp.rows;

    for(int h = 0; h < bitmapHeight; ++h) {
      for(int w = 0; w < 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, &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, &buffer[0]);

  FT_Done_Face(face);
}

Font::~Font() {
  if(texture_ != 0) {
    glDeleteTextures(1, &texture_);
  }
}

void Font::initialize_library() {
  if(Font::LIBRARY == NULL) {
    if(FT_Init_FreeType(&Font::LIBRARY) != 0) {
      std::cout << "Error: Unable to initialize FreeType Library" << std::endl;
      return;
    }
  }
}

Vector2只是一个带有x和y字段的简单结构,在构造时初始化为0。

to_nearest_pow2定义为:

template<typename T>
T to_nearest_pow2(const T num) {
  T nearest = 2;

  while((nearest <<= 1) < num);

  return nearest;
}

哦,这就是我如何将它绘制到屏幕上(我正在使用正交投影)

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, test->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();

修改

我将index更改为@shouston建议的内容,现在我得到了此输出

enter image description here

2 个答案:

答案 0 :(得分:4)

将输出图像重新缩放为1000x60,您会感到惊讶。这就是paint.net由您附加的PNG构成的。

enter image description here

首先是明显的事情。角色基本上都存在,但出于某种原因,你只是把它们渲染得非常小(你传递的size的值是多少?)

此外,您并没有尝试处理任何freetype字符指标,所以不要指望它们都能很好地排列。尚未...

答案 1 :(得分:2)

问题在于纹理缓冲区的索引:

int index = h * bitmapWidth + pen.x;

应该是

int index = h * width_ + pen.x;

即将h乘以纹理的整个宽度,而不是字形的宽度。