- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
真遗憾,我从来没有在我的应用程序上获得可靠的字体渲染系统。所以我决定结束这一切。
当我在搜索字体渲染示例时,我碰巧遇到了这个 valve paper得到大家的称赞。
但我缺乏经验和知识来完成这个。
这是我所做的总结:
首先,使用 Freetype2 库,我将一个字符 ('A') 作为位图加载到内存中。
unsigned char u8Character = 'A';
int32_t u32Character = 0;
utf8::utf8to32(&u8Character, &u8Character + 1, &u32Character);
FT_Set_Char_Size(face, 0, 64 * 64, 300, 300);
FT_Load_Char(face, u32Character, FT_LOAD_RENDER);
其次,我将距离场计算为距位图每个像素的切比雪夫距离,以生成 float 图。
注意:老实说,我不知道该怎么做。下面的算法是完全猜测。
template <typename input_type, typename output_type>
output_type chebyshev_distance( input_type from_x
, input_type from_y
, input_type to_x
, input_type to_y )
{
input_type dx = std::abs(to_x - from_x);
input_type dy = std::abs(to_y - from_y);
return static_cast<output_type>(dx > dy ? dx : dy);
}
void GenerateSigendDistanceFieldFrom( const unsigned char* inputBuffer
, int width
, int height
, float* outputBuffer
, bool normalize = false)
{
for (int iy = 0; iy < height; ++iy)
{
for (int ix = 0; ix < width; ++ix)
{
int index = iy*width + ix;
unsigned char value = inputBuffer[index];
int indexMax = width*height;
int indexMin = 0;
int far = width > height ? width : height;
bool found = false;
for (int distance = 1; distance < far; ++distance)
{
int xmin = (ix - distance) >= 0 ? ix - distance : 0;
int ymin = (iy - distance) >= 0 ? iy - distance : 0;
int xmax = (ix + distance) < width ? ix + distance+1 : width;
int ymax = (iy + distance) < height ? iy + distance+1 : height;
int x = xmin;
int y = ymin;
auto fCompareAndFill = [&]() -> bool
{
if (value != inputBuffer[y*width + x])
{
outputBuffer[index] = chebyshev_distance<int, float>(ix, iy, x, y);
if (value < 0xff/2) outputBuffer[index] *= -1;
//outputBuffer[index] = distance;
return true;
}
return false;
};
while (x < xmax)
{
if (fCompareAndFill())
{
found = true;
break;
}
++x;
}
--x;
if (found == true){ break; }
while (y < ymax)
{
if (fCompareAndFill())
{
found = true;
break;
}
++y;
}
--y;
if (found == true){ break; }
while (x >= xmin)
{
if (fCompareAndFill())
{
found = true;
break;
}
--x;
}
++x;
if (found == true){ break; }
while (y >= ymin)
{
if (fCompareAndFill())
{
found = true;
break;
}
--y;
}
if (found == true){ break; }
} // for( int distance = 1; distance < far; ++distance )
} // for( int ix = 0; ix < width; ++ix )
} // for( int iy = 0; iy < height; ++iy )
if( normalize )
{
float min = outputBuffer[0];
float max = outputBuffer[0];
for( int i = 0; i < width*height; ++i )
{
if( outputBuffer[i] < min )
min = outputBuffer[i];
if( outputBuffer[i] > max )
max = outputBuffer[i];
}
float denominator = (max - min);
float newMin = min / denominator;
for( int i = 0; i < width*height; ++i )
{
outputBuffer[i] /= denominator;
outputBuffer[i] -= newMin;
}
}
} // GenerateSigendDistanceFieldFrom
第三,作为测试,我将这个角色纹理渲染到尺寸为 800x600 的整个屏幕上,以查看它们如何扩展。纹理采样“GL_LINEAR”结果很糟糕。
!1!将 Alpha 渲染为红色值。
const GLchar fssource[] =
"#version 440 \n"
"out vec4 v_color;"
"in vec2 v_uv;"
"uniform sampler2D u_texture;"
"void main()"
"{"
" float mask = texture(u_texture, v_uv).a;"
" v_color = vec4(mask,0,0,1);"
"}"
;
!2!渲染文本。 alpha 的阈值是 0.5
const GLchar fssource[] =
"#version 440 \n"
"out vec4 v_color;"
"in vec2 v_uv;"
"uniform sampler2D u_texture;"
"void main()"
"{"
" vec4 result = vec4(1,1,1,1);"
" float mask = texture(u_texture, v_uv).a;"
" if( mask >= 0.5 ) { result.a = 1; }\n"
" else { result.a = 0; }\n"
" v_color = result;"
"}"
!3!渲染文本。 alpha 的阈值是 0.7
const GLchar fssource[] =
"#version 440 \n"
"out vec4 v_color;"
"in vec2 v_uv;"
"uniform sampler2D u_texture;"
"void main()"
"{"
" vec4 result = vec4(1,1,1,1);"
" float mask = texture(u_texture, v_uv).a;"
" if( mask >= 0.7 ) { result.a = 1; }\n"
" else { result.a = 0; }\n"
" v_color = result;"
"}"
字体看起来凹凸不平,显然距离场太亮了。该算法应该使用 0.5 阈值。不仅结果不正确,距离场的生成也太费时了。因此,我无法使用高分辨率图像作为输入。
这里我做的事情显然是错误的,但似乎我要靠自己弄清楚如何生成正确的结果。
但是,如果你知道我做错了什么,请帮助我。
下面是完整的源文件:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <vector>
#include <cstdint>
#include <climits>
#include "ft2build.h"
#include FT_FREETYPE_H
#include "../utf8_v2_3_4/Source/utf8.h"
#include <SDL.h>
#include "../Glew/glew.h"
#include <gl/GL.h>
#undef main
template <typename input_type, typename output_type>
output_type chebyshev_distance( input_type from_x
, input_type from_y
, input_type to_x
, input_type to_y )
{
input_type dx = std::abs(to_x - from_x);
input_type dy = std::abs(to_y - from_y);
return static_cast<output_type>(dx > dy ? dx : dy);
}
void GenerateSigendDistanceFieldFrom( const unsigned char* inputBuffer
, int width
, int height
, float* outputBuffer
, bool normalize = false)
{
for (int iy = 0; iy < height; ++iy)
{
for (int ix = 0; ix < width; ++ix)
{
int index = iy*width + ix;
unsigned char value = inputBuffer[index];
int indexMax = width*height;
int indexMin = 0;
int far = width > height ? width : height;
bool found = false;
for (int distance = 1; distance < far; ++distance)
{
int xmin = (ix - distance) >= 0 ? ix - distance : 0;
int ymin = (iy - distance) >= 0 ? iy - distance : 0;
int xmax = (ix + distance) < width ? ix + distance+1 : width;
int ymax = (iy + distance) < height ? iy + distance+1 : height;
int x = xmin;
int y = ymin;
auto fCompareAndFill = [&]() -> bool
{
if (value != inputBuffer[y*width + x])
{
outputBuffer[index] = chebyshev_distance<int, float>(ix, iy, x, y);
if (value < 0xff/2) outputBuffer[index] *= -1;
//outputBuffer[index] = distance;
return true;
}
return false;
};
while (x < xmax)
{
if (fCompareAndFill())
{
found = true;
break;
}
++x;
}
--x;
if (found == true){ break; }
while (y < ymax)
{
if (fCompareAndFill())
{
found = true;
break;
}
++y;
}
--y;
if (found == true){ break; }
while (x >= xmin)
{
if (fCompareAndFill())
{
found = true;
break;
}
--x;
}
++x;
if (found == true){ break; }
while (y >= ymin)
{
if (fCompareAndFill())
{
found = true;
break;
}
--y;
}
if (found == true){ break; }
} // for( int distance = 1; distance < far; ++distance )
} // for( int ix = 0; ix < width; ++ix )
} // for( int iy = 0; iy < height; ++iy )
if( normalize )
{
float min = outputBuffer[0];
float max = outputBuffer[0];
for( int i = 0; i < width*height; ++i )
{
if( outputBuffer[i] < min )
min = outputBuffer[i];
if( outputBuffer[i] > max )
max = outputBuffer[i];
}
float denominator = (max - min);
float newMin = min / denominator;
for( int i = 0; i < width*height; ++i )
{
outputBuffer[i] /= denominator;
outputBuffer[i] -= newMin;
}
}
} // GenerateSigendDistanceFieldFrom
namespace
{
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
FT_Library freetype;
FT_Face face;
SDL_GLContext glContext;
GLuint glProgram = 0;
GLuint vbo = 0;
GLuint vao = 0;
GLuint glTexture = 0;
}
GLuint MakeShader( GLenum shaderType, const char* source, int slen )
{
auto shader = glCreateShader(shaderType);
glShaderSource(shader, 1, (const GLchar**)&source, &slen);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE)
{
std::vector<GLchar> glInfoLogBuffer;
int len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
glInfoLogBuffer.resize(len + 1);
GLsizei outlen;
glGetShaderInfoLog(shader, glInfoLogBuffer.size(), &outlen, glInfoLogBuffer.data());
glInfoLogBuffer.back() = 0;
std::cout << glInfoLogBuffer.data() << std::endl;
return 0;
}
return shader;
}
GLuint MakeProgram( GLuint vshader, GLuint fshader )
{
auto program = glCreateProgram();
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
GLint success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if( success == GL_FALSE )
{
int len;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
std::vector<GLchar> buffer;
buffer.resize(len+1);
buffer.back() = 0;
glGetProgramInfoLog(program, buffer.size(), &len, buffer.data());
std::cout << buffer.data() << std::endl;
return 0;
}
return program;
}
int Initialize()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "SDL could not initialize!";
return -1;
}
window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (!window)
{
std::cout << "Window could not be created!";
return -1;
}
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, 0, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
SDL_UpdateWindowSurface(window);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
glContext = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
GLenum glError = glewInit();
if( glError != GLEW_OK )
{
std::cout << "Failed to initialize glew" << std::endl;
return -1;
}
// ----------------------
// Vertex Shader
const GLchar vssource[] =
"#version 440 \n"
"layout(location=0) in vec3 a_position;"
"layout(location=1) in vec2 a_uv;"
"out vec2 v_uv;"
"void main()"
"{"
" gl_Position = vec4(a_position,1);"
" v_uv = a_uv;"
"}\n"
;
auto vshader = MakeShader(GL_VERTEX_SHADER, vssource, _countof(vssource));
// --------------------
// Fragment Shader
//const GLchar fssource[] =
// "#version 440 \n"
// "out vec4 v_color;"
// "in vec2 v_uv;"
// "uniform sampler2D u_texture;"
// "void main()"
// "{"
// " float mask = texture(u_texture, v_uv).a;"
// " v_color = vec4(mask,0,0,1);"
// "}"
//;
const GLchar fssource[] =
"#version 440 \n"
"out vec4 v_color;"
"in vec2 v_uv;"
"uniform sampler2D u_texture;"
"void main()"
"{"
" vec4 result = vec4(1,1,1,1);"
" float mask = texture(u_texture, v_uv).a;"
" if( mask >= 0.7 ) { result.a = 1; }\n"
" else { result.a = 0; }\n"
" v_color = result;"
"}"
;
auto fshader = MakeShader(GL_FRAGMENT_SHADER, fssource, _countof(fssource));
// --------------------
// Shader Program
glProgram = MakeProgram( vshader, fshader );
// --------------------
// Vertex Buffer Object
float vb[] =
{
-1, -1, 0,
1, -1, 0,
-1, 1, 0,
1, -1, 0,
1, 1, 0,
-1, 1, 0,
0, 0,
1, 0,
0, 1,
1, 0,
1, 1,
0, 1,
};
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vb), vb, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// --------------------
// Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)(sizeof(float)*18));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//
// Freetype
//
FT_Error error = FT_Init_FreeType(&freetype);
if (error)
{
std::cout << "FreeType: error occured with error code: " << error << std::endl;
}
error = FT_New_Face(freetype, "C:/Windows/Fonts/Arial.ttf", 0, &face);
if (error)
{
std::cout << "FreeType: error occured with error code: " << error << std::endl;
}
error = FT_Set_Char_Size(face, 0, 64 * 64, 300, 300);
if (error)
{
std::cout << "FreeType: error occured with error code: " << error << std::endl;
}
unsigned char u8Character = 'A';
int32_t u32Character = 0;
utf8::utf8to32(&u8Character, &u8Character + 1, &u32Character);
error = FT_Load_Char(face, u32Character, FT_LOAD_RENDER);
if (error)
{
std::cout << "FreeType: error occured with error code: " << error << std::endl;
}
auto bitmap = face->glyph->bitmap;
const int width = bitmap.width;
const int height = bitmap.rows;
const int size = width*height;
std::vector<float> outputBuffer;
outputBuffer.resize(size);
GenerateSigendDistanceFieldFrom(face->glyph->bitmap.buffer, width, height, outputBuffer.data(), true);
std::ofstream ofs("testout.txt");
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
ofs << bitmap.buffer[i*width + j] << ' ';
}
ofs << std::endl;
}
ofs << std::endl;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
ofs << std::setw(6) << std::setprecision(2) << std::fixed << outputBuffer[i*width + j];
}
ofs << std::endl;
}
// ----
// --------------------
// Texture
std::vector<float> invertY;
invertY.reserve(size);
for( int i = height-1; i >= 0; --i )
{
for( int j = 0; j < width; ++j )
{
invertY.push_back(outputBuffer[i*width+j]);
}
}
glGenTextures(1, &glTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, glTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_FLOAT, invertY.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glClearColor(0.5f,0.5f,1.0f,1.0f);
return 0;
}
void Release()
{
SDL_DestroyWindow(window);
SDL_Quit();
}
int main(int argc, char* argv[])
{
Initialize();
bool quit = false;
while( !quit )
{
SDL_Event e;
if( SDL_PollEvent(&e) != 0 )
{
if (e.type == SDL_QUIT)
{
quit = true;
}
} // if( SDL_PollEvent(&e) != 0 )
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(glProgram);
GLint loc = glGetUniformLocation(glProgram, "u_texture");
glUniform1i(loc, 0);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glUseProgram(0);
SDL_GL_SwapWindow(window);
} // while( !quit )
Release();
return 0;
}
最佳答案
如果我理解正确,您可以将位图字符大小设置为 64(在 Freetype 26.6 定点类型中为 64.64),但是您需要将位图拉伸(stretch)到更大的大小,从而进行缩放。
我建议您将字符大小(使用 FT_Set_Char_Size)设置为等于或大于最终大小的维度。然后 SW 的其余部分应该保持位图不变,或者最终缩小它的大小。这并不意味着质量下降,而放大(将大小为 x 的光栅图像变为更大的大小)会将您带到观察到的问题。然后缩小任何插值方案都会给你不错的结果。
关于c++ - 距离场字体渲染的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31028530/
A是不同元素的序列,B是A的子序列,A-B是A中的所有元素,但不是B中的所有元素距离(A) = 总和|a(i)-a(i+1)|从 i=1 到 n-1找到一个子序列 B 使得 Dist(B)+Dist(
我想通过计算每对中所有(多维)点集之间距离的平均值来量化组相似性。 我可以很容易地手动为每对组手动完成此操作,如下所示: library(dplyr) library(tibble) library(
在 OpenXML 中用于指定大小或 X、Y 坐标的度量单位是什么? (介绍)。 将那些与像素匹配是否有意义,如果是这样,那些如何转换为像素? graphicFrame.Transform = new
我想知道是否有人可以帮助我替换过渡层中的值。 如果我尝试: transitionlayer[transitionlayer >= 0.14] = 0.14 : comparison (5) is
我在 firebase 中有一个列表,其中包括地理位置(经度和纬度),并且我想获得距给定坐标最近的 10 个位置。 我正在从 MySQL 过渡,在那里我将计算 SELECT 中的距离, 并在 ORDE
如何在 Python 中根据 2 个 GPS 坐标计算速度、距离和方向(度)?每个点都有纬度、经度和时间。 我在这篇文章中找到了半正矢距离计算: Calculate distance between
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我只想使用 matplotlib 标记两条曲线之间发生最大偏差的位置。请帮助我。 垂直距离适用于 Kolmogorov–Smirnov test import numpy as np %matplot
我有一个包含数万行重复项的文件。我想根据行号找到重复项之间的平均时间/距离。 例如:(其中第一列是行号) 1 string1 2 string2 3 string2 4 string1 5 strin
用公式speed=distance/time计算时间 但时间总是0我的输入是 distance=10 和 speed=5 我的输出必须 = 2 #include int main() { in
我正在使用 Levenshtein 算法来查找两个字符串之间的相似性。这是我正在制作的程序的一个非常重要的部分,因此它需要有效。问题是该算法没有发现以下示例相似: CONAIR AIRCON 算法给出
对于一个房地产网站,我需要实现一个允许搜索文本和距离的搜索机制。 当 lat 和 lon 记录在单独的列中时,在 MySQL 表上进行距离计算很容易,但房子往往有 LOT true/false 属性。
是否可以在触发前更改 UIPanGestureRecognizer 的距离?目前的实现似乎在触发前有 5-10 像素的距离余量,我想降低它如果可能的话。 原因是我将 UIPanGestureRecog
我试图找到两个网格之间的偏差。例如在 3d 空间中定义的两组点之间的差异,我计划使用一些 3d 可视化工具来可视化距离,例如QT3d 或一些基于开放式 gl 的库。 我有两组网格,基本上是两个 .ST
所以,我有这个函数可以快速返回两个字符串之间的 Levenshtein 距离: Function Levenshtein(ByVal string1 As String, ByVal string2
我正在尝试用字典创建一个光学字符识别系统。 事实上,我还没有实现字典=) 我听说有一些基于 Levenstein 距离的简单指标,这些指标考虑了不同符号之间的不同距离。例如。 'N' 和 'H' 彼此
我在PostGIS数据库(-4326)中使用经纬度/经度SRID。我想以一种有效的方式找到最接近给定点的点。我试图做一个 ORDER BY ST_Distance(point, ST_GeomF
我想从线串的一端开始提取沿线串已知距离处的点的坐标。 例如: library(sf) path % group_by(L1) %>% summarise(do_union =
我已经编写了这些用于聚类基于序列的数据的函数: library(TraMineR) library(cluster) clustering <- function(data){ data <- s
是否可以设置 UILabel 的行之间的距离,因为我有一个 UILabel 包含 3 行,并且换行模式是自动换行? 最佳答案 如果您指的是“前导”,它指的是类型行之间的间隙 - 您无法在 UILabe
我是一名优秀的程序员,十分优秀!