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