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};
00016 GLubyte TGAcompare[12];
00017 GLubyte header[6];
00018 GLuint bytesPerPixel;
00019 GLuint imageSize;
00020 GLuint temp;
00021 GLuint type=GL_RGBA;
00022 FILE *file = fopen(TexName, "rb");
00023 if( file==NULL ||
00024 fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||
00025 memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 ||
00026 fread(header,1,sizeof(header),file)!=sizeof(header))
00027 {
00028 if (file == NULL)
00029 {
00030 MessageBox(NULL,"Image file was not found","Error",MB_OK|MB_ICONERROR);
00031 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);
00040 return false;
00041 }
00042 Width = header[1] * 256 + header[0];
00043 Height = header[3] * 256 + header[2];
00044 if( Width <=0 ||
00045 Height <=0 ||
00046 (header[4]!=24 && header[4]!=32))
00047 {
00048 fclose(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;
00054 }
00055 Bpp = header[4];
00056 bytesPerPixel = Bpp/8;
00057 imageSize = Width*Height*bytesPerPixel;
00058 ImageData = (GLubyte*)malloc(imageSize);
00059 if( ImageData == NULL ||
00060 fread(ImageData, 1, imageSize, file)!=imageSize)
00061 {
00062 if(ImageData!=NULL)
00063 free(ImageData);
00064 MessageBox(NULL,"Image load failed for unknown reason.","Error",MB_OK|MB_ICONERROR);
00065 fclose(file);
00066 return false;
00067 }
00068 for(GLuint i=0; i<(GLuint)imageSize; i+=bytesPerPixel)
00069 {
00070 temp = ImageData[i];
00071 ImageData[i] = ImageData[i + 2];
00072 ImageData[i + 2] = (GLubyte)temp;
00073 }
00074 fclose (file);
00075
00076
00077 glGenTextures(1, &TexID);
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)
00084 {
00085 type=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;
00092 }