使用DEVIL加载ogl纹理

时间:2015-03-20 20:00:52

标签: c++ opengl segmentation-fault devil

这是我的代码

typedef struct Texture
{
    GLubyte *Data;
    GLuint  bpp;
    GLuint width, height;
    GLuint ID;
}Texture;

class TextureLoader
{
    public:
    TextureLoader()
    {
        ilInit();
        iluInit();
    }
    void load(ILenum FileType, const char *filename, Texture *texture)
    {
        ilLoad(FileType, filename);

        texture->width = ilGetInteger(IL_IMAGE_WIDTH);
        texture->height = ilGetInteger(IL_IMAGE_HEIGHT);
        texture->bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);

        texture->Data = ilGetData();
        ilEnable(IL_CONV_PAL);

        unsigned int type = ilGetInteger(IL_IMAGE_FORMAT);
        glGenTextures(1, &texture->ID);
        glBindTexture(GL_TEXTURE_2D,texture->ID);
        gluBuild2DMipmaps(GL_TEXTURE_2D, texture->bpp, texture->width,
            texture->height, type, GL_UNSIGNED_BYTE, texture->Data);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    }
}Loader;

它似乎不起作用。
调用gluBuild2DMipmaps后出现分段错误 我无法理解什么是问题,因为代码主要是从示例中复制粘贴的(... 这是TextureLoader :: load:

的调用
vector <Texture> bgtextures;
bgtextures.resize(1);
Loader.load(IL_JPG,"bgsnow.jpg",&bgtextures[0]);

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

看看这个的返回值:

ilLoad(FileType, filename);

通过执行以下代码:

if (ilLoad(FileType, filename) == IL_FALSE) {
    //Make it produce an error or something.
}

正如genpfault建议的那样,你可能传递了错误的文件名。