- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用 FreeType2 和 this online tutorial 渲染字符,但有一些问题,结果非常不规则。我查看了本网站上的其他问题 here和 here并进行了这些更正,但我仍然很困惑。我已经完成了,但位图显示不正确:
不仅如此,显示效果还取决于我要渲染的角色。有些显示为空白,而另一些显示为矩形。代码如下,任何建议将不胜感激。
主程序:
#include <iostream>
#include <fstream>
#include <vector>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "OGLT.hpp"
OGLT Toolkit;
GLuint vao, vbo[2], shader, uniform_color, tex, uniform_tex;
static void key_callback(GLFWwindow * window, int key, int scancode, int action, int mods){
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void MyInitialize(){
// Initialize FreeType
FT_Library library;
FT_Face face;
int error = FT_Init_FreeType(&library);
if (error){
std::cout << "An error occurred during library initialization!\n";
}
//error = FT_New_Face(library, "open-sans/OpenSans-Regular.ttf", 0, &face);
error = FT_New_Face(library, "/usr/share/fonts/truetype/droid/DroidSans.ttf", 0, &face);
if(error == FT_Err_Unknown_File_Format){
std::cout << "The font file could be opened and read, but it appears " <<
"that its font format is unsupported.\n";
}
else if(error){
std::cout << "Another error code means that the font file could not be " <<
"opened or read, or that it is broken.\n";
}
else std::cout << "Font file sucessfully loaded!\n";
FT_Set_Pixel_Sizes(face, 0, 100);
// Initialize GLEW and load shaders
Toolkit.StartGLEW();
// OpenGL state initialization
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Vertex buffer initialization
FT_Load_Char(face, 'W', FT_LOAD_RENDER);
FT_GlyphSlot g = face->glyph;
float x = 0.0f, y = 0.0f;
float sx = 2.0/1000.0;
float sy = 2.0/800.0;
float x2 = x + g->bitmap_left * sx;
float y2 = -y - g->bitmap_top * sy;
float w = g->bitmap.width * sx;
float h = g->bitmap.rows * sy;
static float letter[] = {
x2, -y2, 0.0f,
x2+w, -y2, 0.0f,
x2, -y2-h, 0.0f,
x2+w, -y2-h, 0.0f,
};
static float letter_uv[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(2, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(letter), letter, GL_STATIC_DRAW);
GLuint VertexAttributeID = 0;
glVertexAttribPointer(VertexAttributeID, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(VertexAttributeID);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(letter_uv), letter, GL_STATIC_DRAW);
GLuint UvAttributeID = 1;
glVertexAttribPointer(UvAttributeID, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(UvAttributeID);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, g->bitmap.width, g->bitmap.rows, 0,
GL_RED, GL_UNSIGNED_BYTE, g->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);
shader = Toolkit.LoadShaders("test_vertex.glsl", "test_fragment.glsl");
uniform_color = glGetUniformLocation(shader, "color");
uniform_tex = glGetUniformLocation(shader, "texture_sampler");
}
void MyDisplay(GLFWwindow * window){
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader);
glm::vec4 ColorVec = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
glUniform4fv(uniform_color, 1, &ColorVec[0]);
glUniform1i(uniform_tex, 0);
glBindVertexArray(vao);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
}
int main(){
// Initialize window
GLFWwindow * window;
// Initialize the library
if(!glfwInit()) return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a windowed mode window and its OpenGL context
window = glfwCreateWindow(1000, 800, "Font Test", NULL, NULL);
if(!window){
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
MyInitialize();
// Loop until the user closes the window
while(!glfwWindowShouldClose(window)){
// Render here
MyDisplay(window);
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
#version 330 core
layout (location = 0) in vec3 vertex;
layout (location = 1) in vec2 vertex_uv;
out vec2 uv;
void main(){
gl_Position = vec4(vertex.xyz, 1);
uv = vertex_uv;
}
#version 330 core
in vec2 uv;
uniform vec4 color;
uniform sampler2D texture_sampler;
out vec4 color_out;
void main(){
color_out = vec4(1.0, 1.0, 1.0, texture(texture_sampler, uv).r) * color;
}
最佳答案
如果您尝试显示不同的字符,您将看到您只显示其中的一部分,这应该会导致您找到错误的原因。它在上传uv缓冲区的代码中,一个简单的copy_paste错误:
glBufferData(GL_ARRAY_BUFFER, sizeof(letter_uv), letter, GL_STATIC_DRAW);
关于ubuntu - OpenGL FreeType2 位图不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38677575/
我有点困惑设置字符大小对象的字符宽度和高度字段(示例来自 FreeType tutorial 中所述) error = FT_Set_Char_Size( face, /*
我正在尝试使用 freetype 枚举字体文件中的字形(名称和 unicode)。 为了获取名称,我使用了 FT_Get_Glyph_Name。但是我怎样才能得到字形 unicode 值呢? 我是字形
它运行在 OS X 10.9 上。我使用 brew 和 FreeType v2.5.2 安装了 GraphicsMagick。但是当我在程序中使用 gm Convert 时,出现此错误: { [Err
项目设置: GameProject [此项目包含游戏代码] GameProject-Android GameProject-桌面 GameProject-robovm [这是一个无需使用任何自由字体即
尝试在 Ubuntu Linux 86-64 上编译 freetype 2.6.1 时,我在配置步骤收到以下消息: /usr/local/include/harfbuzz/hb-common.h:31
当字符串确实包含我使用的默认字体中不存在的字符时,如何告诉 freetype 使用后备字体? 我需要在我的应用程序中正确呈现非拉丁字形。 我必须自己管理回退吗? 如果是这样:如何检测给定字符串中是否缺
我已将“arial.ttf”文件(从我的/Windows/Fonts 文件夹中获取)加载到内存中,但是将其传递到 FT_New_Memory_Face 时会崩溃(在 FT_Open_Face 中的某处
我正在尝试使用 FreeType 为微 Controller 创建位图字体,但我对坐标表达方式的根本差异感到困惑。我的微 Controller 期望字形位图相对于左上角的原点有 X 和 Y 偏移,而
我正在编写一个 DirectX WindowsPhone 应用程序,我需要使用 FreeType。好吧,我已经下载了 FreeType 源代码并使用 CMake 为 WP ARM 生成 Visual
所以我尝试在我的图形引擎中使用带符号的距离字段实现字体渲染。 为此,我使用 FreeType 库生成所有字母的字形。具体来说,我正在尝试像这样加载单色位图: FT_Load_Char(face, c,
我目前正在尝试创建仅包含字形外部边界、内部没有任何填充的字形位图。为此使用 FreeType 库。这是我的代码,类似于 FreeType 引用中的示例 2 代码: Spans outlineSpans
我正在渲染文本,我需要字体的大写高度达到一定的像素数。例如,在下面的标志中,我需要让 SPEED 和 LIMIT 的高度相同,在本例中为 45 px,因此我将字体大小设置为45(我现在明白这不是一个好
我正在尝试使用 FreeType 2.4.11在我使用 openGL 渲染文本的项目中。 虽然我初始化了库,但只将 charmap 设置为 unicode 一次。在每次绘制的字符循环中使用 FT_Lo
我正在尝试创建单色字形图集,但遇到了问题。 Freetype 在字形的位图中呈现“废话”。我责怪 freetype,因为一些字形仍然正确呈现。 生成的纹理图集: 为什么会这样,我该如何解决? 但是我仍
我尝试在 OpenGL 和 FreeType2 中渲染文本,但我不知道如何在渲染字体时获取每行之间的空间(以像素为单位)。当我解析文本时,我希望能够做类似的事情 const char *text =
我正在尝试使用 AIDE 应用程序直接从我的手机使用 freetypefontgenerator。我已完成本文中显示的所有步骤:https://github.com/libgdx/libgdx/wik
这个问题与以下内容密切相关 Is there a way to programmatically determine if a font file has a specific Unicode Gly
我对 Mac OS X 环境比较陌生。 我试图安装 freetype 库以在 OpenGL 中显示文本,但我失败了。 我在一些论坛上读到 freetype 预装了 Mac OS X 山狮。 我只是想确
以前版本的 FreeType 有一个方法 setScale() 现在我可以使用以下方法更改比例: bitmapFont.getData().SetScale(); 但是我遇到了字母间距问题: /ima
使用 FreeType (2.10.1) 在我的游戏引擎中实现文本渲染,我遇到了奇怪的字形,包含重复四次的字母,在 x 轴上镜像并颠倒。 在所需的字母上方似乎有被解释为字形的相邻内存,它在每次运行时都
我是一名优秀的程序员,十分优秀!