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};
00021 GLubyte TGAcompare[12];
00022 GLubyte header[6];
00023 GLuint bytesPerPixel;
00024 GLuint imageSize;
00025 GLuint temp;
00026 GLuint type=GL_RGBA;
00027 FILE *file = fopen(TexName, "rb");
00028 if( file==NULL ||
00029 fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||
00030 memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 ||
00031 fread(header,1,sizeof(header),file)!=sizeof(header))
00032 {
00033 if (file == NULL)
00034 {
00035 MessageBox(NULL,"Image file was not found","Error",MB_OK|MB_ICONERROR);
00036 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);
00045 return false;
00046 }
00047 Width = header[1] * 256 + header[0];
00048 Height = header[3] * 256 + header[2];
00049 if ( Width <=0 ||
00050 Height <=0 ||
00051 (header[4]!=24 && header[4]!=32))
00052 {
00053 fclose(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;
00059 }
00060 Bpp = header[4];
00061 bytesPerPixel = Bpp/8;
00062 imageSize = Width*Height*bytesPerPixel;
00063 ImageData = (GLubyte *)malloc(imageSize);
00064 if (ImageData == NULL ||
00065 fread(ImageData, 1, imageSize, file)!=imageSize)
00066 {
00067 if (ImageData!=NULL)
00068 free(ImageData);
00069 MessageBox(NULL,"Image load failed for unknown reason.","Error",MB_OK|MB_ICONERROR);
00070 fclose(file);
00071 return false;
00072 }
00073 for (GLuint i=0; i<(GLuint)imageSize; i+=bytesPerPixel)
00074 {
00075 temp = ImageData[i];
00076 ImageData[i] = ImageData[i + 2];
00077 ImageData[i + 2] = (GLubyte)temp;
00078 }
00079 fclose(file);
00080
00081
00082 glGenTextures(1, &TexID);
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)
00092 {
00093 type=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;
00102 }