无法使用 DSA 加载纹理

Not able to load texture using DSA

如果我不使用 DSA 创建纹理,我的代码就可以工作。

这就是我使用 DSA 创建纹理的方式。

我的opengl版本是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;
}

您必须使用 glTextureStorage2D 创建纹理存储。例如:

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