Not able to load texture using DSA

Viewed 58

My code works if i don't use DSA to create texture.

This is how i am creating texture using DSA.

My opengl version is 4.6

unsigned int loadTexture(char const* path)
{
    unsigned int textureID;
  //  glGenTextures(1, &textureID);

    glCreateTextures(GL_TEXTURE_2D, 1, &textureID);
   
    int width, height, nrComponents;
    unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);

    if (data)
    {
        GLenum format;
        if (nrComponents == 1)
            format = GL_RED;
        else if (nrComponents == 3)
            format = GL_RGB;
        else if (nrComponents == 4)
            format = GL_RGBA;    

        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);
       
        glTextureSubImage2D(textureID, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, data);
        glGenerateTextureMipmap(textureID);

        stbi_image_free(data);
    }
    else
    {
        
        stbi_image_free(data);
    }

    glBindTextureUnit(0, textureID);

    return textureID;
}
1 Answers

You have to create the texture storage with glTextureStorage2D. e.g.:

glCreateTextures(GL_TEXTURE_2D, 1, &textureID);
glTextureStorage2D(textureID, 1, GL_RGBA8, width, height);
Related