来自BMP图像的OpenGL纹理绑定问题

时间:2013-10-21 05:51:10

标签: c++ opengl graphics textures bmp

我正在尝试加载大小为1024x1024的BMP图像并将其作为纹理绑定在openGL中。 对于尺寸为256x256的BMP,输出是合乎需要的。但对于尺寸为1024x1024的BMP,  它引发了以下错误

The program has unexpectedly finished.
C:\Users\xxxx\Desktop\lapping-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug\lapping.exe exited with code -1073741819

加载BMP的代码,

Texture2d* loadBMP( char *fname){
    int i,j, w, h, bits;
    unsigned long l;
    GLubyte c[3];
    Texture2d* tex;
    FILE *fin;
    unsigned char head[54];
    fin = fopen( fname,"rb");
    if( fin == NULL){
        printf("Bitmap '%s' not found\n", fname);
        return NULL;
    }
    fread( head, 54, 1, fin);
    w = (head[21]<<24)|(head[20]<<16)|(head[19]<<8)|head[18]; //returns the width of the image
    h = (head[25]<<24)|(head[24]<<16)|(head[23]<<8)|head[22];//returns the height of the image          
    tex = new Texture2d[1];
    tex->w = w;
    tex->h = h;
    tex->buf = new GLubyte[h*w*4];
    tex->id = avTotalTextures++;
    for(i=h-1;i>=0;i--){
        l = i*w*4;
        for(j=0;j<w;j++){
            fread( c, 1, 3, fin);
            tex->buf[l++] = c[2];
            tex->buf[l++] = c[1];
            tex->buf[l++] = c[0];
            tex->buf[l++] = 255;

        }
    }
    fclose( fin);
    printf("Bitmap_load '%s' loaded\n", fname);
    return tex;
}

用于绑定纹理的代码,

void Object3d::bindTexture( Texture2d *t)
{
    if( t == NULL)
        return;
    tex = t;

    glBindTexture(GL_TEXTURE_2D, tex->id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->buf);
    gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, tex->buf);
    delete [] tex->buf;
    tex->buf = NULL;
}

这适用于尺寸为256x256的BMP。但是当我试图通过改变

中的宽度和高度参数来使这个工作为bmp为1024x1024
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->buf);
gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, tex->buf);

分别从0,0和256,256到1024,1024,它会抛出前面提到的错误。我已经提到了openGL规范,但是没有理由说明为什么这不起作用。

0 个答案:

没有答案