简体   繁体   English

C ++-FreeType位图为空

[英]c++ - FreeType bitmap is empty

I am using freetype to display text, with the the help of this tutorial: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01 在本教程的帮助下,我正在使用freetype显示文本: http : //en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01

But all i see is empty squares, I played around a lot with the shaders and came to a conclusion that that the alpha value of the squares was always 0.0 also for some reason the program crashes after first time of compiling edited text shaders 但是我所看到的只是空的正方形,我在着色器上玩了很多次,得出的结论是,正方形的alpha值始终为0.0,由于某种原因,该程序在第一次编译可编辑的文本着色器后也会崩溃

This is the code: Vertex Shader 这是代码: 顶点着色器

#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;
}

Fragment Shader 片段着色器

#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 Func call Main.cpp Freetype Func调用

    //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();

After gltexImage2d glGetError returns GL_INVALID_OPERATION but only during the last iteration of the text 在gltexImage2d之后,glGetError返回GL_INVALID_OPERATION,但仅在文本的最后一次迭代期间返回

问题出在我的Texture类中,仅通过使用opengl函数即可解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM