00001 #ifndef TEXTURE_UTILS_H
00002 #define TEXTURE_UTILS_H
00003
00004 #include "GenUtils.h"
00005 #include "glwxOpenGL.h"
00006
00007
00008
00009
00010
00011
00012 template <class MediaInfoType>
00013 class MediaInfo
00014 {
00015 public:
00016
00017 MediaInfo(const std::string &mediaPath_, MediaInfoType mediaArg = 0)
00018 {
00019 userCount = 1;
00020 mediaPath = mediaPath_;
00021 media = mediaArg;
00022 }
00023
00024 MediaInfo(const MediaInfo ©)
00025 {
00026 this->operator=(copy);
00027 }
00028
00029 MediaInfo &operator =(const MediaInfo ©)
00030 {
00031 if(this != ©)
00032 {
00033 userCount = copy.userCount;
00034 mediaPath = copy.mediaPath;
00035 media = copy.media;
00036 }
00037 return *this;
00038 }
00039
00040 void setMediaPath(const char *mediaPath_)
00041 {
00042 mediaPath = mediaPath_;
00043 }
00044
00045 const std::string &getMediaPath() const
00046 {
00047 return mediaPath;
00048 }
00049
00050 void increaseUserCount()
00051 {
00052 userCount++;
00053 }
00054
00055 void decreaseUserCount()
00056 {
00057 userCount--;
00058 }
00059
00060 unsigned int getUserCount()
00061 {
00062 return userCount;
00063 }
00064
00065 MediaInfoType &getMedia()
00066 {
00067 return media;
00068 }
00069
00070 void setMedia(MediaInfoType &mediaArg)
00071 {
00072 media = mediaArg;
00073 }
00074
00075 bool operator > (const MediaInfo &info)
00076 {
00077 return mediaPath > info.mediaPath;
00078 }
00079
00080 bool operator < (const MediaInfo &info)
00081 {
00082 return mediaPath < info.mediaPath;
00083 }
00084
00085 bool operator == (const MediaInfo &info)
00086 {
00087 return (mediaPath == info.mediaPath) &&
00088 (userCount == info.userCount) &&
00089 (media == info.media );
00090 }
00091
00092 bool operator != (const MediaInfo &info)
00093 {
00094 return !this->operator ==(info);
00095 }
00096
00097 protected:
00098 unsigned int userCount;
00099 MediaInfoType media;
00100 std::string mediaPath;
00101
00102 };
00103
00104 typedef MediaInfo<unsigned int> TextureInfo;
00105
00106
00107
00108
00109
00110
00111 class TexturesManager
00112 {
00113 public:
00114 static bool addTextureInfo(TextureInfo *textureInfo);
00115 static TextureInfo *getTextureInfo(const char* texturePath);
00116 static TextureInfo *getTextureInfo(GLuint textureID);
00117 static void flushUnusedTextures();
00118
00119 static void printTexturesInfo();
00120 static void flushAllTextures();
00121 private:
00122 static vector<TextureInfo*> textureCollection;
00123
00124 };
00125
00126
00127
00128
00129
00130
00131 class Image
00132 {
00133 private:
00134 std::string path;
00135 unsigned char *dataBuffer;
00136 unsigned int internalFormat,
00137 components,
00138 format;
00139
00140 unsigned short height,
00141 width,
00142 depth;
00143
00144 bool loadTGA(const char* filename);
00145 bool loadUncompressed8BitTGA(const char * filename);
00146 bool loadCompressedTrueColorTGA(const char * filename);
00147 bool loadUncompressedTrueColorTGA(const char * filename);
00148 bool loadPPM(const char * filename);
00149 #ifdef GLWX_PNG_SUPPORT
00150 bool loadPNG(const char* filename);
00151 #endif
00152 bool loadJPG(const char* filename);
00153 void getJPGInfo();
00154 int decodeScanJPG();
00155 int decodeJPG();
00156
00157 public:
00158 Image(const char* path = NULL);
00159 ~Image();
00160
00161 static bool isDDSVolume(const char* filename);
00162 static bool isDDSCube (const char* filename);
00163
00164 void setHeight(unsigned short);
00165 void setWidth(unsigned short);
00166 void setDepth(unsigned short);
00167
00168 void setFormat(unsigned int );
00169 void setDataBuffer(const unsigned char* );
00170 void flipVertically();
00171 void setInternalFormat(unsigned int );
00172 void setComponentsCount(unsigned int );
00173
00174 bool load(const char*);
00175
00176 const std::string &getPath() const;
00177 const unsigned int getComponentsCount() const;
00178 const unsigned int getInternalFormat() const;
00179 const unsigned char* getDataBuffer() const;
00180 const unsigned int getFormat() const;
00181
00182 const unsigned short getHeight() const;
00183 const unsigned short getWidth() const;
00184 const unsigned short getDepth() const;
00185 };
00186
00187
00188
00189
00190
00191
00192
00193 class Texture : public IOXMLObject
00194 {
00195 public:
00196 Texture(GLuint target = GL_TEXTURE_2D);
00197 Texture(const Texture ©);
00198 Texture &operator = (const Texture &);
00199 ~Texture();
00200
00201 virtual bool loadXMLSettings(const TiXmlElement *element);
00202 virtual bool exportXMLSettings(std::ofstream &xmlFile );
00203
00204 bool load2D(const char* infoEndOrPath,
00205 GLuint clampS = GL_REPEAT,
00206 GLuint clampT = GL_REPEAT,
00207 GLuint magFilter = GL_LINEAR,
00208 GLuint minFilter = GL_LINEAR_MIPMAP_LINEAR,
00209 bool mipmap = true);
00210
00211 bool load2DImage(const Image &image,
00212 GLuint clampS = GL_REPEAT,
00213 GLuint clampT = GL_REPEAT,
00214 GLuint magFilter = GL_LINEAR,
00215 GLuint minFilter = GL_LINEAR_MIPMAP_LINEAR,
00216 bool mipmap = true);
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 void copyCurrentBuffer(int newTarget = -1);
00283
00284 const bool activate(GLuint unit = 0);
00285 const bool deactivate();
00286
00287
00288 void setID(GLuint textureID);
00289 const GLuint getID() const;
00290
00291 void setTarget(GLuint target);
00292 const GLuint getTarget() const;
00293
00294 const GLuint getHeight() const;
00295 const GLuint getWidth() const;
00296 const GLuint getDepth() const;
00297 void destroy();
00298
00299 protected:
00300 GLuint height,
00301 width,
00302 depth;
00303 private:
00304 GLuint currUnit,
00305 target,
00306 id;
00307
00308 bool loadTextureFace(const Image &image, GLuint target, bool mipmap);
00309
00310 bool finalizeLoading(const char* string);
00311 bool checkForRepeat (const char* string);
00312
00313 int getMagFilteri(const std::string &value);
00314 int getMinFilteri(const std::string &value);
00315 int getWrapModei (const std::string &value);
00316 int getTypei (const std::string &value);
00317
00318 int getValidWrapMode (int clamp);
00319 int getValidMagFilter(int filter);
00320 int getValidMinFilter(int filter);
00321
00322 };
00323 #endif