1

我编写了这个函数来在 OpenGL 中生成纹理。它正确地生成了我想要的纹理,它是一个木盒。

// This works
bool GenerateTextureFromFile(const int32_t mipMapLevel)
{
    int32_t width, height, channels;

    uint8_t* data = stbi_load(mProps.mPath.c_str(), &width, &height, &channels, 0);

    if (!data)
    {
        cout << "Failed to generate texture." << endl;

        return false;
    }

    mProps.mWidth = width;
    mProps.mHeight = height;

    Bind();

    glTexImage2D(GL_TEXTURE_2D,
                 mipMapLevel,
                 (uint32_t)mProps.mInternalFormat,
                 mProps.mWidth,
                 mProps.mHeight,
                 0,
                 (uint32_t)mProps.mImageFormat,
                 mProps.mDataType,
                 data);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mProps.mWrapS);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mProps.mWrapT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mProps.mFilterMin);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mProps.mFilterMax);

    if (mipMapLevel)
        glGenerateMipmap(GL_TEXTURE_2D);    

    stbi_image_free(data);

    return true;
}

我想将该功能包装到另一个函数中,但是当我这样做时 glTexImage2D 似乎没有生成正确的纹理。我得到的不是木箱,而是黑色纹理,表示纹理无法生成。当我从 uint8_t* 转换为 void* 时会丢失数据吗?做这个的最好方式是什么?

// This fails to generate a texture
void SetDataNew(void* data, TextureProperties props, const int32_t mipMapLevel)
{
    mProps = std::move(props);

    Bind();

    glTexImage2D(GL_TEXTURE_2D,
                 mipMapLevel,
                 (uint32_t)mProps.mInternalFormat,
                 mProps.mWidth,
                 mProps.mHeight,
                 0,
                 (uint32_t)mProps.mImageFormat,
                 mProps.mDataType,
                 data);

    glTextureParameteri(mObjectID, GL_TEXTURE_MIN_FILTER, mProps.mFilterMin);
    glTextureParameteri(mObjectID, GL_TEXTURE_MAG_FILTER, mProps.mFilterMax);
    glTextureParameteri(mObjectID, GL_TEXTURE_WRAP_S, mProps.mWrapS);
    glTextureParameteri(mObjectID, GL_TEXTURE_WRAP_T, mProps.mWrapT);

    if (mipMapLevel)
        glGenerateMipmap(GL_TEXTURE_2D);
}

bool GenerateTextureFromFile(const int32_t mipMapLevel)
{
    int32_t width, height, channels;

    uint8_t* data = stbi_load(mProps.mPath.c_str(), &width, &height, &channels, 0);

    if (!data)
    {
        cout << "Failed to generate texture." << endl;

        return false;
    }

    mProps.mWidth = width;
    mProps.mHeight = height;

    SetDataNew(data, mProps, mipMapLevel);

    stbi_image_free(data);

    return true;
}

这是我的 TextureProperties 结构:

struct TextureProperties
{
    std::string mPath;
    uint32_t mWidth = 1;
    uint32_t mHeight = 1;
    TextureFormat mInternalFormat = TextureFormat::RGBA;
    TextureFormat mImageFormat = TextureFormat::RGBA;
    uint32_t mDataType = GL_UNSIGNED_BYTE;
    uint32_t mWrapS = GL_REPEAT;
    uint32_t mWrapT = GL_REPEAT;
    uint32_t mFilterMin = GL_LINEAR;
    uint32_t mFilterMax = GL_LINEAR;
};
4

1 回答 1

0

问题在这里刚刚意识到不小心放了 objectID 而不是 GL_TEXTURE_2D

glTextureParameteri(mObjectID, GL_TEXTURE_MIN_FILTER, mProps.mFilterMin);
glTextureParameteri(mObjectID, GL_TEXTURE_MAG_FILTER, mProps.mFilterMax);
glTextureParameteri(mObjectID, GL_TEXTURE_WRAP_S, mProps.mWrapS);
glTextureParameteri(mObjectID, GL_TEXTURE_WRAP_T, mProps.mWrapT);
于 2021-04-13T11:42:13.810 回答