How to use GL_TEXTURE_2D_ARRAY in OpenGL 3.2

10,545
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);

Your texture in fact does not have mipmaps. So stop telling OpenGL that it does.

Also, always set the mipmap range parameters (GL_TEXTURE_BASE_LAYER and GL_TEXTURE_MAX_LAYER) for your texture. Or better yet, use texture storage to allocate your texture's storage, and it will do it for you.

Share:
10,545
TheAmateurProgrammer
Author by

TheAmateurProgrammer

An amateur programmer (still) after 4 years of development. Well versed in Objective-C and C, and somewhat proficient at Java and C++. Currently playing around with OpenGL on Mac OS X, learning more and more about OSX's frameworks as well as a bit of dabble in computer science using Java/C++/Python. Starting to learn Python and x86 Assembly.

Updated on June 07, 2022

Comments

  • TheAmateurProgrammer
    TheAmateurProgrammer almost 2 years

    So I've tried following the docs, however I can't seem to get a texture 2D array to work.

    -(GLint)buildTextureArray:(NSArray *)arrayOfImages
    {
        GLImage *sample = [GLImage imageWithImageName:[arrayOfImages objectAtIndex:0] shouldFlip:NO]; //Creates a sample to examine texture width and height
        int width = sample.width, height = sample.height;
        GLsizei count = (GLsizei)arrayOfImages.count; 
    
        GLuint texture3D;
        glGenTextures(1, &texture3D);
        glBindTexture(GL_TEXTURE_2D_ARRAY, texture3D);
    
        glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    
        glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_REPEAT);
        glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, width, height, count, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
    
        int i = 0;
        for (NSString *name in arrayOfImages) //Loops through everything in arrayOfImages
        {
            GLImage *image = [GLImage imageWithImageName:name shouldFlip:NO]; //My own class that loads an image
            glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, image.width, image.height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image.data);
            i++;
        }
        return texture3D;
    }
    
    //Setting Uniform elsewhere
    glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray);
    glUniform1i(textures, 0);    
    
    
    //Fragment Shader
    #version 150
    
    in vec3 texCoords;
    
    uniform sampler2DArray textures;
    
    out vec3 color;
    
    void main()
    {
        color = texture(textures, texCoords.stp, 0).rgb;
    }
    

    I am able to load individual textures with the same texture parameters, but I can't get it to work with the texture 2D array. All I get is a black texture. Why is this happening?