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

Generated on Fri Dec 23 05:22:20 2005 for Template by doxygen1.2.15