我正在使用 C ++ 在我的 OpenGL (v4.1)场景中使用 FreeType (v2)库进行2D绘图。文本被渲染,但不正确:一个字形隐藏另一个字形。如何防止这种行为?我的绘图代码如下:
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);
}
输出如下:
如果我禁用blend mode
,那么图片会清楚地显示字符叠加:
我的顶点着色器:
#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;
}
答案 0 :(得分:1)
结果看起来好像在渲染字体时没有禁用深度测试。深度测试可防止其他字形与之前的字形在同一位置绘制。
禁用深度测试调用
glDisable(GL_DEPTH_TEST);