00001
00044
00045 #include <stdio.h>
00046 #include <GLut/glut.h>
00047 #include "CLogger.h"
00048 #include "CTexture.h"
00049
00050
00051
00055
00056 CTextureTGA::CTextureTGA()
00057 {
00058 m_ImageDataKept = false;
00059 }
00060
00061
00062
00066
00067 CTextureTGA::~CTextureTGA()
00068 {
00070 if(m_ImageDataKept)
00071 free(m_ImageData);
00072 }
00073
00074
00075
00079
00080 unsigned char *CTextureTGA::ReadData(FILE *file)
00081 {
00082 unsigned char *data = NULL;
00083 unsigned char temp;
00084 int bytesread, i;
00085
00087
00088 m_Size = m_ImageWidth * m_ImageHeight * m_ImagePixelSize;
00089 data = (unsigned char *)malloc(m_Size);
00090
00091 if(data == NULL)
00092 return 0;
00093
00094 bytesread = fread(data, sizeof(unsigned char), m_Size, file);
00095
00096 if(bytesread != m_Size)
00097 {
00098 free(data);
00099 return 0;
00100 }
00101
00103
00104 if(m_ImageDepth == 24 || m_ImageDepth == 32)
00105 {
00108 for(i = 0; i < m_Size; i += m_ImagePixelSize)
00109 {
00110 temp = data[i];
00111 data[i] = data[i + 2];
00112 data[i + 2] = temp;
00113 }
00114 }
00115
00116 switch(m_ImageDepth)
00117 {
00118 case 32: m_TextureFormat = GL_RGBA; break;
00119 case 24: m_TextureFormat = GL_RGB; break;
00121 case 8: m_TextureFormat = GL_ALPHA; break;
00122 }
00123 return data;
00124 }
00125
00126
00127
00131
00132 int CTextureTGA::Load(char *name, int id, int texturemode, bool keepimagedata)
00133 {
00134 TGA_HEADER header;
00135 FILE *file;
00136 int error = 0;
00137
00138 if(!(file = fopen(name, "r")))
00139 {
00140 g_pGlobalLog->LogError("CTextureTGA::Load() Filename does not exist:");
00141 }
00142 else
00143 {
00144 fread(&header, sizeof(TGA_HEADER), 1, file);
00145
00146 if(header.colourmap != 0 || (header.imagetype != 2 && header.imagetype != 3))
00147 {
00148 g_pGlobalLog->LogError("CTextureTGA::Load() TGA is compressed: %s", name);
00149 }
00150 else
00151 {
00153 m_ImageWidth = header.width[0] + header.width[1] * 256;
00154 m_ImageHeight = header.height[0] + header.height[1] * 256;
00155 m_ImageDepth = header.depth;
00156 m_ImagePixelSize = header.depth / 8;
00157
00158 if(!IsPowerOf2(m_ImageWidth) || !IsPowerOf2(m_ImageHeight))
00159 {
00160 g_pGlobalLog->LogError("CTextureTGA::Load() Bad height or width in %s", name);
00161 }
00162 else
00163 {
00164 if(m_ImageDepth != 32 && m_ImageDepth != 24 && m_ImageDepth != 8)
00165 {
00166 g_pGlobalLog->LogError("CTextureTGA::Load() Bad bitdepth of %d in %s", m_ImageDepth, name);
00167 }
00168 else
00169 {
00170
00171 m_ImageData = ReadData(file);
00172
00173 if(m_ImageData == NULL)
00174 {
00175 g_pGlobalLog->LogError("CTextureTGA::Load() Bad data in %s", name);
00176 }
00177 else
00178 {
00179 glBindTexture(GL_TEXTURE_2D, id);
00180 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00183 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, texturemode);
00184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00186 gluBuild2DMipmaps(GL_TEXTURE_2D, texturemode, m_ImageWidth, m_ImageHeight, texturemode, GL_UNSIGNED_BYTE, m_ImageData);
00187 }
00188 if(!keepimagedata)
00189 free(m_ImageData);
00190 }
00191 }
00192 }
00193 fclose (file);
00194 }
00195 return error;
00196 }
00197