可以将多个纹理传给shader,下图是两个纹理叠加的效果

基于Qt的OpenGL可编程管线学习(6)- 多重纹理_第1张图片

shader中定义如下:

uniform sampler2D U_MainTexture;
uniform sampler2D U_SubTexture;
varying vec2 M_coord;

void main()
{
    gl_FragColor = texture2D(U_SubTexture, M_coord) * 0.8 + 
                   texture2D(U_MainTexture, M_coord) * 0.2;
}

CUP中参数传递

// 传递0号纹理单元
OpenGLCore->glActiveTexture(GL_TEXTURE0);
OpenGLCore->glBindTexture(GL_TEXTURE_2D, m_Texture);
OpenGLCore->glUniform1i(m_TextureLocation, 0);

// 传递1号纹理单元
OpenGLCore->glActiveTexture(GL_TEXTURE1);
OpenGLCore->glBindTexture(GL_TEXTURE_2D, m_Texture2);
OpenGLCore->glUniform1i(m_Texture2Location, 1);