Al's Programming Resource Homepage  Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

texture.cpp

Go to the documentation of this file.
00001 #include <windows.h>
00002 #include "texture.h"
00003 #include "mmgr.h"
00004 
00005 
00006 TEXTURE::TEXTURE()
00007 {
00008 }
00009 
00010 TEXTURE::~TEXTURE()
00011 {
00012 }
00013 
00014 bool TEXTURE::LoadTGA()
00015 {
00016     return this->LoadTGA(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, 1);
00017 }
00018 
00019 bool TEXTURE::LoadTGA(GLenum MagFilter, GLenum MinFilter, GLenum WrapS, GLenum WrapT, bool MipMap)
00020 {
00021     GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};                        // Uncompressed TGA Header
00022     GLubyte TGAcompare[12];                                                 // Used To Compare TGA Header
00023     GLubyte header[6];                                                      // First 6 Useful Bytes From The Header
00024     GLuint  bytesPerPixel;                                                  // Holds Number Of Bytes Per Pixel Used In The TGA File
00025     GLuint  imageSize;                                                      // Used To Store The Image Size When Setting Aside Ram
00026     GLuint  temp;                                                           // Temporary Variable
00027     GLuint  type=GL_RGBA;                                                   // Set The Default GL Mode To RBGA (32 BPP)
00028     FILE *file = fopen(TexName, "rb");                                      // Open The TGA Fi
00029     if( file==NULL ||                                                       // Does File Even Exist?
00030     fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||      // Are There 12 Bytes To Read?
00031     memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0               ||      // Does The Header Match What We Want?
00032     fread(header,1,sizeof(header),file)!=sizeof(header))                    // If So Read Next 6 Header Bytes
00033     {
00034         if (file == NULL)                                                   // Did The File Even Exist? *Added Jim Strong*
00035         {
00036             MessageBox(NULL,"Image file was not found","Error",MB_OK|MB_ICONERROR);
00037             return false;                                                   // Return False
00038         }
00039             if (TGAcompare[2] == 1)
00040                 MessageBox(NULL,"Image cannot be indexed color. \r\n Convert the image to RGB or RGBA.","Error",MB_OK|MB_ICONERROR);
00041             if (TGAcompare[2] == 3)
00042                 MessageBox(NULL,"Image cannot be greyscale color. \r\n Convert the image to RGB or RGBA.","Error",MB_OK|MB_ICONERROR);
00043             if (TGAcompare[2] == 9 || TGAcompare[2] == 10)
00044                 MessageBox(NULL,"Image cannot be compressed. \r\n Convert the image to an uncompressed format.","Error",MB_OK|MB_ICONERROR);
00045             fclose(file);                                                   // If Anything Failed, Close The File
00046             return false;                                                   // Return False
00047         }
00048         Width  = header[1] * 256 + header[0];                      // Determine The TGA Width      (highbyte*256+lowbyte)
00049         Height = header[3] * 256 + header[2];                      // Determine The TGA Height     (highbyte*256+lowbyte)
00050         if ( Width  <=0     ||                                      // Is The Width Less Than Or Equal To Zero
00051         Height <=0     ||                                          // Is The Height Less Than Or Equal To Zero
00052         (header[4]!=24 && header[4]!=32))                                   // Is The TGA 24 or 32 Bit?
00053         {
00054             fclose(file);                                                   // If Anything Failed, Close The File
00055             if (Width  <=0 || Height  <=0)
00056                 MessageBox(NULL,"Image must have a width and height greater than 0","Error",MB_OK|MB_ICONERROR);
00057             if (header[4]!=24 && header[4]!=32)
00058                 MessageBox(NULL,"Image must be 24 or 32 bit","Error",MB_OK|MB_ICONERROR);
00059             return false;                                               // Return False
00060         }
00061         Bpp    = header[4];                                        // Grab The TGA's Bits Per Pixel (24 or 32)
00062         bytesPerPixel   = Bpp/8;                                   // Divide By 8 To Get The Bytes Per Pixel
00063         imageSize       = Width*Height*bytesPerPixel;     // Calculate The Memory Required For The TGA Data
00064         ImageData = (GLubyte *)malloc(imageSize);                    // Reserve Memory To Hold The TGA Data
00065         if (ImageData == NULL ||                                     // Does The Storage Memory Exist?
00066         fread(ImageData, 1, imageSize, file)!=imageSize)           // Does The Image Size Match The Memory Reserved?
00067         {
00068             if (ImageData!=NULL)                                    // Was Image Data Loaded
00069                 free(ImageData);                                   // If So, Release The Image Data
00070             MessageBox(NULL,"Image load failed for unknown reason.","Error",MB_OK|MB_ICONERROR);
00071             fclose(file);                                                   // Close The File
00072             return false;                                                   // Return False
00073         }
00074         for (GLuint i=0; i<(GLuint)imageSize; i+=bytesPerPixel)                 // Loop Through The Image Data
00075         {                                                                   // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
00076             temp = ImageData[i];                                     // Temporarily Store The Value At Image Data 'i'
00077             ImageData[i] = ImageData[i + 2];                       // Set The 1st Byte To The Value Of The 3rd Byte
00078             ImageData[i + 2] = (GLubyte)temp;                               // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
00079         }
00080         fclose(file);                                                      // Close The File
00081 
00082         // Build A Texture From The Data
00083         glGenTextures(1, &TexID);                                // Generate OpenGL texture IDs
00084         glBindTexture(GL_TEXTURE_2D, TexID);
00085         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00086         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00087         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter);
00088         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter);
00089         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, WrapS);
00090         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, WrapT);
00091 
00092         if (Bpp==24)                                             // Was The TGA 24 Bits
00093         {
00094             type=GL_RGB;                                                    // If So Set The 'type' To GL_RGB
00095         }
00096         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00097         if (MipMap)
00098             gluBuild2DMipmaps(GL_TEXTURE_2D, type, Width, Height, type, GL_UNSIGNED_BYTE, ImageData);
00099         else
00100             glTexImage2D(GL_TEXTURE_2D, 0, type, Width, Height, 0, type, GL_UNSIGNED_BYTE, ImageData);
00101         free(ImageData);
00102         return true;                                                        // Texture Building Went Ok, Return True
00103 }

Generated on Fri Dec 23 05:21:39 2005 for Sound by doxygen1.2.15