|
发表于 2003-11-1 12:59:00
|
显示全部楼层
Re: 关于opengl里面bmp装载怎么做,谢谢,大师指点!!!
bool CTexture: oadBMP(char *Filename) // Load Bitmaps And Convert To Textures
{
AUX_RGBImageRec *pImage = NULL;
FILE *pFile = NULL;
if(!Filename)
return false;
// Open a file pointer to the BMP file and check if it was found and opened
if((pFile = fopen(Filename, "rb")) == NULL)
{
// Display an error message saying the file was not found, then return NULL
char buf[255];
sprintf(buf,"Unable to load BMP File:%s",Filename);
MessageBox(NULL, buf, "Error", MB_OK);
ExitProcess(0);
return false;
}
// Load the bitmap using the aux function stored in glaux.lib
pImage = auxDIBImageLoad(Filename);
// Make sure valid image data was given to pImage, otherwise return false
if(pImage == NULL)
return false;
// Generate a texture with the associative texture ID stored in the array
glGenTextures(1, &m_nTxt);
// This sets the alignment requirements for the start of each pixel row in memory.
// glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// Bind the texture to the texture arrays index and init the texture
glBindTexture(GL_TEXTURE_2D, m_nTxt);
// Build Mipmaps (builds different versions of the picture for distances - looks better)
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX,
pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
//Assign the mip map levels and texture info
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Now we need to free the image data that we loaded since openGL stored it as a texture
if (pImage) // If we loaded the image
{
if (pImage->data) // If there is texture data
{
free(pImage->data); // Free the texture data, we don't need it anymore
// delete []pImage->data;
}
// delete pImage;
free(pImage); // Free the image structure
}
return true;
} |
|