Home > Software engineering >  How to use a 2d texture array in opengl
How to use a 2d texture array in opengl

Time:05-27

I am trying to load textures into a 2d array and than apply them.

But the screen only shows Black.

This is my code.

Creating Textures.

void int loadTexture(char const* path)
{
    glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &textureID);
   
    int width, height, nrComponents;
    unsigned char* data1 = stbi_load("C:\\Temp\\RED.png" , &width, &height, &nrComponents, 0);   // Load the first Image of size 512   X   512  (RGB)
    unsigned char* data2 = stbi_load("C:\\Temp\\BLUE.png", &width, &height, &nrComponents, 0);  // Load the second  Image of size 512   X   512   (RGB)
    glTextureStorage3D(textureID, 1, GL_RGB8, width, height, 2);
   
     GLenum format;
     format = GL_RGB;

     glTextureSubImage3D(textureID, 1 , 0 , 0 , 1 , width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, data1);
     glTextureSubImage3D(textureID, 1 , 0, 0, 2, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, data2);

      glTextureParameteri(textureID, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTextureParameteri(textureID, GL_TEXTURE_WRAP_T, GL_REPEAT);
      glTextureParameteri(textureID, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      glTextureParameteri(textureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                
      stbi_image_free(data1);
      stbi_image_free(data2);
 
      glBindTextureUnit(0, textureID);
}

Using the textures.

while (!glfwWindowShouldClose(window))
    {
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

         Shader.use();
        Shader.setFloat("textureUnit", 1);   // Bind the first texture unit of the texture array
        glBindTextureUnit(0, textureID);  
        renderQuad();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

Vertex Shader.

#version 460 core

layout( location = 0)in vec2 aPos;
layout( location = 1 )in vec2 a_uv;
out vec2 f_uv;

void main() {
f_uv = a_uv;
gl_Position =   vec4(aPos ,0.0, 1.0 );
};

Fragment Shader.

#version 460 core

uniform sampler2DArray u_tex;
in vec2 f_uv;
out vec4 FragColor;
uniform float textureUnit;


in vec2 TexCoord2;
void main() {

        vec3 texCoordTest = vec3( f_uv.x , f_uv.y , textureUnit);
        vec4 color = texture(u_tex  , texCoordTest);            
        FragColor = color ; 
                                
    };

CodePudding user response:

The 5th argument of glTextureSubImage3D is the zoffset. This is 0 for the 1st texture and 1 for the 2nd texture. The 2nd argument is the level not the number of levels:

glTextureSubImage3D(textureID, 1 , 0 , 0 , 1 , width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, data1);
glTextureSubImage3D(textureID, 1 , 0, 0, 2, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, data2);

glTextureSubImage3D(textureID, 0, 0, 0, 0, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, data1);
glTextureSubImage3D(textureID, 0, 0, 0, 1, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, data2);
  • Related