00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "tga.h"
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #ifdef _WIN32
00037 #include <windows.h>
00038 #endif
00039 #include <GL/gl.h>
00040 #include <GL/glu.h>
00041
00042 #include <iostream>
00043 using namespace std;
00044
00045 GLenum texFormat;
00046
00047 typedef unsigned char BYTE ;
00048
00049
00050
00051
00052
00053
00054
00055
00056 int checkSize (int x)
00057 {
00058 if (x == 2 || x == 4 ||
00059 x == 8 || x == 16 ||
00060 x == 32 || x == 64 ||
00061 x == 128 || x == 256 || x == 512)
00062 return 1;
00063 else return 0;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 unsigned char *getRGBA (FILE *s, int size)
00073 {
00074 BYTE *rgba;
00075 BYTE temp;
00076 int bread;
00077 int i;
00078
00079 rgba = new BYTE[size*4];
00080
00081 if (rgba == NULL)
00082 return 0;
00083
00084 bread = fread (rgba, sizeof (unsigned char), size * 4, s);
00085
00086 if (bread != size * 4)
00087 {
00088 delete[] rgba;
00089 return 0;
00090 }
00091
00092
00093
00094 for (i = 0; i < size * 4; i += 4 )
00095 {
00096 temp = rgba[i];
00097 rgba[i] = rgba[i + 2];
00098 rgba[i + 2] = temp;
00099 if ((rgba[i] == 255) && (rgba[i+1] == 0) && (rgba[i+2] == 255))
00100 {
00101
00102 rgba[i+3] = 0;
00103 rgba[i+1] = 255;
00104 }
00105 else
00106 {
00107
00108 rgba[i+3] = 255;
00109 }
00110
00111 }
00112
00113 texFormat = GL_RGBA;
00114 return rgba;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 unsigned char *getRGB (FILE *s, int size)
00124 {
00125 BYTE *rgb;
00126 BYTE temp;
00127 int bread;
00128 int i;
00129
00130 rgb = new BYTE[size*3];
00131
00132 if (rgb == NULL)
00133 return 0;
00134
00135 bread = fread (rgb, sizeof (unsigned char), size * 3, s);
00136
00137 if (bread != size * 3)
00138 {
00139 delete[] rgb;
00140 return 0;
00141 }
00142
00143
00144 for (i = 0; i < size * 3; i += 3)
00145 {
00146 temp = rgb[i];
00147 rgb[i] = rgb[i + 2];
00148 rgb[i + 2] = temp;
00149 }
00150
00151 texFormat = GL_RGB;
00152
00153 return rgb;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 unsigned char *getGray (FILE *s, int size)
00163 {
00164 BYTE *grayData;
00165 int bread;
00166
00167 grayData = new BYTE[size];
00168
00169 if (grayData == NULL)
00170 return 0;
00171
00172 bread = fread (grayData, sizeof (unsigned char), size, s);
00173
00174 if (bread != size)
00175 {
00176 delete[] grayData;
00177 return 0;
00178 }
00179
00180
00181 texFormat = GL_LUMINANCE;
00182
00183
00184 return grayData;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 unsigned char *getData (FILE *s, int sz, int iBits)
00194 {
00195 if (iBits == 32)
00196 return getRGBA (s, sz);
00197 else if (iBits == 24)
00198 return getRGB (s, sz);
00199 else if (iBits == 8)
00200 return getGray (s, sz);
00201 return NULL;
00202 }
00203
00204
00205
00206
00207
00208
00209 int returnError (FILE *s, int error)
00210 {
00211 fclose (s);
00212 return error;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222 int loadTGA (char *name, int id)
00223 {
00224 unsigned char type[4];
00225 unsigned char info[7];
00226 unsigned char *imageData = NULL;
00227 int imageWidth, imageHeight;
00228 int imageBits, size;
00229 FILE *s;
00230
00231 if (!(s = fopen (name, "r+bt")))
00232 return TGA_FILE_NOT_FOUND;
00233
00234 fread (&type, sizeof (char), 3, s);
00235 fseek (s, 12, SEEK_SET);
00236 fread (&info, sizeof (char), 6, s);
00237
00238 if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
00239 returnError (s, TGA_BAD_IMAGE_TYPE);
00240
00241 imageWidth = info[0] + info[1] * 256;
00242 imageHeight = info[2] + info[3] * 256;
00243 imageBits = info[4];
00244
00245 size = imageWidth * imageHeight;
00246
00247
00248 if (!checkSize (imageWidth) || !checkSize (imageHeight))
00249 returnError (s, TGA_BAD_DIMENSION);
00250
00251
00252 if (imageBits != 32 && imageBits != 24 && imageBits != 8)
00253 returnError (s, TGA_BAD_BITS);
00254
00255 imageData = getData (s, size, imageBits);
00256
00257
00258 if (imageData == NULL)
00259 returnError (s, TGA_BAD_DATA);
00260
00261 fclose (s);
00262
00263 glBindTexture (GL_TEXTURE_2D, id);
00264 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00265 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00266 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00267
00268 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00269
00270 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00271
00272
00273 if (texFormat == GL_LUMINANCE)
00274 {
00275 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00276 glTexImage2D (GL_TEXTURE_2D, 0, GL_INTENSITY, imageWidth, imageHeight, 0,
00277 GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData);
00278
00279 }
00280 else
00281 {
00282 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00283 glTexImage2D (GL_TEXTURE_2D, 0, texFormat, imageWidth, imageHeight, 0,
00284 texFormat, GL_UNSIGNED_BYTE, imageData);
00285 }
00286
00287
00288
00289 free (imageData);
00290
00291 return 1;
00292 }
00293
00294
00295
00296
00297 unsigned int createTexture( char * filename )
00298 {
00299 #define CHECK_GL_ERROR(msg) {const GLint e = glGetError(); if ((e != 0) && (e != GL_NO_ERROR)) {cerr\
00300 << endl << "OpenGL-ERROR <0x" << e << "> in " __FILE__ "(" << __LINE__ << ")!" << endl << msg << endl;cerr << gluErrorString(e) << endl;}}
00301
00302 GLuint texid = 0;
00303 glGenTextures(1,&texid);
00304 CHECK_GL_ERROR("glGenTextures failed");
00305 if (loadTGA(filename,texid) != 1)
00306 {
00307 cout << "Error occured while tried to load TGA texture from " << filename << endl;
00308 }
00309
00310 CHECK_GL_ERROR(filename);
00311
00312 return texid;
00313
00314 #undef CHECK_GL_ERROR
00315 }
00316