首页 > 解决方案 > 如何羽化文字显示效果

问题描述

我正在使用 freetype 渲染文本。

渲染以绘制矩形并将文本图像映射到其上的典型样式完成。

在此处输入图像描述

我想根据变量“effectAlphaValue”显示上述文本

如果“effectAlphaValue”的值为 100,那么整个文本应该是可见的。

当“effectAlphaValue”的值为 55 时,文本应该是这样的。

在此处输入图像描述

我已经能够达到这个效果。

问题是我无法添加羽化这种效果,如下图所示。

这里的“effectAlphaValue”是 55,但它是羽化的。 在此处输入图像描述

这是我根据 effectAlphaValue 单独显示文本矩形的代码。

如果有人可以指导我或向我展示如何实现羽毛效果的伪代码,我会非常高兴。

int currentCharNumber = 1;
int eachCharValue = 100 / totalNumberOfChars;  // what should be the value of each char for alpha opacity(spaces not included)
for (c = str.begin(); c != str.end(); c++)
        {
            Character ch = Characters[*c]; // get the char from the character map
            // Calculate the xPos and the yPos for the rectangle vbo here
            //////////////////////////////////////////////////////
            // Update VBO for each character
            GLfloat vertices[6][5] = {

                { xpos ,     ypos - h , 0.0 ,     0.0, 1.0 },
                { xpos + w , ypos ,     0.0 ,     1.0, 0.0 },
                { xpos ,     ypos,     0.0 ,    0.0, 0.0 },

                { xpos ,     ypos - h , 0.0,      0.0, 1.0 },
                { xpos + w , ypos - h , 0.0,      1.0, 1.0 },
                { xpos + w , ypos ,     0.0,       1.0, 0.0 }
            };

            // Render glyph texture over quad
            glBindTexture(GL_TEXTURE_2D, ch.TextureID);
            // Update content of VBO memory
            glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
            glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            //============UPDate the alpha value for current char=======================

            if (*c != ' ') // only calculate alpha for non spaces
            {
                int startValue = (currentCharNumber - 1) * eachCharValue; // 
                int currentValue = effectAlphaValue - startValue;   
                float finalOpacity;
                float percentValueCurrent = ((float)currentValue  / (float)eachCharValue) * 100;                
                finalOpacity = ((float)percentValueCurrent / 100.0f);   
                finalOpacity = ceil(finalOpacity);
                lastAlphaValue = finalOpacity;              
                if (finalOpacity > 100.0)
                    finalOpacity = 100.0;

                ShaderManager::GetShader("TextShader").SetFloat("opacity", finalOpacity / 100.0f);  // set the current opacity
                currentCharNumber++;
            }
            // Render quad
            glDrawArrays(GL_TRIANGLES, 0, 6);
            // Now advance cursors for next glyph
            x += (ch.Advance >> 6); // Bitshift by 6 to get value in pixels (1/64th times 2^6 = 64)
            x += this->Kerning;             
        }   
    }
}

标签: openglglsl

解决方案


推荐阅读