Thread: ARG.... My Texture Loader is screwed... PLEASE HELP!

  1. #1
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Exclamation ARG.... My Texture Loader is screwed... PLEASE HELP!

    I am working on a Graphics API for DOS. Its going great untill now.

    I wrote the texture mapper and that, and now I am trying to load a bitmap to 'create' a texture from a file. I tried modfying the cBoard FAQ's bitmap loader... but for some reason I just get static? It works okay on it's own... but one I change it to load into my texture pointer it dies... here is the troubling function:

    UPDATE: I re-wrote it, and I think I know what the problem is!!!

    For some reason, it sort of warps the image, the image IS being loaded... but for some reason its being entered into the buffer incorrectly, I am going to try rewritingthe mapper...

    UPDATE: AGAIN... I think the mapper AND the loader are at fault... I have discovered that the loader is laodign a warnped image still... even without the mapper's imperfections.

    (For this project I am compiling using Bc5 IDE):

    u_IBitmap *DLG_Runtime::LoadBMP (char *filename){
    struct BMPHeader {
    char ID[2];
    unsigned long Size;
    unsigned long _Res;
    unsigned long Foffset;
    unsigned long hsize;
    unsigned long sizeX;
    unsigned long sizeY;
    unsigned int Planes;
    unsigned int BitsPerPel;
    unsigned long Compression;
    unsigned long bitlen;
    unsigned long xmet;
    unsigned long ymet;
    unsigned long Bits;
    //unsigned long *Pallet;
    //char *CompUnused;
    } Header;

    u_IBitmap *Ret = new u_IBitmap;

    _uZeroMemory((char*)Ret,sizeof(u_IBitmap));

    //This checks for the file
    ifstream BMPFile(filename, ios::binary);
    if (BMPFile == NULL) {

    return (u_IBitmap*)NULL;
    }

    BMPFile.read((char*)&Header,sizeof(Header));

    Ret->sizeX = Header.sizeX;
    Ret->sizeY = Header.sizeY;

    Ret->data = new char[Ret->sizeX*Ret->sizeY];

    BMPFile.seekg(Header.Foffset);

    BMPFile.read(Ret->data,Ret->sizeX*Ret->sizeY);

    BMPFile.close();
    return Ret;
    }
    Here is the 'u_IBitmap' struct:


    struct u_IBitmap
    {
    unsigned long sizeX,sizeY;
    char *data;
    };


    The texture mapper itself seems to work... but this I beleive if the bad function...

    SPH
    Last edited by minime6696; 01-19-2002 at 05:24 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    BMPs are zero padded to the nearest byte boundary. If you do not account for this then your image will be warped when you display it.

  3. #3
    Is your bitmaps' width and heighth divisable by 4?

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Question ?

    Thanks Bubba, can you elaborate, like how I would implement this?

    Also, it doesnt matter if it is / by 4, becouse I am writing the mapper and loader myself... for my own API..., and thats not a requirement.

    SPH

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yes it does matter if your BMP is divisible by four. Without accounting for the padding to the nearest byte boundary all of your bmps must be divisible by four or they will not display correctly.

    I believe the formula I used was

    if (x>(4-(BMPWidth % 4)))
    {
    //Next scan line
    y++;
    x=StartX;
    }

    If that does not work just try

    if (x>(BMPWidth %4))
    {
    //Next scan line
    y++;
    x=StartX;
    }

    I did not write the entire bitmap draw function but you get the idea. Also, remember that all BMPs start at bottom right so you may need to start at the bottom scan line and work up. Whatever you do, just make sure that you are always linearily tracing through the bitmap. This way you never have to calculate the offset into the bitmap. If you use one offset into the bitmap and one offset into the frame buffer you will save two multiplies per pixel. One to compute offset into bitmap and one to compute offset into frame buffer.

    If this is unclear, post again and I'll try to show you what I mean with some code.

    Also with BMPs make sure you are accounting for the fourth color component when you read in the data, or the alignment will be messed up and the colors will not be correct. I usually read in the data with a block read and then have a function that extracts the RGBA values into an array of RGBA structs, the A being the fourth color component (usually alpha but normally not used at all in BMPs)

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In a program I made one time I had a problem that actually stemmed from the bitmap header. When I did fread(&bmheader, sizeof(header), 1, file_handle) I found that sizeof(header) was actually 2bytes larger than it should have been! That may have been an error on my compiler's behalf but check it anyway. Also read in the width of each line, skip width%4 bytes of data then read the next line.

  7. #7
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Question Help?

    Well one problem... could you epxlain it more? like I dont know what all of the variables are and stuff... a great thing would be a function the simple writes to something like this:

    struct _RGBA
    {
    char r,g,b,a;
    };

    struct _Bitmap
    {
    unsigned int x,y;
    _RGBA *data;
    };

    Thanks! SPH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. d3d9 c++ texture wrapper... again...
    By yaya in forum Game Programming
    Replies: 0
    Last Post: 04-01-2009, 01:08 PM
  2. D3d Texture Wrapper == ARRRGGGGHHH
    By yaya in forum Game Programming
    Replies: 1
    Last Post: 03-25-2009, 06:41 PM
  3. Replies: 4
    Last Post: 02-27-2008, 03:10 PM
  4. OpenGL - look trough surface -nehe lesson
    By GanglyLamb in forum Game Programming
    Replies: 8
    Last Post: 08-24-2006, 11:06 PM
  5. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM