Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于OpenGL的透明纹理_Opengl - Fatal编程技术网

基于OpenGL的透明纹理

基于OpenGL的透明纹理,opengl,Opengl,我想使用纹理在opengl窗口上创建字体 我这样说: 一, 二, 绘制背景色的四边形 三, 四, 绘制纹理 五, 六, 画正面颜色 但现在我发现,当我使用color_mix绘制前颜色时,它也会在纹理透明的地方混合背景色 如何使字体可以使用前颜色和背景颜色?此答案假设您使用的是传统OpenGL: 可以将纹理设置为直接以前景色绘制,如下所示: // Preparation glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_A

我想使用纹理在opengl窗口上创建字体

我这样说:

一,

二,

绘制背景色的四边形

三,

四,

绘制纹理

五,

六,

画正面颜色

但现在我发现,当我使用color_mix绘制前颜色时,它也会在纹理透明的地方混合背景色


如何使字体可以使用前颜色和背景颜色?

此答案假设您使用的是传统OpenGL:

可以将纹理设置为直接以前景色绘制,如下所示:

// Preparation
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

{
    glDisable(GL_TEXTURE_2D);
    glColor3d(bgR,bgG,bgB); // Set background color

    /* Draw background quad here */

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, yourFontTexture);
    glColor3d(fgR,fgG,fgB); // Set foreground color

    /* Draw texture quads here*/
}
这要求字体纹理在透明背景上为白色。绘制时,纹理颜色将与活动颜色相乘


请注意,使用此方法,您可以在任何喜欢的内容上绘制彩色文本,如果背景已经是所需的颜色,则无需显式绘制背景矩形。

首先使用背景颜色绘制四边形的意图是什么?我认为,如果您坚持使用过时的固定功能管道,您真正想要的是使用
GL_贴花
纹理环境模式。或者,如果使用现代OpenGL(推荐),请编写适当的着色器,将背景颜色考虑在内

建议的片段着色器:

uniform sampler2D u_font_tex;
uniform vec4 u_bkgd_color;
in vec2 v_tex_coord;
out vec4 output_color;

main()
{
     vec4 t = texture(u_font_tex, v_tex_coord);
     output_color = mix(u_bkgd_color, t, t.a);
}

我试过了,但是frontcolor没有被绘制,frontcolor仍然是纹理中的颜色。如何混合frontcolor和纹理?
gldisable(gl_texture_2d);
glblendfunc(gl_dst_alpha,gl_dst_alpha);
// Preparation
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

{
    glDisable(GL_TEXTURE_2D);
    glColor3d(bgR,bgG,bgB); // Set background color

    /* Draw background quad here */

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, yourFontTexture);
    glColor3d(fgR,fgG,fgB); // Set foreground color

    /* Draw texture quads here*/
}
uniform sampler2D u_font_tex;
uniform vec4 u_bkgd_color;
in vec2 v_tex_coord;
out vec4 output_color;

main()
{
     vec4 t = texture(u_font_tex, v_tex_coord);
     output_color = mix(u_bkgd_color, t, t.a);
}