Thread: No one can seem to help me, loading bitmap

  1. #1
    Shadow12345
    Guest

    No one can seem to help me, loading bitmap

    This is supposedly very simple to do, yet I can't get it to work. It uses OpenGL functions to actually draw the bitmap but i still think it's more appropriate in the C++ board. Anyway this doesn't work and I'm wondering if I did anything wrong

    code for loading the bitmap:
    Code:
    byte * LoadBMP(char * filename, BITMAPINFOHEADER *bih) {
    	byte * imageData;	//THIS WILL END UP BEING OUR IMAGE READ FROM THE FILE
    	ifstream fin(filename, ios::in | ios::binary | ios::nocreate);	//OPEN FILE
    	if(fin.fail())
                 return NULL;                                       
    	fin.seekg(0, ios::end);
    	long filesize = fin.tellg();
    	fin.seekg(0, ios::beg);
    	byte * buffer = new byte[filesize];  //ALLOCATE TEMPORARY BUFFER
    	fin.read(buffer, filesize);
    	fin.close();
    	const byte * ptr = buffer;	//CREATE POINTER TO BUFFER TO EXTRACT STUFF FROM THE FILE
    	BITMAPFILEHEADER * pHeader = (BITMAPFILEHEADER*)ptr;	//EXTRACT THE BITMAPFILEHEADER TO CHECK TYPE
    	ptr += sizeof(BITMAPFILEHEADER);	//GO PAST BITMAPFILEHEADER IN FILE
    	if(pHeader->bfType != BITMAP_ID) {	//CHECK THE TYPE, DEFINED IN MAIN.H
    		MessageBox(NULL, "BITMAP ID INVALID", "BITMAP ID INVALID", MB_OK);
    		return NULL;
    	}
    	bih = (BITMAPINFOHEADER*) ptr;	//EXTRACT OUR BITMAPINFOHEADER
    	ptr += sizeof(BITMAPINFOHEADER);
    
    	imageData = new byte[bih->biSizeImage];
    	imageData = (byte*)ptr;	//EXTRACT ALL OF THE INFORMATION FOR THE IMAGE
    	//SWAP THE R AND B VALUES OF THE IMAGE DATA
    	
    	return imageData;	//RETURN IT
    }
    code used in main to actually draw the bitmap (this is opengl specific stuff):
    Code:
    //globals
    byte * bitmap;
    BITMAPINFOHEADER bih;
    //...
    	glRasterPos2i(100, 100);	//START DRAWING AT THESE PIXELS
    	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);	//SET MEMORY ALIGNMENT
    	glDrawPixels(bih.biWidth, bih.biHeight, GL_RGB, GL_UNSIGNED_BYTE, bitmap); //DRAW STUFF!
    this should be displaying something, albeit the fact that I should have switched the red and blue values with the current mode I'm using in drawpixels (supposedly with opengl 1.2 and later you can use GL_BGR to draw, but that enumerated type doesn't seem to exist).

    thanks if anyone can help this poor soul

  2. #2

  3. #3
    Shadow12345
    Guest
    if you had actually read any of those yourself you would realize that they do things completely differently than I am doing. I am not interested in cutting and pasting code. I have already read tutorials on loading bitmaps, so I know how it needs to be done, but I need to know why my variation of it doesn't work. So you see, it's kind of, how should i put this, along the lines of pointless to be pointing me to tutorials on loading bitmaps when I obviously know HOW it should be done, those tutorials won't help with my version.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Does anything get drawn at all?

  5. #5
    Shadow12345
    Guest
    No, the screen is just blank

    No error message is given, but I think I may add more error checking. I didn't know if it mattered at all, but I tried making the dimensions be a power of 2 (I know the bitmaps have to be a power of 2 when texturing to a polygon) but alas, no success

    EDIT:
    I tried doing some simple error checking, just to make sure things were in fact being created, but no errors come up, I'm perplexed
    EDIT1:
    Is the way I did the error checking even valid/good? Should I try doing something with try/catch? (been a long time since I've tried using try/catch)


    Code:
    #include "Main.h"
    
    byte * LoadBMP(char * filename, BITMAPINFOHEADER *bih) {
    	byte * imageData;	//THIS WILL END UP BEING OUR IMAGE READ FROM THE FILE
    	ifstream fin(filename, ios::in | ios::binary | ios::nocreate);	//OPEN FILE
    	if(fin.fail())
                 return NULL;                                       
    	fin.seekg(0, ios::end);
    	long filesize = fin.tellg();
    	fin.seekg(0, ios::beg);
    	byte * buffer = new byte[filesize];  //ALLOCATE TEMPORARY BUFFER
    	fin.read(buffer, filesize);
    	fin.close();
    	const byte * ptr = buffer;	//CREATE POINTER TO BUFFER TO EXTRACT STUFF FROM THE FILE
    	if(!ptr) {
    		MessageBox(NULL, "COULD NOT INITIALIZE CONST POINTER TO BUFFER", "ERROR", MB_OK);
    		return 0;
    	}
    	BITMAPFILEHEADER * pHeader = (BITMAPFILEHEADER*)ptr;	//EXTRACT THE BITMAPFILEHEADER TO CHECK TYPE
    	if(!pHeader) {
    		MessageBox(NULL, "BITMAPFILEHEADER couldn't be initialized", "ERROR", MB_OK);
    		return 0;
    	}
    	ptr += sizeof(BITMAPFILEHEADER);	//GO PAST BITMAPFILEHEADER IN FILE
    	
    	if(pHeader->bfType != BITMAP_ID) {	//CHECK THE TYPE, DEFINED IN MAIN.H
    		MessageBox(NULL, "BITMAP ID INVALID", "BITMAP ID INVALID", MB_OK);
    		return NULL;
    	}
    	bih = (BITMAPINFOHEADER*) ptr;	//EXTRACT OUR BITMAPINFOHEADER
    	if(!bih) {
    		MessageBox(NULL, "COULD NOT INITIALIZE BITMAP INFO HEADER", "ERROR", MB_OK);
    		return 0;
    	}
    	ptr += sizeof(BITMAPINFOHEADER);
    
    	imageData = new byte[bih->biSizeImage];
    	imageData = (byte*)ptr;	//EXTRACT ALL OF THE INFORMATION FOR THE IMAGE
    
    
      return imageData;	//RETURN IT
    }
    Hmm, I'll figure it out eventually I guess
    Last edited by Shadow12345; 12-28-2002 at 01:20 PM.

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Why not try the Load Bitmap function in the glaux library?
    I tried loading a raw bitmap with opengl and displaying it, but failed miserably. So I quit OpenGL, and "OpenGL Superbible" sits on my shelf gathering dust.

    Try your loading function with the Windows GDI, just to see if you're actually loading the bitmaps. That's what I did... plus, if you have petzolds book (or an ebook) he has working examples that you can easily port over to C++ ifstreams.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Oh, and you might have a problem trying to extract sizeof(BITMAPFILEHEADER/BITMAPINFOHEADER/BITMAPINFO) structures if they're aligned on the 32-bit boundary.

  8. #8
    Shadow12345
    Guest
    Why not try the Load Bitmap function in the glaux library?
    I tried loading a raw bitmap with opengl and displaying it, but failed miserably. So I quit OpenGL, and "OpenGL Superbible" sits on my shelf gathering dust.

    Try your loading function with the Windows GDI, just to see if you're actually loading the bitmaps. That's what I did... plus, if you have petzolds book (or an ebook) he has working examples that you can easily port over to C++ ifstreams.
    A few reasons, first of all I'm a paranoid schizophrenic when it comes to original material, whenever I see something that I can possibly write on my own I try for it. Secondly I've 'heard' that glaux is, in short, awful, that it isn't optimized, and that is even buggy and corrupted. Basically everyone at gamedev.net told me to write my own simple routines for bitmap loading. Yeah well thanks for replying Eibro you've always been a good sport.

    I have the opengl superbible, it's good because it shows how to write across all platforms using glut, and just concentrates on opengl not winapi crap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading bitmap file into a HBITMAP
    By wind_addict in forum Windows Programming
    Replies: 9
    Last Post: 11-17-2007, 10:42 AM
  2. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. Loading a Bitmap resource for OpenGL Texture[x]
    By the dead tree in forum Game Programming
    Replies: 4
    Last Post: 08-26-2004, 01:12 PM
  5. Loading bitmap in dll
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2003, 01:53 PM