iOS 纹理单元的使用步骤:


片元着色器

precision mediump float;
                           
uniform sampler2D u_TextureUnit;
varying vec2 v_TextureCoordinates;
  
void main()
{                                  
(u_TextureUnit, v_TextureCoordinates);
}

示例1

//—————————使用纹理———————————
// Set the active texture unit to texture unit 0.
glActiveTexture(GL_TEXTURE0);

// Bind the texture to this unit.
glBindTexture(GL_TEXTURE_2D, textureId);

// Tell the texture uniform sampler to use this texture in the shader by telling it to read from texture unit 0.
glUniform1i(uTextureUnitLocation, 0);//将片元着色器中的uTextureUnitLocation纹理采样器(sample2D)和纹理单元0对应起来。

//glActiveTexture(GL_TEXTURE0)表示把活动的纹理单元设置为纹理单元0,调用glBindTexture将textureId指向的纹理绑定到纹理单元0,最后,调用glUniform1i把选定的纹理单元传递给片段着色器中的u_TextureUnit(sampler2D)。
//—————————使用纹理———————————

示例2

//获取shader里面uniform变量的地址
int tex1_location=glGetUniformLocation(m_uProgram, "tex1");
int tex2_location=glGetUniformLocation(m_uProgram, "tex2");
int tex3_location=glGetUniformLocation(m_uProgram, "tex3");

//对这几个纹理采样器变量进行设置
glUniform1i( (GLint)tex1_location, 0);//对应纹理第一层
glUniform1i( (GLint)tex2_location, 1);//对应纹理第二层
glUniform1i( (GLint)tex3_location, 2);//对应纹理第三层

//后面渲染的时候,设置三成纹理
(GL_TEXTURE0 + 0);
(GL_TEXTURE_2D, texture1Id);
 
(GL_TEXTURE0 + 1);
(GL_TEXTURE_2D, texture2Id);
 
(GL_TEXTURE0 + 2);
(GL_TEXTURE_2D, texture3Id);

 

所以glActiveTexture第几层,并不表示是shader里面第几个采样器,

 //中间有glUniform1i进行纹理层和采样器地址进行绑定