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

转载于:https://www.cnblogs.com/zhsuiy/p/5235291.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值