My heightmap loader is giving me crap… It is giving out negative numbers and so on… here is the code, im using 24 bit bitmap heightmap even though i should be using greyscale…
Anyway, code:
// Structure for holding bitmap info
struct Image{
long SizeX;
long SizeY;
char *data;
};
typedef struct Image Image;
/*****************************
-
*
-
LOADBMP FUNCTION *
-
*
*****************************/
void LoadBMP(char *filename, Image *image){
FILE *file;
short int bpp;
short int planes;
long i;
long size;
char temp;
// Open the file
file = fopen(filename, “rb”);
// Seek to the 18th byte in the file
fseek(file, 18, SEEK_CUR);
// Read the size of the texture
i = fread(&image->SizeX, 4, 1, file);
i = fread(&image->SizeY, 4, 1, file);
size = image->SizeX * image->SizeY * 3;
i = fread(&bpp, 2, 1, file);
i = fread(&planes, 2, 1, file);
fseek(file, 24, SEEK_CUR);
image->data = (char *)malloc(size);
// Read the pixel information from the file
i = fread(image->data, size, 1, file);
// Convert BRG pixels into RGB pixels
for(i = 0; i < size; i+=3){
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
fclose(file);
free(image);
}
void generateHeightmapTerrain(void){
int i, i2;
Image *map;
map = (Image *)malloc(sizeof(Image));
LoadBMP(heightmap, map);
for(i = 0; i < TERRAIN_WIDTH; i++){
for(i2 = 0; i2 < TERRAIN_LENGTH; i2++){
terrain_height[i][i2] = (float)map->data[(iTERRAIN_WIDTH3) + i2*3]/HEIGHT_MODIFIER;
}
}
}
Now, im getting negative numbers for some of my heights, and some are larger than 255… ummm, this shouldnt be happening since the bitmap pizel colours are from 0 to 255 only… Can anyone help?