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

Generated on Fri Dec 23 05:20:18 2005 for Portals by doxygen1.2.15