OpenGL 4.1及以下版本的黑色纹理,Mac和Windows。

问题描述 投票:0回答:1

我在不支持OpenGL 4.5的低端PC和mac上编译OpenGL代码时遇到了这个问题。在我的常规代码中,我会使用glCreateTextures和glTextureStorage2D这样的函数,但其他版本不支持,所以我走了其他glGenTextures的路径。

这里是图像生成代码。

Texture::Texture(const std::string& path)
        : m_Path(path)
    {
        int width, height, channels;
        stbi_set_flip_vertically_on_load(1);
        unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);

        RW_CORE_ASSERT(data, "Failed to load image!");
        m_Width = width;
        m_Height = height;

        GLenum internalFormat = 0, dataFormat = 0;
        if (channels == 4)
        {
            internalFormat = GL_RGBA8;
            dataFormat = GL_RGBA;
        }
        else if (channels == 3)
        {
            internalFormat = GL_RGB8;
            dataFormat = GL_RGB;
        }

        m_InternalFormat = internalFormat;
        m_DataFormat = dataFormat;

        RW_CORE_ASSERT(internalFormat & dataFormat, "Format not supported!");

        glGenTextures(1, &m_ID);

        glBindTexture(GL_TEXTURE_2D, m_ID);


        glTexParameteri(m_ID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(m_ID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        glTexParameteri(m_ID, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(m_ID, GL_TEXTURE_WRAP_T, GL_REPEAT);

        glTexImage2D(m_ID, 1, internalFormat, m_Width, m_Height, 0, dataFormat, GL_UNSIGNED_BYTE, data);

        glBindTexture(GL_TEXTURE_2D, 0);

        stbi_image_free(data);
    }

我想把我的纹理绑定到GPU上的特定插槽上,所以我有这个函数。

void Texture::Bind(uint32_t slot) const
    {

        glActiveTexture(GL_TEXTURE0 + slot);
        glBindTexture(GL_TEXTURE_2D, m_ID); 
    }

这是被绘制的截图enter image description here

为了确保不是渲染问题,我决定把它放到ImGui的渲染器中去enter image description here

这就是我应该得到的图片。

enter image description here

图片正确导入,没有错误,同样的导入代码和路径在高端电脑上也能用,唯一改变的是高端电脑有OpenGL 4.5的纹理生成代码。

c++ opengl 2d textures stb-image
1个回答
0
投票

原来我必须在这些地方指定GL_TEXTURE_2D而不是纹理ID。

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

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 1, internalFormat, m_Width, m_Height, 0, dataFormat, GL_UNSIGNED_BYTE, data);

© www.soinside.com 2019 - 2024. All rights reserved.