FreeType位图为空
FreeType bitmap is empty
我使用freetype来显示文本,在本教程的帮助下: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01
但是我看到的都是空的方块,我玩了很多着色器,得出的结论是,正方形的alpha值总是0.0,也由于某种原因,程序在第一次编译编辑的文本着色器后崩溃
代码如下:顶点着色器
#version 420
//vertexData.xy contains pos vertexData.zw contains uv coords
in vec4 vertexData;
out vec2 TexCoord;
void main(){
gl_Position = vec4(vertexData.x, vertexData.y, 0.0, 1.0);
TexCoord = vertexData.zw;
}
片段着色器
#version 420
in vec2 TexCoord;
out vec4 fragData;
uniform sampler2D tex;
uniform vec4 TextColor;
void main(){
//TextColor.rgb is the color texture(tex, TexCoord).r is texture alpha value
fragData = vec4(TextColor.rgb, texture(tex, TexCoord).r * TextColor.a);
}
FreeType的
//Freetype init and face creation is in func Font::init()
void Font::renderText(const char *text, float x, float y, float sx, float sy, float rgba[4]){
//Attribute and Uniform Locations
GLint vDataLoc = ShaderResource::TextProgram->getAttributeLocation("vertexData");
GLint textColLoc = ShaderResource::TextProgram->getUniformLocation("TextColor");
GLint texSamplerLoc = ShaderResource::TextProgram->getUniformLocation("tex");
const char *p;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
FT_Set_Pixel_Sizes(face, 0, 48);
//iterate through text
for(p = text; *p; p++) {
if(FT_Load_Char(face, *p, FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
int width = to_nearest_pow2( g->bitmap.width );
int height = to_nearest_pow2( g->bitmap.rows );
glUniform4fv(textColLoc, 1, rgba);
glEnable(GL_TEXTURE_2D);
Texture Tex;
Tex = Texture();
Tex.bind();
Tex.setParameter(GL_TEXTURE_WRAP_S, GL_CLAMP);
Tex.setParameter(GL_TEXTURE_WRAP_T, GL_CLAMP);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//At first I thought I was passing wrong formats to glteximage2d so I played around with it using gl alpha, gl luminance alpha, gl luminance8 alpha8 etc...
//I believe the issue is that g->bitmap.buffer was not created properly i must not be loading that char correctly??
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
width,
height,
0,
GL_RED,
GL_UNSIGNED_BYTE,
g->bitmap.buffer
);
Tex.active(0);
glUniform1i(texSamplerLoc, 0);
//Then I create textbox and draw it using a vertex array obj and vbos using gl triangle strip etc...
Main.cpp Freetype函数调用
//Disable Normal Shader Enable Text Shader
mainProgram->disable();
textProgram->use();
//Scale x & Scale y / window get screen width and height
float sx = 2.0 / window->getFrameBufferWidth();
float sy = 2.0 / window->getFrameBufferHeight();
//Text color
float color[4] = {0.0,0.0,0.0,1.0};
IHateComicSans.renderText("Hello World!", 0.0, 0.0, sx, sy, color);
textProgram->disable();
mainProgram->use();
在gltexImage2d之后,glGetError返回GL_INVALID_OPERATION,但仅在文本的最后一次迭代期间
问题是在我的纹理类,通过使用opengl函数修复。
相关文章:
- C / C++ 移位/偏移/向左或向右移动位图?
- 如何在快板的屏幕中显示子位图的绘制?
- CreateDIBSection为同一图像返回不一致的位图位值
- C++ 位图中的 ttc 字体
- 使用 GDI+ 旋转位图,然后转换为 HDC
- 难以从 CImageList 设置菜单项位图
- 如何将位图拉伸到父面板(wxWidgets 自定义)
- 如何使用 freetype2 访问单色位图中的像素状态
- 将位图 (bmp) 转换为具有透明度的 png (Windows c++)
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- 位图到垫子/2D 数组
- Gdiplus位图没有Alpha通道
- 如何从路径字符串加载Gdiplus::位图
- 在C++中使用Gdiplus创建透明位图
- 用C++压缩内存中的位图
- C++gdi::内存中的位图到PNG图像
- LoadImage 函数不适用于 ImageMagick 创建的位图图像
- (位图)LoadImage() 返回 NULL,GetLastError() 返回 0
- 渲染freetype位图字形的问题
- FreeType位图为空