CxImage与OpenGL结合

来源:互联网 发布:windows动态桌面 编辑:程序博客网 时间:2024/06/05 16:12

From:http://www.physdev.com/phpbb/cms_view_article.php?aid=30
关于CxImage的文章,网上有许多,这里只介绍如何把CxImage与OpenGL结合起来,用于读入多种格式的纹理以及用来把屏幕保存为各种格式的图像文件。 

支持的格式有:BMP,GIF,ICO,JP2,FPC,FPG,PCX,PNG,PNM,RAS,TGA,TIF等等。 

支持读入透明纹理。 

CxImage官方网站:
http://www.xdp.it/ 

[cpp] view plain copy
  1. //使用CxImage来为OpenGL读入多种格式的纹理   
  2. //CxImage是一个开源的图片处理函数库,支持的文件格式有:   
  3. //CXIMAGE_FORMAT_BMP   
  4. //CXIMAGE_FORMAT_GIF   
  5. //CXIMAGE_FORMAT_ICO   
  6. //CXIMAGE_FORMAT_JP2   
  7. //CXIMAGE_FORMAT_JPC   
  8. //CXIMAGE_FORMAT_JPG   
  9. //CXIMAGE_FORMAT_PCX   
  10. //CXIMAGE_FORMAT_PGX   
  11. //CXIMAGE_FORMAT_PNG   
  12. //CXIMAGE_FORMAT_PNM   
  13. //CXIMAGE_FORMAT_RAS   
  14. //CXIMAGE_FORMAT_TGA   
  15. //CXIMAGE_FORMAT_TIF   
  16. //CXIMAGE_FORMAT_UNKNOWN   
  17. //CXIMAGE_FORMAT_WBMP   
  18. //CXIMAGE_FORMAT_WMF   
  19. //们知道OpenGL自带有读取图形文件作纹理的函数,但功能很弱,只支持BMP图片   
  20. //如果要读取其它格式的纹理,就需要用到第三方函数库了。这里我们介绍CxImage   
  21. //CxImage下载:www.xdp.it   
  22. //以下代码是用来读取JPG文件的,紧供参考。   
  23.   
  24. //读入纹理,支持读入一个alpha纹理,alpha纹理的大小必须与原图一至。   
  25. //LoadTexture("pic.jpg",NULL,resultID); //读入单个JPG图片作纹理   
  26. //LoadTexture("pic.jpg","pic_alpha.jpg",resultID); //读入一个纹理图,及一个用于透明过滤的alpha图   
  27. //LoadTexture("pic.png",NULL,resultID) //读入一个自身带有透明信息的图片作纹理。   
  28.   
  29. bool CCxImage_GLView::LoadTexture(const char *tex_name, const char *alpha_name, unsigned int &texID)   
  30. {   
  31.    // TODO: Add your command handler code here   
  32.    CxImage image ,alpha,blendTex;//   
  33.   
  34.    unsigned char *pImage_RGBA = NULL;   
  35.   
  36.    // Load the bitmap using the aux function stored in glaux.lib   
  37.    //pImage = auxDIBImageLoad(tex_name);   
  38.    image.Load(tex_name);   
  39.    // Make sure valid image data was given to pImage, otherwise return false   
  40.    if(!image.IsValid())   
  41.       return false;   
  42.   
  43.    int sizeX,sizeY;   
  44.    sizeX = image.GetWidth();   
  45.    sizeY = image.GetHeight();   
  46.    float texAspectRatio = (float)sizeX / (float)sizeY;   
  47.    if(alpha_name && strlen(alpha_name) > 0 )   
  48.    {   
  49.          int imageSize_RGB  = sizeX * sizeY * 3;   
  50.          int imageSize_RGBA = sizeX * sizeY * 4;   
  51.          alpha.Load(alpha_name);      
  52.          if(!alpha.IsValid())   
  53.          {   
  54.             return false;   
  55.          }   
  56.          // allocate buffer for a RGBA image   
  57.          pImage_RGBA = new unsigned char[imageSize_RGBA];   
  58.          
  59.          RGBQUAD col_image,col_alpha;   
  60.             
  61.          for(int y=0;y<sizeY;y++)   
  62.          {for(int x=0;x<sizeX;x++)   
  63.             {   
  64.                col_image = image.GetPixelColor(x,y,false);   
  65.                col_alpha = alpha.GetPixelColor(x,y,false);   
  66.                pImage_RGBA[(x+y*sizeX)*4 +0] = col_image.rgbRed;   
  67.                pImage_RGBA[(x+y*sizeX)*4 +1] = col_image.rgbGreen;   
  68.                pImage_RGBA[(x+y*sizeX)*4 +2] = col_image.rgbBlue;   
  69.                pImage_RGBA[(x+y*sizeX)*4 +3] = col_alpha.rgbRed;   
  70.             }   
  71.          }  
  72.   
  73.          glGenTextures(1, &texID);   
  74.          glBindTexture(GL_TEXTURE_2D, texID);   
  75.   
  76.          glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);   
  77.          glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);   
  78.          glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);      
  79.          // Don't forget to use GL_RGBA for our new image data we support Alpha transparency now!   
  80.       // Build Mipmaps (builds different versions of the picture for distances - looks better)   
  81.          gluBuild2DMipmaps(GL_TEXTURE_2D, 4, sizeX,   
  82.             sizeY, GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA);      
  83.       if(pImage_RGBA)   
  84.       {   
  85.          delete [] pImage_RGBA;   
  86.       }   
  87.    }   
  88.    else if(image.AlphaIsValid())   
  89.    {   
  90.          int imageSize_RGB  = sizeX * sizeY * 3;   
  91.          long imageSize_RGBA = sizeX * sizeY * 4;   
  92.          // allocate buffer for a RGBA image   
  93.       //   pImage_RGBA = new unsigned char[imageSize_RGBA];   
  94.   
  95.       image.Encode2RGBA(pImage_RGBA,imageSize_RGBA);   
  96.          
  97.       // Generate a texture with the associative texture ID stored in the array   
  98.       glGenTextures(1, &texID);   
  99.          
  100.       // This sets the alignment requirements for the start of each pixel row in memory.   
  101.       //   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);   
  102.          
  103.       // Bind the texture to the texture arrays index and init the texture   
  104.       glBindTexture(GL_TEXTURE_2D, texID);   
  105.          
  106.       //Assign the mip map levels and texture info   
  107.       //   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);   
  108.       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);   
  109.       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);   
  110.       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);   
  111.       // Build Mipmaps (builds different versions of the picture for distances - looks better)   
  112.       gluBuild2DMipmaps(GL_TEXTURE_2D, 4, sizeX, sizeY, GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA);      
  113.          
  114.       image.FreeMemory( pImage_RGBA);   
  115.    }   
  116.    else   
  117.    {   
  118.       // Generate a texture with the associative texture ID stored in the array   
  119.       glGenTextures(1, &texID);   
  120.          
  121.       // This sets the alignment requirements for the start of each pixel row in memory.   
  122.       //   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);   
  123.          
  124.       // Bind the texture to the texture arrays index and init the texture   
  125.       glBindTexture(GL_TEXTURE_2D, texID);   
  126.   
  127.       //Assign the mip map levels and texture info   
  128.       //   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);   
  129.       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);   
  130.       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);   
  131.       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);   
  132.       // Build Mipmaps (builds different versions of the picture for distances - looks better)   
  133.       gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sizeX,   
  134.          sizeY, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.GetBits());   
  135.    }   
  136.    //glEnable(GL_TEXTURE_2D);   
  137.    // Now we need to free the image data that we loaded since openGL stored it as a texture   
  138.   
  139.    return true;   
  140. }   


[cpp] view plain copy
  1. //用来保存屏幕到图像文件。  
  2. void CCxImage_GLView::OnSaveScene()   
  3. {   
  4.    // TODO: Add your command handler code here   
  5.    static char BASED_CODE szFilter[] = "jpg Files (*.jpg)|*.jpg|bmp Files (*.bmp)|*.bmp|tga Files (*.tga)|*.tga|All Files (*.*)|*.*||";   
  6.    CString filename;   
  7.       
  8.    CString ext = "";   
  9.   
  10.    if(filename.IsEmpty())   
  11.       filename = "NoName";   
  12.   
  13.    CFileDialog dlg(false"jpg",filename, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);   
  14.   
  15.    if(dlg.DoModal() == IDOK)   
  16.    {   
  17.       ext = dlg.GetFileExt();   
  18.       filename = dlg.GetPathName();   
  19.   
  20.       //CDC* m_pDC;                  // Windows设备描述表   
  21.        //HGLRC m_hRC;               // OpenGL渲染描述表   
  22.   
  23.        // TODO: Add your command handler code here   
  24.       wglMakeCurrent(m_pDC->m_hDC,m_hRC);   
  25.       //这里要注意,如果就面渲染完毕的时候,调用了wglMakeCurrent(NULL,NULL);上面一行就一定要加上。   
  26.   
  27.        int expand = 0;   
  28.       if((m_width *3)%4)   
  29.          expand = 4 - (m_width*3)%4;   //保证位图宽度能被4整除   
  30.          
  31.        int mapSize = (m_width*3 +expand) * (m_height);   
  32.   
  33.       if(mapSize == 0)   
  34.          return;   
  35.   
  36.       //hDIB = (HGLOBAL) ::GlobalAlloc(GHND,mapSize);   
  37.       unsigned char * pTmp = new BYTE[mapSize];   
  38.   
  39.       if(!pTmp)   
  40.          return ;   
  41.       // 读取屏幕像素   
  42.        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);   
  43.       glReadPixels(0, 0, m_width, m_height, GL_BGR_EXT, GL_UNSIGNED_BYTE, pTmp);   
  44.       // glPixelStorei(GL_PACK_ALIGNMENT, 1);   
  45.   
  46.       // BMP信息头   
  47.        CxImage image;   
  48.       //image.CreateFromHBITMAP(hbit);   
  49.       image.CreateFromArray(pTmp,m_width,m_height,24,m_width*3 + expand,false);   
  50.       image.SetJpegQuality(98);      //指定JPG文件的质量(0-100)   
  51.          
  52.        if(ext == "jpb")   
  53.          image.Save(filename,CXIMAGE_FORMAT_JPG);   
  54.       else if(ext == "bmp")   
  55.          image.Save(filename,CXIMAGE_FORMAT_BMP);   
  56.       else if(ext == "tga")   
  57.          image.Save(filename,CXIMAGE_FORMAT_TGA);   
  58.   
  59.       //pFile->Write(pTmp,mapSize*3);   
  60.   
  61.      delete[] pTmp;   
  62.    }   
  63. }  


http://www.cppblog.com/tx7do/archive/2007/04/09/21554.html

0 0