BMP.cpp
上传用户:yunyi28
上传日期:2021-04-25
资源大小:6978k
文件大小:4k
源码类别:

分形几何

开发平台:

Visual C++

  1. #include "BMP.h"
  2. BMP::BMP(void)
  3. {
  4. }
  5. BMP::~BMP(void)
  6. {
  7. }
  8. AUX_RGBImageRec *BMP::LoadBMP(char* Filename)
  9. {
  10. FILE *File=NULL; // File Handle
  11. if (!Filename) // Make Sure A Filename Was Given
  12. {
  13. return NULL; // If Not Return NULL
  14. }
  15. File=fopen(Filename,"r"); // Check To See If The File Exists
  16. if (File) // Does The File Exist?
  17. {
  18. fclose(File); // Close The Handle
  19. return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
  20. }
  21. return NULL; // If Load Failed Return NULL
  22. }
  23. // Load Bitmaps And Convert To Textures
  24. int BMP::LoadBMPTexture(GLuint *texture,char *Filename)
  25. {
  26.         int Status=FALSE;                               // Status Indicator
  27.         AUX_RGBImageRec *TextureImage[1];               // Create Storage Space For The Texture
  28.         memset(TextureImage,0,sizeof(void *)*1);        // Set The Pointer To NULL
  29.         // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
  30.         if (TextureImage[0]=LoadBMP(Filename))
  31.         {
  32.                 Status=TRUE;                            // Set The Status To TRUE
  33.  //               glGenTextures(1, &texture[0]);          // Create One Texture
  34.                 // Create Linear Filtered Texture
  35.                 glBindTexture(GL_TEXTURE_2D, texture[0]);
  36.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  37.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  38.                 glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
  39.         }
  40.         if (TextureImage[0])                            // If Texture Exists
  41.         {
  42.                 if (TextureImage[0]->data)              // If Texture Image Exists
  43.                 {
  44.                         free(TextureImage[0]->data);    // Free The Texture Image Memory
  45.                 }
  46.                free(TextureImage[0]);                  // Free The Image Structure
  47.         }
  48.         return Status;                                  // Return The Status
  49. }
  50. bool BMP::MakeAlphaTextureBind(char *TextureFileName, unsigned int *TextureID)
  51. {
  52. bool status=true; // Status Indicator
  53. AUX_RGBImageRec *Image=NULL; // Create Storage Space For The Texture
  54. unsigned char *alpha=NULL;
  55. ////////////////////////////////////
  56. if (Image=LoadBMP(TextureFileName)) 
  57. {
  58. alpha=new unsigned char[4*Image->sizeX*Image->sizeY]; // Create Memory For RGBA8-Texture
  59. for (int a=0; a<Image->sizeX*Image->sizeY; a++)
  60. {
  61. alpha[4*a]=Image->data[a*3]; // R
  62. alpha[4*a+1]=Image->data[a*3+1]; // G
  63. alpha[4*a+2]=Image->data[a*3+2]; // B
  64.             ////////////Get max
  65.             if(alpha[4*a+0]+alpha[4*a+1]+alpha[4*a+2] >0)
  66. alpha[4*a+3] = 255;
  67. else alpha[4*a+3] = 0;
  68. }
  69. ///////////////////////////////////////////////
  70. glGenTextures(1, TextureID); // Create One Textures
  71. // Create Linear Filtered RGBA8-Texture
  72. glBindTexture(GL_TEXTURE_2D, *TextureID);
  73.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  74.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
  75.     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, Image->sizeX, Image->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, alpha);
  76. delete [] alpha;
  77. }
  78. else status=false;
  79. if (Image) { // If Texture Exists
  80. if (Image->data) delete Image->data; // If Texture Image Exists
  81. delete Image;
  82. Image=NULL;
  83. }
  84. ///////////////////////////////////
  85.     return status;
  86. }