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 --


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM