Jump to content
  • Advertisement
Sign in to follow this  
anist

BitmapToTexture function

This topic is 4631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

here's my function for converting a .bmp to a texture function. submitted not only for your approval, but your improvement. known issues: i eliminated glaux, but now it's GDI dependent. ideally it would have no OS dependencies. support for non powers of 2. ideally it would resize if the driver did not support non powers of two.
#include <windows.h>

GLuint BitmapToTexture(char* filename, COLORREF key/*= RGB(255, 0, 255)*/, unsigned char alpha/*=255*/, unsigned char key_alpha/*=0*/)
{
	GLuint texture = 0;							//the return value				
	BITMAP bmp;									//used only to get bmp dimensions
	HDC hDC = CreateCompatibleDC(NULL);			//choose DC like the desktop
	HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,	//load the bitmap
										 filename,
										 IMAGE_BITMAP,
										 0, 0, 
										 LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);

	if(hBitmap == NULL)							//bitmap failed to load
	{
		return 0;
	}
	//get bitmap dimensions
	GetObject(hBitmap, sizeof(BITMAP), &bmp);

	//allocate memory for the bitmap
	COLORREF *data = new COLORREF[bmp.bmWidth * bmp.bmHeight];

	//put it into our device context for GetPixel calls
	SelectObject(hDC, hBitmap);
	for(int y = 0; y < bmp.bmHeight; y++)
	{
		for(int x = 0; x < bmp.bmWidth; x++)
		{
			data[x + (y*bmp.bmHeight)] = GetPixel(hDC, x, y);
			if(key != data[x + (y*bmp.bmHeight)])
			{
				data[x + (y*bmp.bmHeight)] |= (alpha << 24);
			}
			else
			{
				data[x + (y*bmp.bmHeight)] |= (key_alpha << 24);
			}


		}
	}
	
	//get rid of some GDI stuff we used to convert to RGB buffer
	DeleteDC(hDC);
	DeleteObject(hBitmap);
		
	//finally generate a texture
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, bmp.bmWidth, bmp.bmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	delete data;
	return texture;
}



Share this post


Link to post
Share on other sites
Advertisement
Hi

I implemented a simple BMP loader (only grayscale or RGB and no compression) by my self only using fopen(), fread() and fclose(). If you are intrested I can send it to you but it depends on a little bit of declarations within my environment so you cant use it out of the box.

But why not using some other file format? I am always using PNG now. To load it I use the excellent libpng which is cross platform and virtually everywhere available.


mod42

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!