3

currently I am having problems with passing multiple Textures to a glsl shader on iOS.

I have read through several similar questions and also tried whats written in eg. How can I pass multiple textures to a single shader, but that did not work either.

Here is my code:

    [self setupVBOs];
    [self compileSimpleShaders];
    [self render];

in compileSimpleShaders the Shader gets compiled and uniforms get set. For the Textures it does:

    _textureUniform = glGetAttribLocation(programHandle, "Texture");
    _textureUniform2 = glGetAttribLocation(programHandle, "Texture2");

in render the wanted Textures get bound to the Uniform as

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(CVOpenGLESTextureGetTarget(_display.chromaTexture),
    CVOpenGLESTextureGetName(_display.chromaTexture));
    glUniform1i(_textureUniform, 0);


    glActiveTexture(GL_TEXTURE1);
    glBindTexture(CVOpenGLESTextureGetTarget(_display.lumaTexture),
              CVOpenGLESTextureGetName(_display.lumaTexture));
    glUniform1i(_textureUniform2, 0);


    glDisable(GL_BLEND);

I have been using GL_TEXTURE0 and GL_TEXTURE1 here, because when the Textures, which are actually the luma and Chroma Textures from the iPhone Camera,that are ultimately used to calculate the corresponding rgb value, are getting created, these Textureslots are used.

The fragmentshader I am using is very simple, it just textures a simple Screen Filling Quad with a given texture:

    varying lowp vec4 DestinationColor;
    varying lowp vec2 TexCoordsOut;

    uniform sampler2D Texture;
    uniform sampler2D Texture2;

    void main(void) {

        gl_FragColor = texture2D(Texture2,TexCoordsOut);
    }

I have been using this to check if both textures are getting correctly uploaded by swapping Texture/Texture2 in the gl_FragColor.

The Textures themselves work just perfectly fine. The problem here is, that only one Texture gets used despite with what Texture the Quad gets textured.

Since thats the case, the Problem might be that the first loaded texture gets overwritten with the second texture.

I Hope someone can help me here and sees what I did wrong, because i simply don't see it.

Community
  • 1
  • 1
seko
  • 47
  • 5

2 Answers2

3

You use the same uniform location (0) for both textures. They have to be different :

glUniform1i(_textureUniform, 0);
glUniform1i(_textureUniform2, 1);

Also you can change the color space of AVCaptureVideoDataOutput videoSettings to kCVPixelFormatType_32BGRA if you want to create one texture instead of luma, chroma.

stefos
  • 1,235
  • 1
  • 10
  • 18
  • Thanks, that helped. At first I thought this wouldnt work. Then I saw that I had something missing in the shader, what caused the program to fail. – seko Jun 09 '16 at 08:29
0

The value you set inside your uniform has to be the number / ID of the texture unit you bound the textures to.

So in you're case you've been using TEXTURE0 and TEXTURE1, so you need to set the uniforms to 0 & 1.

246tNt
  • 2,122
  • 1
  • 16
  • 20