PDA

View Full Version : load a bmp as a texture in c and not using glaux



cornsalt
05-25-2002, 05:32 AM
hi
im sorry to be annoying but how do i load a bmp as a texture to a 2d object.
I've seen the version on NeHe but i dont have and dont want to have glaux installed. Im using gcc on win98.
Please help me.

pifaf
05-25-2002, 05:44 AM
Hi.Try with this function, it's not mine, i saw it in a web page but i can't remember its autor.
I use it in a personal program i'm coding.
Sorry if there are spanish words, it's my language.


BOOL LoadBMP(TCHAR* szFileName,int indice)
{
HDC hDC = ::GetDC(*this);
wglMakeCurrent(hDC, m_hGLContext);

HANDLE hFileHandle;
BITMAPINFO *pBitmapInfo = NULL;
unsigned long lInfoSize = 0;
unsigned long lBitSize = 0;
int nTextureWidth;
int nTextureHeight;
BYTE *pBitmapData;


// Open the Bitmap file
hFileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ ,
NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL) ;

// Check for open failure (most likely file does not exist).
if(hFileHandle == INVALID_HANDLE_VALUE)
return FALSE;

// File is Open. Read in bitmap header information
BITMAPFILEHEADER bitmapHeader;
DWORD dwBytes;
ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFILEHEADER),
&dwBytes,NULL);

__try {
if(dwBytes != sizeof(BITMAPFILEHEADER))
return FALSE;

// Check format of bitmap file
if(bitmapHeader.bfType != 'MB')
return FALSE;

// Read in bitmap information structure
lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
pBitmapInfo = (BITMAPINFO *) new BYTE[lInfoSize];

ReadFile(hFileHandle,pBitmapInfo,lInfoSize,&dwBytes,NULL);

if(dwBytes != lInfoSize)
return FALSE;


nTextureWidth = pBitmapInfo->bmiHeader.biWidth;
nTextureHeight = pBitmapInfo->bmiHeader.biHeight;

if(nTextureWidth % 8 != 0 | | nTextureHeight % 8 != 0 | | nTextureWidth != nTextureHeight)
return FALSE;

if(pBitmapInfo->bmiHeader.biBitCount!=24)
return FALSE;

lBitSize = pBitmapInfo->bmiHeader.biSizeImage;



if(lBitSize == 0)
lBitSize = (nTextureWidth *
pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *
abs(nTextureHeight);

// Allocate space for the actual bitmap
pBitmapData = new BYTE[lBitSize];

// Read in the bitmap bits
ReadFile(hFileHandle,pBitmapData,lBitSize,&dwBytes,NULL);

if(lBitSize != dwBytes)
{
if(pBitmapData)
delete [] (BYTE *) pBitmapData;
pBitmapData = NULL;

return FALSE;
}
}
__finally // Fail or success, close file and free working memory
{
CloseHandle(hFileHandle);

if(pBitmapInfo != NULL)
delete [] (BYTE *)pBitmapInfo;
}


// This is specific to the binary format of the data read in.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);

glBindTexture(GL_TEXTURE_2D, nombre_texturas[indice]);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, nTextureWidth, nTextureHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pBitmapData);


if(pBitmapData)
delete [] (BYTE *) pBitmapData;

return TRUE;
}

nexusone
05-25-2002, 07:43 PM
You have another option also, instead of using BMP format use TGA format.
nehe has code for loading it( lesson 33), and it does not require glaux.



Originally posted by cornsalt:
hi
im sorry to be annoying but how do i load a bmp as a texture to a 2d object.
I've seen the version on NeHe but i dont have and dont want to have glaux installed. Im using gcc on win98.
Please help me.

cornsalt
05-25-2002, 08:46 PM
thanks ill try out both methods http://www.opengl.org/discussion_boards/ubb/biggrin.gif

CopyCat
05-26-2002, 01:38 AM
The firs method isn't very portable.
WIN32 api...I would suggets reading the file specs.
BMP loading is quite ease.
Look at the code of OpenIL or somthing.

jmathies
05-26-2002, 10:08 AM
Here's some code that does it, all free.
http://www.mathies.com/cpw/srcindex.html

Look at cpw_images, cpw_bitmaps and cpw_libtarga.

Regards,
Jim

dabeav
05-26-2002, 03:29 PM
One word, OpenIL. or devil. www.imagelib.org (http://www.imagelib.org)
its opengl syntax, very easy, very portable and takes all the pain out of loading image files.

rpxmaster
05-28-2002, 08:24 PM
Try using DevIL. It's fun.
http://www.imagelib.org