前言
在openGL绘制字体,我们一般都使用freeType字体库,如下图所示
下载
freeType官网
编译源码
使用CMake编译源码
如果嫌麻烦,我这里有编译好的库,包括头文件、lib静态库、dll动态库
编译好的lib和dll库下载地址
例子
// Hacky test//#include "glfreetype/TextRenderer.hpp"
#include "TextRenderer.hpp"
#include "GLFW/glfw3.h"int main(int argc, char **argv)
{GLFWwindow* window;/* Initialize the library */if (!glfwInit()) {return -1;}glfwWindowHint(GLFW_SAMPLES, 4);int windowWidth = 300;int windowHeight = 40;window = glfwCreateWindow(windowWidth, windowHeight, "glfreetype test", NULL, NULL);if (!window) {glfwTerminate();return -1;}/* Make the window's context current */glfwMakeContextCurrent(window);// NEHE's font systemglfreetype::font_data our_font;//font_dataour_font.init("C:/Windows/Fonts/Arial.ttf", 25 /* size */);/* Loop until the user closes the window */while (!glfwWindowShouldClose(window)) {/* Render here */glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glClearColor(255.0, 255.0, 255.0, 0.0);glPushMatrix();glLoadIdentity();// Blue textglColor3ub(0,0,0xff);glfreetype::print(our_font, 20 /* xpos */, 20 /* ypos */, "The quick brown fox blah blah blah");glPopMatrix();/* Swap front and back buffers */glfwSwapBuffers(window);/* Poll for and process events */glfwPollEvents();}our_font.clean();glfwTerminate();return 0;
}
运行结果
工程源码下载
工程源码下载