将 uint8_t* 传递给 void* 参数函数时丢失数据?

Losing data when passing uint8_t* to void* parameter function?

我写这个函数是为了在 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;
};

这里的问题刚刚意识到不小心把 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);