How do I use multiple textures in an OpenGL SL shader in SceneKit in iOS?

433 views Asked by At

I've got a frag file with these two variables:

uniform highp sampler2D textureImage1;
uniform highp sampler2D textureImage2;

And in my code I'm binding images to these with:

[myNode.geometry.firstMaterial handleBindingOfSymbol:@"textureImage1" usingBlock:^(unsigned int programID, unsigned int location, SCNNode *renderedNode, SCNRenderer *renderer) {
    glBindTexture(GL_TEXTURE_2D, texture1.name);
}];

[myNode.geometry.firstMaterial handleBindingOfSymbol:@"textureImage2" usingBlock:^(unsigned int programID, unsigned int location, SCNNode *renderedNode, SCNRenderer *renderer) {
    glBindTexture(GL_TEXTURE_2D, texture2.name);
}];

What's weird is that the first image appears for both textures.

Can I only use one texture per frag file?

This is iOS 8.

1

There are 1 answers

0
Josh Knowles On

It works if I do this:

[myNode.geometry.firstMaterial handleBindingOfSymbol:@"textureImage1" usingBlock:^(unsigned int programID, unsigned int location, SCNNode *renderedNode, SCNRenderer *renderer) {
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture1.name);
    glUniform1i(location, 0);
}];

[myNode.geometry.firstMaterial handleBindingOfSymbol:@"textureImage2" usingBlock:^(unsigned int programID, unsigned int location, SCNNode *renderedNode, SCNRenderer *renderer) {
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2.name);
    glUniform1i(location, 1);
}];