ios - 无法在 opengl es 2.0 中加载多个纹理

标签 ios opengl-es textures

我试图加载两个纹理并将它们传递到我的着色器中的两个采样器,但是两个采样器都返回我加载的第一个纹理。此外,如果我不将纹理加载到 GL_TEXTURE0 中,两个采样器都会返回黑色。我已将其缩减为单个采样器/纹理,但仍然存在相同的问题:

    GLuint texture1;

    glGenTextures(2, &texture1);

    glActiveTexture(GL_TEXTURE0); //if this is GL_TEXTURE1 or any other, I get black
    glBindTexture(GL_TEXTURE_2D, texture1);
    glEnable(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    [self LoadTextureData: [[NSBundle mainBundle] pathForResource:@"normals" ofType:@"png"]];

    glUniform1i(uniforms[SAMPLER_1], 1); //whether this is 0 or 1 or anything else doesn't seem to make any difference for both samplers. 

    return TRUE;

关于我做错了什么的任何想法? 我有理由相信我的采样器索引是正确的,我的着色器也是如此。 LoadTextureData 归结为 glTexImage2D。

编辑: 这就是我尝试加载和设置两个纹理的方式

        //texture 1
    GLuint texture1;

    glGenTextures(1, &texture1);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture1);
    glEnable(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    [self LoadTextureData: [[NSBundle mainBundle] pathForResource:@"normals" ofType:@"png"]];

    //texture 2
    GLuint texture2;

    glGenTextures(1, &texture2);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2);
    glEnable(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    [self LoadTextureData: [[NSBundle mainBundle] pathForResource:@"bricks" ofType:@"jpg"]];


    glUniform1i(uniforms[SAMPLER_1], 0);
    glUniform1i(uniforms[SAMPLER_2], 1);

结果是两个采样器都返回“法线”图像。

LoadTextureData、采样器 ID 的分配和我的着色器可以在 http://paste2.org/p/1514200 找到

最佳答案

总而言之:确保在加载着色器后将纹理分配给采样器。我被骗了,以为 uniforms[] 被正确分配了,因为我只是在所有加载完成后才检查它。

关于ios - 无法在 opengl es 2.0 中加载多个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6650148/

相关文章:

graphics - 确定现有 DDS 纹理文件的 block 压缩格式

java - 使用 Jogl 直接从 volatileImage 创建纹理

iphone - ".../libtool: can' t 找到 : -lxml2"的文件

ios - 我怎样才能看到 UIImageView 中的图像?

iphone - 如何在 TableView 中显示来自 NSMutablearray 的数据

ios - 单击按钮时隐藏键盘?

ios - Opengl Renderbuffer Texture touchmove ios 问题

android - Open GL ES - GLM 库和 Android Studio

android - GLSL 2D 旋转不起作用

c# - XNA 4.0 中的 WebBrowser 无法导航