BMP.cpp
资源名称:XLT.rar [点击查看]
上传用户:yunyi28
上传日期:2021-04-25
资源大小:6978k
文件大小:4k
源码类别:
分形几何
开发平台:
Visual C++
- #include "BMP.h"
- BMP::BMP(void)
- {
- }
- BMP::~BMP(void)
- {
- }
- AUX_RGBImageRec *BMP::LoadBMP(char* Filename)
- {
- FILE *File=NULL; // File Handle
- if (!Filename) // Make Sure A Filename Was Given
- {
- return NULL; // If Not Return NULL
- }
- File=fopen(Filename,"r"); // Check To See If The File Exists
- if (File) // Does The File Exist?
- {
- fclose(File); // Close The Handle
- return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
- }
- return NULL; // If Load Failed Return NULL
- }
- // Load Bitmaps And Convert To Textures
- int BMP::LoadBMPTexture(GLuint *texture,char *Filename)
- {
- int Status=FALSE; // Status Indicator
- AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
- memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
- // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
- if (TextureImage[0]=LoadBMP(Filename))
- {
- Status=TRUE; // Set The Status To TRUE
- // glGenTextures(1, &texture[0]); // Create One Texture
- // Create Linear Filtered Texture
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
- }
- if (TextureImage[0]) // If Texture Exists
- {
- if (TextureImage[0]->data) // If Texture Image Exists
- {
- free(TextureImage[0]->data); // Free The Texture Image Memory
- }
- free(TextureImage[0]); // Free The Image Structure
- }
- return Status; // Return The Status
- }
- bool BMP::MakeAlphaTextureBind(char *TextureFileName, unsigned int *TextureID)
- {
- bool status=true; // Status Indicator
- AUX_RGBImageRec *Image=NULL; // Create Storage Space For The Texture
- unsigned char *alpha=NULL;
- ////////////////////////////////////
- if (Image=LoadBMP(TextureFileName))
- {
- alpha=new unsigned char[4*Image->sizeX*Image->sizeY]; // Create Memory For RGBA8-Texture
- for (int a=0; a<Image->sizeX*Image->sizeY; a++)
- {
- alpha[4*a]=Image->data[a*3]; // R
- alpha[4*a+1]=Image->data[a*3+1]; // G
- alpha[4*a+2]=Image->data[a*3+2]; // B
- ////////////Get max
- if(alpha[4*a+0]+alpha[4*a+1]+alpha[4*a+2] >0)
- alpha[4*a+3] = 255;
- else alpha[4*a+3] = 0;
- }
- ///////////////////////////////////////////////
- glGenTextures(1, TextureID); // Create One Textures
- // Create Linear Filtered RGBA8-Texture
- glBindTexture(GL_TEXTURE_2D, *TextureID);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, Image->sizeX, Image->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, alpha);
- delete [] alpha;
- }
- else status=false;
- if (Image) { // If Texture Exists
- if (Image->data) delete Image->data; // If Texture Image Exists
- delete Image;
- Image=NULL;
- }
- ///////////////////////////////////
- return status;
- }