【问题标题】:OpenGL 4.1 and lower Black Texture, Mac and WindowsOpenGL 4.1 和更低的黑色纹理,Mac 和 Windows
【发布时间】:2020-06-09 21:24:20
【问题描述】:

我在不支持 OpenGL 4.5 和 mac 的低端 PC 上编译 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); 
    }

这是绘制的屏幕截图:

为了确保这不是渲染问题,我决定将它放入 ImGui 的渲染器中。

这是我应该得到的图片:

并且图像导入正确,我没有收到任何错误,并且相同的导入代码和路径在高端 PC 上工作,唯一改变的是高端 PC 具有 OpenGL 4.5 纹理生成代码。

【问题讨论】:

  • slot 使用哪些值?您使用哪个着色器版本?
  • 我觉得我用的是shader version 330 core

标签: c++ opengl 2d textures stb-image


【解决方案1】:

原来我必须在这些地方指定 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 2012-02-07
    • 2010-09-24
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多