Skip to main content
gamedev.net
🔒 Locked

Texture Mapping: Bitmap not loading

Started by Tapp 6 replies 750+ views
Original Post
Tapp
Tapp
I'm currently learning OpenGL and have encountered a problem while trying to bind a bitmap to a cube. The coding is very simple as it is a tutorial with a cube rotating on a black background. The program builds successfully and when in debug it displays a white cube rotating. This leads me to believe the bitmap is not loading as everything else works properly. The bitmap is stored under the resource files which is properly included. The bitmap itself has dimensions of a power of 2, which the book I'm using says is required. I have tried changing the pointer along with altering the programming to no avail. If anyone has any advice on how to make the bitmap load or anything else that may be wrong your advice is appreciated.
_DarkWIng_
_DarkWIng_
Did you check for opengl errors while loading (glGetError). Is pointer to bitmap data valid while passing it to opengl?
You should never let your fears become the boundaries of your dreams.
Tapp
Tapp
Thanks for the replies.

Here's the pertinent code,

BITMAPINFOHEADER	bitmapInfoHeader;unsigned char*		bitmapData;unsigned int		texture;unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader){	FILE                *filePtr;					BITMAPFILEHEADER	bitmapFileHeader;			unsigned char		*bitmapImage;				int					imageIdx = 0;		unsigned char		tempRGB;					filePtr = fopen(filename, "rb");	if (filePtr == NULL)		return NULL;	fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);		if (bitmapFileHeader.bfType != BITMAP_ID)	{		fclose(filePtr);		return NULL;	}	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);	fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);	bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);	if (!bitmapImage)	{		free(bitmapImage);		fclose(filePtr);		return NULL;	}	fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);	if (bitmapImage == NULL)	{		fclose(filePtr);		return NULL;	}	for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)	{		tempRGB = bitmapImage[imageIdx];		bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];		bitmapImage[imageIdx + 2] = tempRGB;	}		fclose(filePtr);	return bitmapImage;}void Initialize(){	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	glShadeModel(GL_SMOOTH);	glEnable(GL_DEPTH_TEST);					glEnable(GL_CULL_FACE);						glFrontFace(GL_CCW);					        glEnable(GL_TEXTURE_2D);					bitmapData = LoadBitmapFile("checker.bmp", &bitmapInfoHeader);	glGenTextures(1, &texture);	glBindTexture(GL_TEXTURE_2D, texture);        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,				 bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);	}void Render(){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		glLoadIdentity();	glTranslatef(0.0f, 0.0f, -3.0f);	glRotatef(angle, 1.0f, 0.0f, 0.0f);	glRotatef(angle, 0.0f, 1.0f, 0.0f);	glRotatef(angle, 0.0f, 0.0f, 1.0f);	DrawTextureCube(0.0f, 0.0f, 0.0f);	if (angle >= 360.0f)		angle = 0.0f;	angle+=0.2f;    	glFlush();	SwapBuffers(g_HDC);}


When using glGetError() it detected no errors, but I thought it would give me a GL_INVALID_ENUM or somethig similar if it didn't like the pointer or bitmap itself.

Thanks again for any help.
C-Junkie
C-Junkie
Common opengl screw up #23948742: TExture must be powers of 2 in dimensions (eg: 128x1228, 16x16, 1024x1024, etc)

Havent looked at the code, jsut guessing.
hplus0603
hplus0603
@C-Junkie: The very first post said that:
Quote:

The bitmap itself has dimensions of a power of 2


To find this problem, I would step through the program in the debugger, making sure that I get the data I expect at each point. I would also add calls to GetLastError() after calling Windows functions, and assert(!glGetError()) after calls to GL.

Also, if the filtering mode is set to MIP mapping, but you haven't loaded all the MIP map levels, then you'll get white texels, because the texture image set is "incomplete" (according to GL spec).
enum Bool { True, False, FileNotFound };
Ademan555
Ademan555
Well, im assuming your at least following the model of OpenGL Game Programming (one of lamothe's books) and anyways, i followed that model as well, and recieved hell... My function could load any bitmap saved in paint, but it COULD NOT for the life of it... load a paintshop saved one... i looked into it, it was because paintshop neglects to fill in the biSizeImage parameter (oh and also look out for struct padding issues, BITMAPFILEHEADER IS NOT a multiple of 4 bytes, so you cant read it directly, you need to read in individual parts) so anyways, i ended up changing my code to use the width and height information along with the bits per pixel (divided by 8) to get the number of bytes i needed to allocate. Of course, photoshop images STILL hafta be different, because now they appear blue-er thany they should, i tried not-switching the b and r bytes, and it hasnt worked... im wondering if its palletted or something.... anways... hoped that helped
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Eschelon
Eschelon
In my opinion, bitmap loaders aren't worth all the effort. They may be quick and easy (especially if you can use GLaux) but they lack certain benefits such as the inability to use RGBA. My advice is instead of spending hours over a bitmap loader, which I doubt will be used for much longer in the future of gaming, get a TGA (targa) loader. TGA loaders can use RGBA or RGB, don't rely on GLaux, have many of the same commands as BMP loaders and will fit nicely in a seperate header file to boot. If you download a program called Irfanview you can easily convert any BMP into TGA in seconds. If you can't find a TGA loader I have a great one you can use, all you need to do is include a header file and add a few lines of code to your program.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.