当前位置: 首页 > news >正文

openGL文本渲染FreeType常见问题

这里写自定义目录标题

  • 源码下载及编译
  • 编译生成的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
在这里插入图片描述

其他问题后续更新


http://www.mrgr.cn/news/7827.html

相关文章:

  • 【MySQL】基础入门(第一篇)
  • Maven命令传pom或者jar异常
  • UniApp中的Flex布局技巧
  • redis常用操作命令
  • 【CPP】单生产者单消费者无锁队列使用记录
  • 如何在Docker中部署Eureka Server:容器化微服务注册中心
  • Ciallo~(∠・ω・ )⌒☆第二十一篇 入门re 正则表达式
  • 【计算机三级-数据库技术】操作题大题(第七套)
  • 机械学习—零基础学习日志(如何理解概率论7)
  • 剪辑视频工具分享,这4款值得收藏
  • C语言-输出菱形
  • C语言刷题日记(附详解)(2)
  • 鲲鹏服务器安装Kafka
  • SQL server 2008 获取当前年,季度 和月的最后一天
  • 【C++ 面试 - 面向对象】每日 3 题(十)
  • 常用的分类算法及其优缺点
  • DevEco Studio 预览器报错踩坑
  • AI大模型日报#0823:GPT-4无师自通预测蛋白质结构登Nature子刊、豆包版《Her》升级上新
  • 设计模式-命令模式
  • 【大数据算法】一文掌握大数据算法之:时间亚线性算法。