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
00033
00034
00035
00036 #include "GUI/Texture.H"
00037
00038 Texture::Texture (const char *filename)
00039 {
00040 image = new VPImage (filename);
00041 itsInitialized = false;
00042 if (image->itsInitialized)
00043 {
00044 glGenTextures (1,&name);
00045 glBindTexture (GL_TEXTURE_2D,name);
00046
00047
00048 glPixelStorei (GL_UNPACK_SWAP_BYTES, 0);
00049 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
00050 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00051 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
00052 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
00053
00054
00055
00056 #ifdef INVT_HAVE_LIBGLUT
00057 gluBuild2DMipmaps (GL_TEXTURE_2D, 3, image->width(), image->height(),
00058 GL_RGB, GL_UNSIGNED_BYTE, image->data());
00059 #endif
00060
00061
00062 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00063 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00064
00065 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00066 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
00067 GL_LINEAR_MIPMAP_LINEAR);
00068
00069 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
00070 itsInitialized = true;
00071 }
00072 }
00073
00074 Texture::~Texture()
00075 {
00076 delete image;
00077 glDeleteTextures (1,&name);
00078 }
00079
00080
00081 void Texture::bind (int modulate)
00082 {
00083 glBindTexture (GL_TEXTURE_2D,name);
00084 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
00085 modulate ? GL_MODULATE : GL_DECAL);
00086 }
00087
00088 VPImage::VPImage (const char *filename)
00089 {
00090 itsInitialized = false;
00091 FILE *f = fopen (filename,"rb");
00092 if (!f)
00093 {
00094 LINFO ("Can't open image file `%s'",filename);
00095 return;
00096 }
00097
00098
00099 if (fgetc(f) != 'P' || fgetc(f) != '6')
00100 {
00101 LINFO ("image file \"%s\" is not a binary PPM (no P6 header)",filename);
00102 return;
00103 }
00104 skipWhiteSpace (filename,f);
00105
00106
00107 image_width = readNumber (filename,f);
00108 skipWhiteSpace (filename,f);
00109 image_height = readNumber (filename,f);
00110 skipWhiteSpace (filename,f);
00111 int max_value = readNumber (filename,f);
00112
00113
00114 if (image_width < 1 || image_height < 1)
00115 LFATAL ("bad image file \"%s\"",filename);
00116 if (max_value != 255)
00117 LFATAL ("image file \"%s\" must have color range of 255",filename);
00118
00119
00120 int c = fgetc(f);
00121 if (c == 10) {
00122
00123 }
00124 else if (c == 13) {
00125
00126 c = fgetc(f);
00127 if (c != 10) ungetc (c,f);
00128 }
00129 else ungetc (c,f);
00130
00131
00132 image_data = new byte [image_width*image_height*3];
00133 if (fread (image_data,image_width*image_height*3,1,f) != 1)
00134 LFATAL ("Can not read data from image file `%s'",filename);
00135 fclose (f);
00136
00137 itsInitialized = true;
00138 }
00139
00140
00141 VPImage::~VPImage()
00142 {
00143 delete[] image_data;
00144 }
00145
00146 void VPImage::skipWhiteSpace (const char *filename, FILE *f)
00147 {
00148 int c,d;
00149 for(;;) {
00150 c = fgetc(f);
00151 if (c==EOF) LFATAL ("unexpected end of file in \"%s\"",filename);
00152
00153
00154 if (c == '#') {
00155 do {
00156 d = fgetc(f);
00157 if (d==EOF) LFATAL ("unexpected end of file in \"%s\"",filename);
00158 } while (d != '\n');
00159 continue;
00160 }
00161
00162 if (c > ' ') {
00163 ungetc (c,f);
00164 return;
00165 }
00166 }
00167 }
00168
00169
00170
00171
00172
00173 int VPImage::readNumber (const char *filename, FILE *f)
00174 {
00175 int c,n=0;
00176 for(;;) {
00177 c = fgetc(f);
00178 if (c==EOF) LFATAL ("unexpected end of file in \"%s\"",filename);
00179 if (c >= '0' && c <= '9') n = n*10 + (c - '0');
00180 else {
00181 ungetc (c,f);
00182 return n;
00183 }
00184 }
00185 }
00186
00187