您的位置:首页 > 运维架构

QOpenGLTexture 两个纹理叠加

2016-03-02 15:57 465 查看
如何做纹理混合?

方法是,定义多个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 --
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: