如何在iOS中的SceneKit中的OpenGL SL着色器中使用多个纹理?

时间:2015-06-12 22:17:33

标签: ios opengl-es scenekit

我有一个包含这两个变量的碎片文件:

uniform highp sampler2D textureImage1;
uniform highp sampler2D textureImage2;

在我的代码中,我用以下方法将图像绑定到这些:

[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);
}];

奇怪的是,两个纹理都出现了第一个图像。

每个frag文件只能使用一个纹理吗?

这是iOS 8。

1 个答案:

答案 0 :(得分:2)

如果我这样做,它会起作用:

[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);
}];