利用免费的CxImage类库,读取多种图像作为纹理贴图

         很久以来(1年?)一直使用自己修改的一个纹理读取类CTexture。它可以读取bmp,tga,rgb,rgba的图像格式,并且可以实现操作Alpha通道。核心的代码大多是来自Nehe的教程,还有一些我都不记得的来源吧。感觉还是挺好用的,毕竟是自己修改的,用起来就是一个“顺”字:)

        几天前,本来是找FreeImage库的,不小心发现了免费的cxImage类库。查阅了一些资料,终于自己改写了一些代码,把需要的头文件集中在了ximage.h中,并编写了CLoadImage类,利用CxImage的功能可以顺利读取多种图像文件啦!!并且效率应该比我那个CTexture类高。

       CLoadImage的核心代码:

//载入多种图像
bool CLoadImage::LoadImage(const char *tex_name, const char *alpha_name)
{

CxImage image ,alpha,blendTex;//
unsigned char *pImage_RGBA = NULL;
image.Load(tex_name);
if(!image.IsValid())
   return false;

int sizeX,sizeY;
sizeX = image.GetWidth();
sizeY = image.GetHeight();
float texAspectRatio = (float)sizeX / (float)sizeY;
if(alpha_name && strlen(alpha_name) > 0 )
{
   int imageSize_RGB   = sizeX * sizeY * 3;
   int imageSize_RGBA = sizeX * sizeY * 4;
   alpha.Load(alpha_name);
   if(!alpha.IsValid())
   {
    return false;
   }
   // allocate buffer for a RGBA image
   pImage_RGBA = new unsigned char[imageSize_RGBA];
   RGBQUAD col_image,col_alpha;
   for(int y=0;y<sizeY;y++)
    for(int x=0;x<sizeX;x++)
     {
      col_image = image.GetPixelColor(x,y,false);
      col_alpha = alpha.GetPixelColor(x,y,false);
      pImage_RGBA[(x+y*sizeX)*4 +0] = col_image.rgbRed;
      pImage_RGBA[(x+y*sizeX)*4 +1] = col_image.rgbGreen;
      pImage_RGBA[(x+y*sizeX)*4 +2] = col_image.rgbBlue;
      pImage_RGBA[(x+y*sizeX)*4 +3] = col_alpha.rgbRed;
     }

   glGenTextures(1, &m_ID);
   glBindTexture(GL_TEXTURE_2D, m_ID);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, sizeX,
                   sizeY, GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA);
   if(pImage_RGBA)
   {
    delete [] pImage_RGBA;
   }

}
else if(image.AlphaIsValid())
{
   int imageSize_RGB   = sizeX * sizeY * 3;
   long imageSize_RGBA = sizeX * sizeY * 4;
   image.Encode2RGBA(pImage_RGBA,imageSize_RGBA);
   // Generate a texture with the associative texture ID stored in the array
   glGenTextures(1, &m_ID);
   // Bind the texture to the texture arrays index and init the texture
   glBindTexture(GL_TEXTURE_2D, m_ID);
   //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);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   // Build Mipmaps (builds different versions of the picture for distances - looks better)
   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, sizeX,
               sizeY, GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA);
   image.FreeMemory( pImage_RGBA);
}
else
{
   // Generate a texture with the associative texture ID stored in the array
   glGenTextures(1, &m_ID);
   glBindTexture(GL_TEXTURE_2D, m_ID);
   //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);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   // Build Mipmaps (builds different versions of the picture for distances - looks better)
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sizeX,
                  sizeY, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.GetBits());
}
return true;
}


你可能感兴趣的:(image)