c++ - OpenGL、FreeType2 : italic characters overlay each other

标签 c++ opengl glsl freetype freetype2

我正在尝试使用 C++ 在我的 OpenGL (v4.1) 场景中使用 FreeType (v2) 库进行二维绘图。文本已呈现,但不正确:一个字形隐藏另一个字形。如何防止这种行为?我的绘图代码如下:

void CFreeTypeFont::Print(string sText, int x, int y, int iPXSize) {
    glBindVertexArray(uiVAO);
    glUniform1i(shp->uniform("gSampler"), 0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    int iCurX = x, iCurY = y;
    if(iPXSize == -1)iPXSize = iLoadedPixelSize;
    float fScale = float(iPXSize)/float(iLoadedPixelSize);
    FOR(i, ESZ(sText)) {
        if(sText[i] == '\n') {
            iCurX = x;
            iCurY -= iNewLine*iPXSize/iLoadedPixelSize;
            continue;
        }
        int iIndex = int(sText[i]);
        iCurX += iBearingX[iIndex]*iPXSize/iLoadedPixelSize;
        if(sText[i] != ' ') {
            tCharTextures[iIndex].BindTexture(0);
            glm::mat4 mModelView = glm::translate(glm::mat4(1.0f), glm::vec3(float(iCurX), float(iCurY), 0.0f));
            mModelView = glm::scale(mModelView, glm::vec3(fScale));

            glUniformMatrix4fv(shp->uniform("matrices.modelViewMatrix"),
                               1, GL_FALSE, glm::value_ptr(mModelView));

            // Draw character
            glDrawArrays(GL_TRIANGLE_STRIP, iIndex*4, 4);
        }

        iCurX += (iAdvX[iIndex]-iBearingX[iIndex])*iPXSize/iLoadedPixelSize;
    }
    glDisable(GL_BLEND);
}

输出是这样的:

Must be "Hellow, World!"

如果我禁用混合模式,那么图片会清楚地显示字符叠加:

The same text without opacity

我的顶点着色器:

#version 410

uniform struct Matrices {
    mat4 modelViewMatrix;
} matrices;

uniform float screenWidth;
uniform float screenHeight;

layout (location = 0) in vec2 inPosition;
layout (location = 1) in vec2 inCoord;

out vec2 texCoord;

void main() {
    texCoord = inCoord;
    vec4 pre = matrices.modelViewMatrix*vec4(inPosition, 0.0, 1.0);
    pre.x = pre.x / screenWidth - 1;
    pre.y = pre.y / screenHeight - 1;
    gl_Position = pre;
}

片段着色器:

#version 410

in vec2 texCoord;
out vec4 outputColor;

uniform sampler2D gSampler;
uniform vec4 vColor;

void main() {
    vec4 vTexColor = texture(gSampler, texCoord);
    outputColor = vTexColor*vColor;
}

最佳答案

结果看起来好像您在渲染字体时没有禁用深度测试。深度测试防止其他字形被绘制在与先前字形相同的位置。

禁用深度测试调用

glDisable(GL_DEPTH_TEST);

关于c++ - OpenGL、FreeType2 : italic characters overlay each other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48664176/

相关文章:

api - opengl当前顶点是什么?

c++ - glDrawArrays 调用 mangling 数据之间的 glBufferSubData

c++ - 如何在 visual studio 2015 中显示 "unimplemented pure virtual"?

c++ - 如何循环继承自 boost::any 中包含的 std::vector 的类

c++ - 我们如何使用强制转换来允许将字符屏蔽为字符串?

glsl 函数指针(或等效的)

opengl - 为什么有一个单独的投影矩阵是有益的,但结合模型和 View 矩阵?

c++ - 为什么 unsigned char 具有与其他数据类型不同的默认初始化行为?

c++ - 如何缩放到 SDL 中的分辨率?

glsl - 检查属性是否提供给着色器