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 7.9KB

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