原来是LoadBMP()函数是教程示例中唯一的问题。在这里,我发布了此函数的修改版本,以便您可以运行示例。只需将 LoadBMP 替换为:
/* simple loader for 24bit bitmaps (data is in rgb-format) */
int loadBMP(char *filename, textureImage *texture)
{
FILE *file;
/* unsigned short int bfType;
long int bfOffBits;
short int biPlanes;
short int biBitCount;
long int biSizeImage;
int i;
unsigned char temp;*/
uint16_t bfType;
int32_t bfOffBits;
short int biPlanes;
short int biBitCount;
long int biSizeImage;
int i;
unsigned char temp;
/* make sure the file is there and open it read-only (binary) */
if ((file = fopen(filename, "rb")) == NULL)
{
printf("File not found : %s\n", filename);
return 0;
}
if(!fread(&bfType, sizeof(uint16_t), 1, file))
{
printf("Error reading file!\n");
return 0;
}
/* check if file is a bitmap */
if (bfType != 19778)
{
printf("Not a Bitmap-File!\n");
return 0;
}
/* get the file size */
/* skip file size and reserved fields of bitmap file header */
fseek(file, 8, SEEK_CUR);
/* get the position of the actual bitmap data */
if (!fread(&bfOffBits, sizeof(int32_t), 1, file))
{
printf("Error reading file!\n");
return 0;
}
printf("Data at Offset: %ld\n", bfOffBits);
/* skip size of bitmap info header */
fseek(file, 4, SEEK_CUR);
/* get the width of the bitmap */
fread(&texture->width, sizeof(int), 1, file);
printf("Width of Bitmap: %d\n", texture->width);
/* get the height of the bitmap */
fread(&texture->height, sizeof(int), 1, file);
printf("Height of Bitmap: %d\n", texture->height);
/* get the number of planes (must be set to 1) */
fread(&biPlanes, sizeof(short int), 1, file);
if (biPlanes != 1)
{
printf("Error: number of Planes not 1!\n");
return 0;
}
/* get the number of bits per pixel */
if (!fread(&biBitCount, sizeof(short int), 1, file))
{
printf("Error reading file!\n");
return 0;
}
printf("Bits per Pixel: %d\n", biBitCount);
if (biBitCount != 24)
{
printf("Bits per Pixel not 24\n");
return 0;
}
/* calculate the size of the image in bytes */
biSizeImage = texture->width * texture->height * 3;
printf("Size of the image data: %ld\n", biSizeImage);
texture->data = malloc(biSizeImage);
/* seek to the actual data */
fseek(file, bfOffBits, SEEK_SET);
if (!fread(texture->data, biSizeImage, 1, file))
{
printf("Error loading file!\n");
return 0;
}
/* swap red and blue (bgr -> rgb) */
for (i = 0; i < biSizeImage; i += 3)
{
temp = texture->data[i];
texture->data[i] = texture->data[i + 2];
texture->data[i + 2] = temp;
}
return 1;
}