柔软、透明的笔触纹理没有像我预期的那样混合

Posted

技术标签:

【中文标题】柔软、透明的笔触纹理没有像我预期的那样混合【英文标题】:Soft, transparent paint stroke textures not blending like I expect 【发布时间】:2016-02-17 17:34:22 【问题描述】:

我正在尝试在我的 GL 4.5 应用程序中实现软画笔。我已经实现了 100% 的硬刷,但软刷功能低于标准。我试图实现的这种绘画模式在 Krita 中称为“水洗”绘画,是 GIMP 中的正常模式。

例如:在 Photoshop 中,我有一个低硬度的圆形笔刷,笔刷不透明度为 10%,按住鼠标并拖动光标,我得到一个均匀的笔触,纹理最多10% 的不透明度,除了淡出的边缘。如果我再次单击,由于添加剂混合(?),它会增加 10% 的白色。

在我的带有软笔刷纹理的应用程序中,由于纹理的不透明度逐渐消失,我总是在鼠标移动时出现条带。在混合当前笔画和 BG 纹理后,我尝试了各种混合模式并限制不透明度,它们或多或少看起来如下:

pPaintStroke_washF 全屏四边形着色器

#version 450 core
#extension GL_ARB_bindless_texture : require

in Vert

    vec2 uv;
 v;

layout(bindless_sampler, location = 0) uniform sampler2D blended_64; // blended bg + stroke
layout(location = 0) out vec4 Ci;

uniform vec4 brushRGBA = vec4(1.f, 1.f, 1.f, .001f);

void main()

    vec4 blended = texture(blended_64, v.uv);

    if (blended.a == 0.f)
        Ci = vec4(0.f);

    else
    
        Ci.rgb = brushRGBA.rgb;
        Ci.a = clamp(blended.a, 0.f, brushRGBA.a);
        // Ci.a = clamp(blended.a, blended.a, brushRGBA.a);
    

应用程序

glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

for (int i = 0; i < Bresenham.size(); ++i)

    /*
        //bind brushTempN.fbo1
        //render Bresenham[i] into it
    */

    glBindFramebuffer(GL_FRAMEBUFFER, brushTempN.fbo1);
    glViewport(0, 0, brushTempN.width, brushTempN.height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    myWin.myGLWidgetSh->glUseProgram2("pPaintStroke_unwashed");

    myWin.paintStroke->s->val_3 = glm::vec3((float)Bresenham[i].size / 100);
    myWin.paintStroke->t->val_3 = glm::vec3(Bresenham[i].coord, 0.f);
    myWin.paintStroke->mvpGet(myWin.allGL[GLidx]);
    myWin.paintStroke->render(myWin.allGL[GLidx]);

    /*
        //bind brushTempN.fbo2
        //render bg - starts as 0 (brushBGN.tex1)
        //render curr stroke (brushTempN.tex1)
        //this blends them
    */

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glBindFramebuffer(GL_FRAMEBUFFER, brushTempN.fbo2);
    glViewport(0, 0, brushTempN.width, brushTempN.height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    myWin.myGLWidgetSh->glUseProgram2("pPaintStroke_washBlend");

    washBlendPhase = 0; myWin.myFSQ->render(myWin.allGL[GLidx]); //render BG

    washBlendPhase = 1; myWin.myFSQ->render(myWin.allGL[GLidx]); //render the curr stroke (brushTempN.tex1)

    /*
        //bind brushN.fbo1
        //read brushTempN.tex2_64
        //clamp the blended alpha with Ci.a = clamp(in.a, 0.f, brushRGBA.a)
    */

    glBindFramebuffer(GL_FRAMEBUFFER, brushN.fbo1);
    glViewport(0, 0, brushN.width, brushN.height);

    myWin.myGLWidgetSh->glUseProgram2("pPaintStroke_wash");
    myWin.myFSQ->render(myWin.allGL[GLidx]);

    /*
        //bind brushBGN.fbo1
        //copy the above brushN.tex1 into it
    */

    glBindFramebuffer(GL_FRAMEBUFFER, brushBGN.fbo1);
    glViewport(0, 0, brushBGN.width, brushBGN.height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    myWin.myGLWidgetSh->glUseProgram2("pPaintStroke_washBlend");

    washBlendPhase = 2; myWin.myFSQ->render(myWin.allGL[GLidx]); //render new BG

【问题讨论】:

【参考方案1】:

您也应该使用添加剂混合。

glBlendFunc(GL_SRC_ALPHA, GL_ONE);

【讨论】:

以上是关于柔软、透明的笔触纹理没有像我预期的那样混合的主要内容,如果未能解决你的问题,请参考以下文章

Three.js中同一网格面上的多个透明纹理

OpenGL GLSL 通过任意形状混合两个纹理

OpenGL渲染到纹理透明度问题

unity如何从纹理图片中获取透明度参数

在 OpenGL ES 中混合不能像我认为的那样工作

OpenGL纹理覆盖材质颜色