多个活动纹理opengl

时间:2016-02-05 06:49:17

标签: opengl-es textures

我有一个背景纹理和透明层纹理。 当我加载这些纹理时,只有_layer3最终纹理处于活动状态。

我希望所有纹理都处于活动状态,以便我知道要继续进行的图层。

   glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);//texture _floorTexture
    glUniform1i(_textureUniform, 0);
           //glUniform1i(glPrograms[currentProgram].glUniforms[_textureUniform],_floorTexture);

    glActiveTexture(GL_TEXTURE0 + 1);
    glBindTexture(GL_TEXTURE_2D, texture[1]); //_layer0
    glUniform1i(_textureUniform, 1);

    glActiveTexture(GL_TEXTURE0 + 2);
    glBindTexture(GL_TEXTURE_2D, texture[2]); //_layer1
    glUniform1i(_textureUniform, 2);

    glActiveTexture(GL_TEXTURE0 + 3);
    glBindTexture(GL_TEXTURE_2D, texture[3]); //_layer2
    glUniform1i(_textureUniform, 3);

    glActiveTexture(GL_TEXTURE0 + 4);
    glBindTexture(GL_TEXTURE_2D, texture[4]); //_layer3
    glUniform1i(_textureUniform, 4); 

2 个答案:

答案 0 :(得分:0)

使用多个纹理时,还必须在着色器中使用多个采样器。这可能是这样的例子:

in vec2 uv;

out vec3 color;

uniform sampler2D floor_texture;
uniform sampler2D layer0_texture;

void main()
{
    vec4 f = texture(floor_texture, uv);
    vec4 l0 = texture(layer0_texture, uv);

    color = mix(f.rgb, l0.rgb, 1 - l0.a);
}

请注意,构图取决于您想要达到的目标,这只是一个例子。

答案 1 :(得分:-2)

这是我的着色器

precision highp float;
varying vec2 interpolatedTextureCoordinate;
uniform sampler2D sourceTexture;
uniform float amount;
uniform sampler2D layerTexture;
const float PI = 3.14159265358979323846264;
const vec4 backgroundColor = vec4(0.0, 0.0, 0.0, 1.0);
const vec2 center = vec2(0.5, 0.5);
void main()
{   
vec4 source = texture2D(sourceTexture, interpolatedTextureCoordinate);
vec4 layer0 = texture2D(layerTexture, interpolatedTextureCoordinate);
float sigma = amount;
float distance = distance(center, interpolatedTextureCoordinate);
loat alpha = 2.0 * (1.0 / sqrt(2.0 * PI)) * exp((-sigma) * pow(distance, 2.0));   
gl_FragColor = mix(source.rgb, layer0.rgb , 1-layer0.a);

}

此致 Sathiya