这里写自定义目录标题
- 源码下载及编译
- 编译生成的dll及lib
- 使用FreeType
- include头文件加载
- 附加包含目录
- lib文件加载
- 添加lib文件位置
- 添加lib文件下的lib名
- 字体使用
- 代码编写
- 代码初始化
- 中文字体输出简单封装
- 存在问题列表
- 问题1:无法打开stddef.h
- 其他问题后续更新
源码下载及编译
点击此链接进入下载地址
https://download.savannah.gnu.org/releases/freetype/
下载完毕后在windows下找到freetype.sln进行编译(如下图位置)
编译生成的dll及lib
(本项目使用vs2017进行编译),编译文件生成地址在objs里
使用FreeType
include头文件加载
附加包含目录
注意把freetype的头文件复制到根目录,而不是 放到 "include/freeType"文件夹内部(编译会报错)
lib文件加载
添加lib文件位置
添加lib文件下的lib名
字体使用
选择 C:\Windows\Fonts下的字体文件即可(或网上下载)
代码编写
代码初始化
#include <ft2build.h>
#include FT_FREETYPE_H
FT_Library ft,ft1;
if (FT_Init_FreeType(&ft)) std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
if (FT_Init_FreeType(&ft1)) std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
if (FT_New_Face(ft, "Fonts/simsun.ttc", 0, &face)) std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
if (FT_New_Face(ft1, "Fonts/simsun.ttc", 0, &face1)) std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
中文字体输出简单封装
完整代码 learnOpenGL-CN
void RenderText_CN(Shader &shader, std::wstring text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color, GLuint VAO, GLuint VBO, FT_Face face, FT_Library ft)
{
shader.use();
glUniform3f(glGetUniformLocation(shader.ID, "textColor"), color.x, color.y, color.z);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
for (auto c : text)
{
//if (Characters.find(c) == Characters.end()||1)
//{
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cerr << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
continue;
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
//Characters[c] = character;
//}
Character ch = character;// Characters[c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale;
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 0.0 }
};
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (ch.Advance >> 6) * scale;
glDeleteTextures(1, &texture);//openGL gen的必须delete不然会导致软件内存越来越大
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
存在问题列表
问题1:无法打开stddef.h
vs2017(win10版本):
“项目”-“配置属性”-“常规”-"windows SDK版本"选择10.0.17763.0