Open Source Tomb Raider Engine
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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*!
  2. * \file src/TextureManager.cpp
  3. * \brief Texture registry
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include "stb/stb_image.h"
  9. #include "global.h"
  10. #include "Log.h"
  11. #include "RunTime.h"
  12. #include "utils/Folder.h"
  13. #include "utils/pcx.h"
  14. #include "utils/pixel.h"
  15. #include "utils/random.h"
  16. #include "utils/strings.h"
  17. #include "TextureManager.h"
  18. std::vector<unsigned int> TextureManager::mTextureIdsGame;
  19. std::vector<unsigned int> TextureManager::mTextureIdsSystem;
  20. std::vector<TextureTile*> TextureManager::tiles;
  21. std::vector<std::vector<int>> TextureManager::animations;
  22. std::vector<int> TextureManager::gameUnits;
  23. std::vector<int> TextureManager::systemUnits;
  24. unsigned int TextureManager::nextFreeTextureUnit = 0;
  25. int TextureManager::initialize() {
  26. assert(mTextureIdsGame.size() == 0);
  27. assert(mTextureIdsSystem.size() == 0);
  28. while (mTextureIdsSystem.size() < 2) {
  29. unsigned int id;
  30. glGenTextures(1, &id);
  31. mTextureIdsSystem.push_back(id);
  32. }
  33. return 0;
  34. }
  35. int TextureManager::initializeSplash() {
  36. unsigned char* image = generateColorTexture(WHITE, 32, 32, 32);
  37. int res = loadBufferSlot(image, 32, 32, ColorMode::RGBA, 32, TextureStorage::SYSTEM, TEXTURE_WHITE,
  38. false);
  39. delete [] image;
  40. if (res < 0) {
  41. return -1;
  42. }
  43. Folder f(RunTime::getPakDir());
  44. std::vector<File> files;
  45. f.findRecursiveFilesEndingWith(files, ".pcx");
  46. if (files.size() == 0) {
  47. if (loadImage(RunTime::getDataDir() + "/splash.tga", TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  48. return -2;
  49. }
  50. } else {
  51. int i = randomInteger(files.size() - 1);
  52. if (loadImage(files.at(i).getPath(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  53. if (loadImage(RunTime::getDataDir() + "/splash.tga", TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  54. return -3;
  55. }
  56. }
  57. }
  58. return 0;
  59. }
  60. void TextureManager::shutdown() {
  61. while (mTextureIdsSystem.size() > 0) {
  62. unsigned int id = mTextureIdsSystem.at(mTextureIdsSystem.size() - 1);
  63. glDeleteTextures(1, &id);
  64. mTextureIdsSystem.pop_back();
  65. }
  66. clear();
  67. }
  68. void TextureManager::clear() {
  69. while (mTextureIdsGame.size() > 0) {
  70. unsigned int id = mTextureIdsGame.at(mTextureIdsGame.size() - 1);
  71. glDeleteTextures(1, &id);
  72. mTextureIdsGame.pop_back();
  73. }
  74. while (!tiles.empty()) {
  75. delete tiles.at(tiles.size() - 1);
  76. tiles.pop_back();
  77. }
  78. animations.clear();
  79. gameUnits.clear();
  80. systemUnits.clear();
  81. nextFreeTextureUnit = 0;
  82. }
  83. int TextureManager::loadBufferSlot(unsigned char* image,
  84. unsigned int width, unsigned int height,
  85. ColorMode mode, unsigned int bpp,
  86. TextureStorage s, int slot, bool filter) {
  87. assert(width > 0);
  88. assert(height > 0);
  89. assert((mode == ColorMode::RGB)
  90. || (mode == ColorMode::BGR)
  91. || (mode == ColorMode::ARGB)
  92. || (mode == ColorMode::RGBA)
  93. || (mode == ColorMode::BGRA));
  94. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  95. if (slot < 0)
  96. slot = getIds(s).size();
  97. while (getIds(s).size() <= slot) {
  98. unsigned int id;
  99. glGenTextures(1, &id);
  100. getIds(s).push_back(id);
  101. }
  102. if (image == nullptr)
  103. return slot;
  104. unsigned int glcMode;
  105. switch (mode) {
  106. case ColorMode::BGR:
  107. glcMode = GL_BGR;
  108. break;
  109. case ColorMode::RGB:
  110. glcMode = GL_RGB;
  111. break;
  112. case ColorMode::ARGB:
  113. argb2rgba32(image, width, height);
  114. glcMode = GL_RGBA;
  115. break;
  116. case ColorMode::BGRA:
  117. glcMode = GL_BGRA;
  118. break;
  119. case ColorMode::RGBA:
  120. glcMode = GL_RGBA;
  121. break;
  122. }
  123. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  124. bindTexture(slot, s);
  125. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, glcMode, GL_UNSIGNED_BYTE, image);
  126. if (filter) {
  127. // Trilinear filtering
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  129. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  130. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  132. glGenerateMipmap(GL_TEXTURE_2D);
  133. } else {
  134. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  136. }
  137. return slot;
  138. }
  139. int TextureManager::numTextures(TextureStorage s) {
  140. return getIds(s).size();
  141. }
  142. void TextureManager::bindTextureId(unsigned int n, TextureStorage s, unsigned int unit) {
  143. assert(n < getIds(s).size());
  144. assert(unit < 80); //! \todo Query GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
  145. glActiveTexture(GL_TEXTURE0 + unit);
  146. glBindTexture(GL_TEXTURE_2D, getIds(s).at(n));
  147. }
  148. int TextureManager::bindTexture(unsigned int n, TextureStorage s) {
  149. assert(n < getIds(s).size());
  150. if ((n < getUnits(s).size()) && (getUnits(s).at(n) >= 0)) {
  151. bindTextureId(n, s, getUnits(s).at(n));
  152. return getUnits(s).at(n);
  153. } else {
  154. while (getUnits(s).size() <= n)
  155. getUnits(s).push_back(-1);
  156. getUnits(s).at(n) = nextFreeTextureUnit;
  157. bindTextureId(n, s, nextFreeTextureUnit);
  158. nextFreeTextureUnit++;
  159. return nextFreeTextureUnit - 1;
  160. }
  161. }
  162. void TextureManager::addTile(TextureTile* t) {
  163. tiles.push_back(t);
  164. }
  165. int TextureManager::numTiles() {
  166. return tiles.size();
  167. }
  168. TextureTile& TextureManager::getTile(int index) {
  169. assert(index >= 0);
  170. assert(index < tiles.size());
  171. return *tiles.at(index);
  172. }
  173. void TextureManager::addAnimatedTile(int index, int tile) {
  174. while (index >= animations.size())
  175. animations.push_back(std::vector<int>());
  176. animations.at(index).push_back(tile);
  177. }
  178. int TextureManager::numAnimatedTiles() {
  179. return animations.size();
  180. }
  181. int TextureManager::getFirstTileAnimation(int index) {
  182. assert(index < animations.size());
  183. assert(animations.at(index).size() > 0);
  184. return animations.at(index).at(0);
  185. }
  186. int TextureManager::getNextTileAnimation(int tile) {
  187. for (int a = 0; a < animations.size(); a++) {
  188. for (int i = 0; i < animations.at(a).size(); i++) {
  189. if (animations.at(a).at(i) == tile) {
  190. if (i < (animations.at(a).size() - 1))
  191. return animations.at(a).at(i + 1);
  192. else
  193. return animations.at(a).at(0);
  194. }
  195. }
  196. }
  197. return -1;
  198. }
  199. int TextureManager::loadImage(std::string filename, TextureStorage s, int slot) {
  200. if (stringEndsWith(filename, ".pcx")) {
  201. return loadPCX(filename, s, slot);
  202. } else {
  203. int x, y, n;
  204. unsigned char* data = stbi_load(filename.c_str(), &x, &y, &n, 0);
  205. if (data) {
  206. if ((n < 3) || (n > 4)) {
  207. Log::get(LOG_ERROR) << "Image \"" << filename << "\" has unsupported format ("
  208. << n << ")!" << Log::endl;
  209. stbi_image_free(data);
  210. return -2;
  211. }
  212. int id = loadBufferSlot(data, x, y, (n == 3) ? ColorMode::RGB : ColorMode::RGBA,
  213. (n == 3) ? 24 : 32, s, slot);
  214. stbi_image_free(data);
  215. return id;
  216. } else {
  217. Log::get(LOG_ERROR) << "Can't load image \"" << filename << "\"!" << Log::endl;
  218. return -1;
  219. }
  220. }
  221. }
  222. int TextureManager::loadPCX(std::string filename, TextureStorage s, int slot) {
  223. int error = pcxCheck(filename.c_str());
  224. if (!error) {
  225. unsigned char* image;
  226. unsigned int w, h, bpp;
  227. ColorMode c;
  228. error = pcxLoad(filename.c_str(), &image, &w, &h, &c, &bpp);
  229. if (!error) {
  230. unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
  231. if (image2) {
  232. delete [] image;
  233. image = image2;
  234. }
  235. int id = loadBufferSlot(image, w, h, c, bpp, s, slot);
  236. delete [] image;
  237. return id;
  238. }
  239. return -5;
  240. }
  241. return -4;
  242. }
  243. std::vector<unsigned int>& TextureManager::getIds(TextureStorage s) {
  244. if (s == TextureStorage::GAME)
  245. return mTextureIdsGame;
  246. else
  247. return mTextureIdsSystem;
  248. }
  249. std::vector<int>& TextureManager::getUnits(TextureStorage s) {
  250. if (s == TextureStorage::GAME)
  251. return gameUnits;
  252. else
  253. return systemUnits;
  254. }
  255. // ----------------------------------------------------------------------------
  256. glm::vec2 TextureTile::getUV(unsigned int i) {
  257. glm::vec2 uv(vertices.at(i).xPixel,
  258. vertices.at(i).yPixel);
  259. /*! \fixme
  260. * This is my somewhat hacky approach to fixing
  261. * the bad texture-bleeding problems everywhere.
  262. * That's better, but makes the seams between
  263. * each sector much more visible!
  264. */
  265. if (vertices.at(i).xCoordinate == 1) {
  266. uv.x += 0.375f;
  267. }
  268. if (vertices.at(i).yCoordinate == 1) {
  269. uv.y += 0.375f;
  270. }
  271. return uv / 256.0f;
  272. }