我正在尝试加载大小为 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。但是当我尝试通过更改宽度和高度参数来使其适用于 1024x1024 的 bmp
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 规范,但无法想出为什么这不起作用的原因。