Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextureManager.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include "TextureManager.h"
  2. #include "Utilities/Log.h"
  3. #include "Utilities/format.h"
  4. // library includes
  5. //#include "png.h"
  6. #include "physfs.h"
  7. #include "corona.h"
  8. #include "GL/glew.h"
  9. // system includes
  10. #include <fstream>
  11. #include <limits.h>
  12. using namespace std;
  13. namespace BlueCore
  14. {
  15. //----------------------------------------------------------------------------------
  16. TextureManager::TextureManager()
  17. {
  18. _maxAnisotopy = 128.0;
  19. _maxTextureSize = 1024 * 16;
  20. _lodDrop = 0;
  21. _textureCompression = true;
  22. glewInit();
  23. if (GLEW_ARB_texture_compression == false)
  24. _textureCompression = false;
  25. clog << ">>> TextureManager constructed ..."<< endline;
  26. if (_textureCompression)
  27. clog << " using texture compression"<< endline;
  28. else
  29. clog << " texture compression not supported!"<< endline;
  30. }
  31. //------------------------------------------------------------------------------
  32. TextureManager::~TextureManager()
  33. {
  34. clog << ">>> TextureManager destructed ..."<< endline;
  35. }
  36. Texture::~Texture()
  37. {
  38. glDeleteTextures( 1, &_Id);
  39. clog << ">>> Texture destructed ..."<< endline;
  40. }
  41. /*
  42. //------------------------------------------------------------------------------
  43. void TextureManager::initializeSingleton()
  44. {
  45. XmlConfig config("config.xml");
  46. _lodDrop = 0;
  47. config.getBool("TextureManager", "TextureCompression", _textureCompression,
  48. true);
  49. if (GLEW_ARB_texture_compression == false)
  50. _textureCompression = false;
  51. if (_textureCompression )
  52. clog << " using texture compression"<< endline;
  53. else
  54. clog << " texture compression not supported!"<< endline;
  55. config.getUInt("TextureManager", "MaxTextureSize", _maxTextureSize, 8192);
  56. GLint maxTextureSize;
  57. glGetIntegerv (GL_MAX_TEXTURE_SIZE, &maxTextureSize );
  58. if (_maxTextureSize > (unsigned int)maxTextureSize )
  59. _maxTextureSize = maxTextureSize;
  60. clog << " maximum texture size: "<< _maxTextureSize << endline;
  61. config.getFloat("TextureManager", "MaxAnisotopy", _maxAnisotopy, 128.0);
  62. float maxAnisotopy = 0.0;
  63. if (GLEW_EXT_texture_filter_anisotropic )
  64. glGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotopy );
  65. if (_maxAnisotopy > maxAnisotopy )
  66. _maxAnisotopy = maxAnisotopy;
  67. if (_maxAnisotopy > 1.0)
  68. clog << " using anisotopic texture filtering, level: "<< _maxAnisotopy
  69. << endlog;
  70. else
  71. clog << " not using anisotopic texture filtering."<< endlog;
  72. }
  73. //------------------------------------------------------------------------------
  74. void TextureManager::shutdownSingleton()
  75. {
  76. clog << ">>> shutdown TextureManager..."<< endline;
  77. clearTextures();
  78. }
  79. */
  80. //------------------------------------------------------------------------------
  81. bool TextureManager::saveToCache(const std::string &filename, int levels)
  82. {
  83. unsigned char *data = 0;
  84. GLint internalformat, compressed_size = 0, width = 0, height = 0, level = 0;
  85. PHYSFS_file *file = PHYSFS_openWrite(filename.c_str() );
  86. if ( !file)
  87. {
  88. return false;
  89. }
  90. level = 0;
  91. while ( !PHYSFS_eof(file) )
  92. {
  93. glGetTexLevelParameteriv(GL_TEXTURE_2D, level,
  94. GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
  95. glGetTexLevelParameteriv(GL_TEXTURE_2D, level,
  96. GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &compressed_size);
  97. if (compressed_size == 0)
  98. break;
  99. glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
  100. if (width == 0)
  101. break;
  102. glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT,
  103. &height);
  104. if (height == 0)
  105. break;
  106. glGetTexLevelParameteriv(GL_TEXTURE_2D, level,
  107. GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &compressed_size);
  108. data = new unsigned char[ compressed_size ];
  109. glGetCompressedTexImageARB(GL_TEXTURE_2D, level, data);
  110. PHYSFS_write(file, &internalformat, sizeof (internalformat ), 1);
  111. PHYSFS_write(file, &level, sizeof (level ), 1);
  112. PHYSFS_write(file, &width, sizeof (width ), 1);
  113. PHYSFS_write(file, &height, sizeof (height ), 1);
  114. PHYSFS_write(file, &compressed_size, sizeof (compressed_size ), 1);
  115. PHYSFS_write(file, data, compressed_size, 1);
  116. delete [] data;
  117. level += 1;
  118. if ( (width == 1 ) && (height == 1 ))
  119. break;
  120. if (level >= levels)
  121. break;
  122. }
  123. PHYSFS_close(file);
  124. return true;
  125. }
  126. //------------------------------------------------------------------------------
  127. bool TextureManager::loadFromCache(const std::string &filename,
  128. unsigned int &max_width, unsigned int &max_height)
  129. {
  130. unsigned char *data = 0;
  131. GLuint internalformat, compressed_size, width, height, level;
  132. max_width = 0;
  133. max_height = 0;
  134. PHYSFS_file *file = PHYSFS_openRead(filename.c_str() );
  135. if ( !file)
  136. {
  137. return false;
  138. }
  139. while ( !PHYSFS_eof(file) )
  140. {
  141. PHYSFS_read(file, &internalformat, sizeof (internalformat ), 1);
  142. PHYSFS_read(file, &level, sizeof (level ), 1);
  143. PHYSFS_read(file, &width, sizeof (width ), 1);
  144. PHYSFS_read(file, &height, sizeof (height ), 1);
  145. PHYSFS_read(file, &compressed_size, sizeof (compressed_size ), 1);
  146. if ( !data)
  147. data = new unsigned char[ compressed_size ];
  148. PHYSFS_read(file, data, compressed_size, 1);
  149. glCompressedTexImage2DARB(GL_TEXTURE_2D, level, internalformat, width,
  150. height, 0, compressed_size, data);
  151. max_width = max(width, max_width);
  152. max_height = max(height, max_height);
  153. if (width == 1 && height == 1)
  154. break;
  155. }
  156. delete [] data;
  157. PHYSFS_close(file);
  158. return true;
  159. }
  160. //------------------------------------------------------------------------------
  161. Texture *TextureManager::loadTexture(const std::string &filename,
  162. int mipmapLevel, int compressionLevel)
  163. {
  164. if (filename.empty() )
  165. return 0;
  166. std::string cachename = filename;
  167. cachename += "_";
  168. cachename += format("%d", mipmapLevel);
  169. cachename += "_";
  170. cachename += format("%d", compressionLevel);
  171. cachename += ".cache";
  172. TextureMap::const_iterator result;
  173. result = textures.find(filename);
  174. if (result != textures.end() )
  175. {
  176. return result->second;
  177. }
  178. GLint UnpackAlignment;
  179. // Set unpack alignment to one byte
  180. glGetIntegerv(GL_UNPACK_ALIGNMENT, &UnpackAlignment);
  181. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  182. unsigned int id = 0;
  183. glGenTextures( 1, &id);
  184. glBindTexture(GL_TEXTURE_2D, id);
  185. if (mipmapLevel == 0)
  186. {
  187. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  188. }
  189. else
  190. {
  191. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  192. GL_LINEAR_MIPMAP_NEAREST);
  193. }
  194. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  195. if (_maxAnisotopy > 0.0)
  196. {
  197. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  198. ( float ) _maxAnisotopy );
  199. }
  200. Texture *t = 0;
  201. unsigned int width = 0, height = 0;
  202. // check for cached texture
  203. if (_textureCompression && (compressionLevel > 0))
  204. {
  205. PHYSFS_sint64 mod_file = PHYSFS_getLastModTime(filename.c_str() );
  206. PHYSFS_sint64 mod_config = PHYSFS_getLastModTime("config.xml");
  207. PHYSFS_sint64 mod_cache = PHYSFS_getLastModTime(cachename.c_str() );
  208. if ( (mod_cache > mod_file) && (mod_cache > mod_config))
  209. {
  210. if (loadFromCache(cachename, width, height) )
  211. {
  212. glPixelStorei(GL_UNPACK_ALIGNMENT, UnpackAlignment);
  213. clog << ">>> Texture '" << filename << "' loaded from cache ("
  214. << width << ", " << height << ")." << endline;
  215. t = new Texture( id, width, height );
  216. textures[filename] = t;
  217. return t;
  218. }
  219. }
  220. }
  221. //clog << ">>> Texture '" << filename << "': loading from file..." << endline;
  222. corona::Image *image = corona::OpenImage(filename.c_str(),
  223. corona::PF_R8G8B8A8);
  224. if (image)
  225. {
  226. int level = 0;
  227. unsigned int width_scaled = image->getWidth();
  228. unsigned int height_scaled = image->getHeight();
  229. corona::FlipImage(image, corona::CA_X);
  230. // create mipmaps
  231. while ( 1)
  232. {
  233. if ( (width_scaled <= _maxTextureSize ) && (height_scaled
  234. <= _maxTextureSize ))
  235. {
  236. width = max(width_scaled, width);
  237. height = max(height_scaled, height);
  238. if (_textureCompression && (compressionLevel > 0))
  239. {
  240. glHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
  241. GLuint compression = GL_COMPRESSED_RGBA_ARB;
  242. if (GLEW_EXT_texture_compression_s3tc)
  243. {
  244. if (compressionLevel == 1)
  245. compression = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  246. else if (compressionLevel == 2)
  247. compression = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  248. else
  249. compression = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  250. }
  251. glTexImage2D(GL_TEXTURE_2D, level, compression,
  252. width_scaled, height_scaled, 0, GL_RGBA,
  253. GL_UNSIGNED_BYTE, ( void* ) image->getPixels() );
  254. }
  255. else
  256. {
  257. glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, width_scaled,
  258. height_scaled, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  259. ( void* ) image->getPixels() );
  260. }
  261. level += 1;
  262. }
  263. if ( (width_scaled == 1 ) && (height_scaled == 1 ))
  264. break;
  265. if ( (mipmapLevel >= 0 ) && (level >= mipmapLevel ))
  266. break;
  267. // rescale image
  268. int halfwidth = width_scaled > 1 ? width_scaled / 2 : 1;
  269. int halfheight = height_scaled > 1 ? height_scaled / 2 : 1;
  270. int idx1 = width_scaled * 4;
  271. int idx2 = (width_scaled + 1 ) * 4;
  272. unsigned char *dst = (unsigned char *)image->getPixels();
  273. unsigned char *src = (unsigned char *)image->getPixels();
  274. for (int y = 0; y < halfheight; y++)
  275. {
  276. for (int x = 0; x < halfwidth; x++)
  277. {
  278. for (int k = 0; k < 4; k ++)
  279. {
  280. *dst ++ = ( GLubyte ) ( ( ( int ) *src + ( int ) src[4]
  281. + ( int ) src[idx1] + ( int ) src[idx2] + 2 )
  282. >> 2 );
  283. src ++;
  284. }
  285. src += 4;
  286. }
  287. src += 4 * width_scaled;
  288. }
  289. width_scaled = halfheight;
  290. height_scaled = halfwidth;
  291. }
  292. delete image;
  293. if (_textureCompression && (compressionLevel > 0))
  294. saveToCache(cachename, level);
  295. t = new Texture( id, width, height );
  296. clog << ">>> Texture '" << filename << "': loaded from file" << endline;
  297. }
  298. else
  299. {
  300. glDeleteTextures( 1, &id);
  301. t = new Texture( 0, 0, 0 );
  302. clog << "!!! Texture '" << filename << "' not found!" << endline;
  303. }
  304. glPixelStorei(GL_UNPACK_ALIGNMENT, UnpackAlignment);
  305. textures[filename] = t;
  306. return t;
  307. }
  308. /*
  309. //------------------------------------------------------------------------------
  310. void TextureManager::releaseTexture ( const Texture *texture )
  311. {
  312. Texture *t = (Texture *)texture;
  313. if( t->_RefCount > 1 )
  314. t->_RefCount--;
  315. else if( t->_RefCount == 1 )
  316. {
  317. glDeleteTextures ( 1, &t->_Id );
  318. delete t;
  319. TextureMap::iterator i;
  320. for( i = textures.begin(); i != textures.end(); i++ )
  321. {
  322. if( (*i).second == t )
  323. {
  324. textures.erase( i );
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. */
  331. //------------------------------------------------------------------------------
  332. void TextureManager::TextureDestroySlot(Referenced *referenced)
  333. {
  334. TextureMap::iterator i;
  335. for (i = textures.begin(); i != textures.end(); i++)
  336. {
  337. Texture *t = (*i).second;
  338. if (t == (Texture*)referenced)
  339. {
  340. textures.erase(i);
  341. break;
  342. }
  343. }
  344. }
  345. }