Results 1 to 3 of 3

Thread: Hmmmm, bug in heightmap...

  1. #1
    Senior Member Regular Contributor
    Join Date
    Nov 2000
    Sydney, NSW, Australia

    Hmmmm, bug in heightmap...

    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;

    * *
    * *
    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;

    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[(i*TERRAIN_WIDTH*3) + 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?
    :: Sleep is a poor substitute for caffeine ::

  2. #2
    Junior Member Newbie
    Join Date
    Aug 2000
    Mississauga, Ontario, Canada

    Re: Hmmmm, bug in heightmap...

    well, the negative numbers problem is probably because you are using chars for your data. try using unsigned char and see if that helps, [or] try casting to unsigned char before casting to float.

    [This message has been edited by Indiana (edited 04-21-2002).]

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jan 2002

    Re: Hmmmm, bug in heightmap...

    Try changing your
    fseek( file, offset, SEEK_CUR )

    fseek( file, offset, SEEK_SET )

    And as Indiana said you have to make your data unsigned char...

    If you use grayscale you have 8 or 4 bit data files... This is recomended because you have a file that is three times smaller than a 24 bit pic... A lot faster to load!

    What are you making your files with, Photoshop? If you are, then when you create a new pic, in the mode box you select grayscale.

Similar Threads

  1. heightmap
    By grimoire in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-09-2004, 06:26 PM
  2. heightMap help
    By Pops in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 09-20-2003, 02:02 AM
  3. Heightmap
    By in forum OpenGL: Linux
    Replies: 2
    Last Post: 11-27-2001, 01:07 AM
  4. Heightmap from TGA
    By MrShoe in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-15-2001, 12:21 PM
  5. heightmap
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 12-12-2000, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean