zoukankan      html  css  js  c++  java
  • QOpenGLTexture 两个纹理叠加

    如何做纹理混合?

    方法是,定义多个QOpenGLTexture,然后在fragment shader中添加相应的变量,然后把texture bind到对应的uniform变量上。

    废话不多说

    texture.frag

    #version 400 core
    in vec3 ourColor;
    in vec2 TexCoord;
    out vec4 color;
    uniform sampler2D ourTexture;
    uniform sampler2D ourTexture1;
    void main()
    {
    color = mix(texture(ourTexture, TexCoord),texture(ourTexture1,TexCoord),0.3);
    }

    最后的0.2是一个混合系数。如果设置为0,则表示完全使用第一个纹理;设置为1,则完全使用第二个纹理。

    程序中:

    初始化texture

        texture = new QOpenGLTexture(QImage("./resources/texture/flower.jpg").mirrored());
        texture->setMinificationFilter(QOpenGLTexture::Nearest);
        texture->setMagnificationFilter(QOpenGLTexture::Linear);
        texture->setWrapMode(QOpenGLTexture::Repeat);
    
        texture1 = new QOpenGLTexture(QImage("./resources/texture/DSCN4391.JPG").mirrored());
        texture1->setMinificationFilter(QOpenGLTexture::Nearest);
        texture1->setMagnificationFilter(QOpenGLTexture::Linear);
        texture1->setWrapMode(QOpenGLTexture::Repeat);

    paintGL中使用:

            m_program->setUniformValue("ourTexture", 0);
            texture->bind();        
            m_program->setUniformValue("ourTexture1", 1);
            texture1->bind(1);

    这里为ourTexture1指定了一个unit,然后使用bind来把texture赋到指定unit的变量上去。

    如果只有一个纹理,那么bind不需要传参数,默认是0;如果有多个纹理,则需要在bind的时候指定要把当前纹理放到哪个(uniform的)变量中,供fragment shader使用。

    结果:

    color = mix(texture(ourTexture, TexCoord),texture(ourTexture1,TexCoord),0.3);


    原图:
    texture1


    texture2



    -- the end --
  • 相关阅读:
    hdu5002 Tree
    hdu6858(杭电多校第八场) Discovery of Cycles
    杭电多校第八场总结
    ubuntu刷新swap
    python 如何关闭warning的输出
    python 如何获取整数,浮点数的最大值
    补码
    LaTeX 公式集锦
    Codeforces 581D Three Logos
    Codeforces 582 A. GCD Table
  • 原文地址:https://www.cnblogs.com/zhsuiy/p/5235291.html
Copyright © 2011-2022 走看看