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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*!
  2. * \file src/TextureManager.cpp
  3. * \brief Texture registry
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include "imgui/imgui.h"
  9. #include "stb/stb_image.h"
  10. #include "global.h"
  11. #include "Game.h"
  12. #include "Log.h"
  13. #include "RunTime.h"
  14. #include "World.h"
  15. #include "utils/Folder.h"
  16. #include "utils/pcx.h"
  17. #include "utils/pixel.h"
  18. #include "utils/random.h"
  19. #include "utils/strings.h"
  20. #include "TextureManager.h"
  21. glm::vec2 TextureTile::getUV(unsigned int i) {
  22. glm::vec2 uv(vertices.at(i).xPixel,
  23. vertices.at(i).yPixel);
  24. /*! \fixme
  25. * This is my somewhat hacky approach to fixing
  26. * the bad texture-bleeding problems everywhere.
  27. * That's better, but makes the seams between
  28. * each sector much more visible!
  29. */
  30. if (vertices.at(i).xCoordinate == 1) {
  31. uv.x += 0.375f;
  32. }
  33. if (vertices.at(i).yCoordinate == 1) {
  34. uv.y += 0.375f;
  35. }
  36. return uv / 256.0f;
  37. }
  38. // ----------------------------------------------------------------------------
  39. std::vector<unsigned int> TextureManager::mTextureIdsGame;
  40. std::vector<unsigned int> TextureManager::mTextureIdsSystem;
  41. std::vector<TextureTile*> TextureManager::tiles;
  42. std::vector<std::vector<int>> TextureManager::animations;
  43. std::vector<int> TextureManager::gameUnits;
  44. std::vector<int> TextureManager::systemUnits;
  45. unsigned int TextureManager::nextFreeTextureUnit = 0;
  46. std::vector<BufferManager> TextureManager::gameBuffers;
  47. std::vector<BufferManager> TextureManager::systemBuffers;
  48. int TextureManager::initialize() {
  49. assert(mTextureIdsGame.size() == 0);
  50. assert(mTextureIdsSystem.size() == 0);
  51. while (mTextureIdsSystem.size() < 2) {
  52. unsigned int id;
  53. glGenTextures(1, &id);
  54. mTextureIdsSystem.push_back(id);
  55. }
  56. unsigned char* image = generateColorTexture(WHITE, 32, 32, 32);
  57. int res = loadBufferSlot(image, 32, 32, ColorMode::RGBA, 32, TextureStorage::SYSTEM, TEXTURE_WHITE,
  58. false);
  59. delete [] image;
  60. if (res < 0) {
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. int TextureManager::initializeSplash() {
  66. Folder f(RunTime::getPakDir());
  67. std::vector<File> files;
  68. f.findRecursiveFilesEndingWith(files, ".pcx");
  69. if (files.size() == 0) {
  70. if (loadImage(RunTime::getDataDir() + "/splash.tga", TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  71. return -2;
  72. }
  73. } else {
  74. int i = randomInteger(files.size() - 1);
  75. if (loadImage(files.at(i).getPath(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  76. if (loadImage(RunTime::getDataDir() + "/splash.tga", TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  77. return -3;
  78. }
  79. }
  80. }
  81. return 0;
  82. }
  83. void TextureManager::shutdown() {
  84. while (mTextureIdsSystem.size() > 0) {
  85. unsigned int id = mTextureIdsSystem.at(mTextureIdsSystem.size() - 1);
  86. glDeleteTextures(1, &id);
  87. mTextureIdsSystem.pop_back();
  88. }
  89. gameBuffers.clear();
  90. systemBuffers.clear();
  91. clear();
  92. }
  93. void TextureManager::clear() {
  94. while (mTextureIdsGame.size() > 0) {
  95. unsigned int id = mTextureIdsGame.at(mTextureIdsGame.size() - 1);
  96. glDeleteTextures(1, &id);
  97. mTextureIdsGame.pop_back();
  98. }
  99. while (!tiles.empty()) {
  100. delete tiles.at(tiles.size() - 1);
  101. tiles.pop_back();
  102. }
  103. animations.clear();
  104. gameUnits.clear();
  105. systemUnits.clear();
  106. nextFreeTextureUnit = 0;
  107. }
  108. int TextureManager::loadBufferSlot(unsigned char* image,
  109. unsigned int width, unsigned int height,
  110. ColorMode mode, unsigned int bpp,
  111. TextureStorage s, int slot, bool filter) {
  112. assert(width > 0);
  113. assert(height > 0);
  114. assert((mode == ColorMode::RGB)
  115. || (mode == ColorMode::BGR)
  116. || (mode == ColorMode::ARGB)
  117. || (mode == ColorMode::RGBA)
  118. || (mode == ColorMode::BGRA));
  119. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  120. if (slot < 0)
  121. slot = getIds(s).size();
  122. while (getIds(s).size() <= slot) {
  123. unsigned int id;
  124. glGenTextures(1, &id);
  125. getIds(s).push_back(id);
  126. }
  127. if (image == nullptr)
  128. return slot;
  129. unsigned int glcMode;
  130. switch (mode) {
  131. case ColorMode::BGR:
  132. glcMode = GL_BGR;
  133. break;
  134. case ColorMode::RGB:
  135. glcMode = GL_RGB;
  136. break;
  137. case ColorMode::ARGB:
  138. argb2rgba32(image, width, height);
  139. glcMode = GL_RGBA;
  140. break;
  141. case ColorMode::BGRA:
  142. glcMode = GL_BGRA;
  143. break;
  144. case ColorMode::RGBA:
  145. glcMode = GL_RGBA;
  146. break;
  147. }
  148. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  149. bindTexture(slot, s);
  150. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, glcMode, GL_UNSIGNED_BYTE, image);
  151. if (filter) {
  152. // Trilinear filtering
  153. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  154. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  156. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  157. glGenerateMipmap(GL_TEXTURE_2D);
  158. } else {
  159. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  160. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  161. }
  162. return slot;
  163. }
  164. int TextureManager::numTextures(TextureStorage s) {
  165. return getIds(s).size();
  166. }
  167. void TextureManager::bindTextureId(unsigned int n, TextureStorage s, unsigned int unit) {
  168. assert(n < getIds(s).size());
  169. assert(unit < 80); //! \todo Query GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
  170. glActiveTexture(GL_TEXTURE0 + unit);
  171. glBindTexture(GL_TEXTURE_2D, getIds(s).at(n));
  172. }
  173. int TextureManager::bindTexture(unsigned int n, TextureStorage s) {
  174. assert(n < getIds(s).size());
  175. if ((n < getUnits(s).size()) && (getUnits(s).at(n) >= 0)) {
  176. bindTextureId(n, s, getUnits(s).at(n));
  177. return getUnits(s).at(n);
  178. } else {
  179. while (getUnits(s).size() <= n)
  180. getUnits(s).push_back(-1);
  181. getUnits(s).at(n) = nextFreeTextureUnit;
  182. bindTextureId(n, s, nextFreeTextureUnit);
  183. nextFreeTextureUnit++;
  184. return nextFreeTextureUnit - 1;
  185. }
  186. }
  187. void TextureManager::addTile(TextureTile* t) {
  188. tiles.push_back(t);
  189. }
  190. int TextureManager::numTiles() {
  191. return tiles.size();
  192. }
  193. TextureTile& TextureManager::getTile(int index) {
  194. assert(index >= 0);
  195. assert(index < tiles.size());
  196. return *tiles.at(index);
  197. }
  198. void TextureManager::addAnimatedTile(int index, int tile) {
  199. while (index >= animations.size())
  200. animations.push_back(std::vector<int>());
  201. animations.at(index).push_back(tile);
  202. }
  203. int TextureManager::numAnimatedTiles() {
  204. return animations.size();
  205. }
  206. int TextureManager::getFirstTileAnimation(int index) {
  207. assert(index < animations.size());
  208. assert(animations.at(index).size() > 0);
  209. return animations.at(index).at(0);
  210. }
  211. int TextureManager::getNextTileAnimation(int index, int tile) {
  212. assert(index < animations.size());
  213. for (int i = 0; i < animations.at(index).size(); i++) {
  214. if (animations.at(index).at(i) == tile) {
  215. if (i < (animations.at(index).size() - 1))
  216. return animations.at(index).at(i + 1);
  217. else
  218. return animations.at(index).at(0);
  219. }
  220. }
  221. return -1;
  222. }
  223. BufferManager* TextureManager::getBufferManager(int tex, TextureStorage store) {
  224. auto& v = (store == TextureStorage::GAME) ? gameBuffers : systemBuffers;
  225. while (v.size() <= (tex + 1)) {
  226. v.emplace_back(v.size(), store);
  227. }
  228. return &(v.at(tex));
  229. }
  230. int TextureManager::loadImage(std::string filename, TextureStorage s, int slot) {
  231. if (stringEndsWith(filename, ".pcx")) {
  232. return loadPCX(filename, s, slot);
  233. } else {
  234. int x, y, n;
  235. unsigned char* data = stbi_load(filename.c_str(), &x, &y, &n, 0);
  236. if (data) {
  237. if ((n < 3) || (n > 4)) {
  238. Log::get(LOG_ERROR) << "Image \"" << filename << "\" has unsupported format ("
  239. << n << ")!" << Log::endl;
  240. stbi_image_free(data);
  241. return -2;
  242. }
  243. int id = loadBufferSlot(data, x, y, (n == 3) ? ColorMode::RGB : ColorMode::RGBA,
  244. (n == 3) ? 24 : 32, s, slot);
  245. stbi_image_free(data);
  246. return id;
  247. } else {
  248. Log::get(LOG_ERROR) << "Can't load image \"" << filename << "\"!" << Log::endl;
  249. return -1;
  250. }
  251. }
  252. }
  253. int TextureManager::loadPCX(std::string filename, TextureStorage s, int slot) {
  254. int error = pcxCheck(filename.c_str());
  255. if (!error) {
  256. unsigned char* image;
  257. unsigned int w, h, bpp;
  258. ColorMode c;
  259. error = pcxLoad(filename.c_str(), &image, &w, &h, &c, &bpp);
  260. if (!error) {
  261. unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
  262. if (image2) {
  263. delete [] image;
  264. image = image2;
  265. }
  266. int id = loadBufferSlot(image, w, h, c, bpp, s, slot);
  267. delete [] image;
  268. return id;
  269. }
  270. return -5;
  271. }
  272. return -4;
  273. }
  274. std::vector<unsigned int>& TextureManager::getIds(TextureStorage s) {
  275. if (s == TextureStorage::GAME)
  276. return mTextureIdsGame;
  277. else
  278. return mTextureIdsSystem;
  279. }
  280. std::vector<int>& TextureManager::getUnits(TextureStorage s) {
  281. if (s == TextureStorage::GAME)
  282. return gameUnits;
  283. else
  284. return systemUnits;
  285. }
  286. void TextureManager::display() {
  287. if (ImGui::CollapsingHeader("Texture Viewer")) {
  288. static bool game = Game::isLoaded();
  289. static int index = 0;
  290. ImGui::SliderInt("##texslide", &index, 0, TextureManager::numTextures(
  291. game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1);
  292. ImGui::SameLine();
  293. if (ImGui::Button("+##texplus", ImVec2(0, 0), true)) {
  294. if (index < (numTextures(
  295. game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1))
  296. index++;
  297. else
  298. index = 0;
  299. }
  300. ImGui::SameLine();
  301. if (ImGui::Button("-##texminus", ImVec2(0, 0), true)) {
  302. if (index > 0)
  303. index--;
  304. else
  305. index = numTextures(
  306. game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1;
  307. }
  308. if ((numTextures(TextureStorage::GAME) > 0)) {
  309. ImGui::SameLine();
  310. ImGui::Checkbox("Game##texgame", &game);
  311. } else {
  312. game = false;
  313. }
  314. if (index >= numTextures(game ? TextureStorage::GAME : TextureStorage::SYSTEM)) {
  315. index = numTextures(game ? TextureStorage::GAME : TextureStorage::SYSTEM) - 1;
  316. if (index < 0) {
  317. game = false;
  318. index = 0;
  319. }
  320. }
  321. auto bm = getBufferManager(index, game ? TextureStorage::GAME
  322. : TextureStorage::SYSTEM);
  323. ImGui::Image(bm, ImVec2(ImGui::GetColumnWidth() * 2 / 3, ImGui::GetColumnWidth() * 2 / 3));
  324. }
  325. if (ImGui::CollapsingHeader("Textile Viewer")) {
  326. if (numTiles() > 0) {
  327. static int index = 0;
  328. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  329. ImGui::SliderInt("##tileslide", &index, 0, numTiles() - 1);
  330. ImGui::PopItemWidth();
  331. ImGui::SameLine();
  332. if (ImGui::Button("+##tileplus", ImVec2(0, 0), true)) {
  333. if (index < (numTiles() - 1))
  334. index++;
  335. else
  336. index = 0;
  337. }
  338. ImGui::SameLine();
  339. if (ImGui::Button("-##tileminus", ImVec2(0, 0), true)) {
  340. if (index > 0)
  341. index--;
  342. else
  343. index = numTiles() - 1;
  344. }
  345. if (index >= numTiles())
  346. index = 0;
  347. auto& tile = getTile(index);
  348. auto bm = getBufferManager(tile.getTexture(), TextureStorage::GAME);
  349. ImVec2 size(ImGui::GetColumnWidth() * 2 / 3, ImGui::GetColumnWidth() * 2 / 3);
  350. auto uvA = tile.getUV(0);
  351. auto uvB = tile.getUV(2);
  352. ImVec2 uv1(uvA.x, uvA.y);
  353. ImVec2 uv2(uvB.x, uvB.y);
  354. ImGui::Image(bm, size, uv1, uv2);
  355. } else {
  356. ImGui::Text("No textiles are currently loaded...!");
  357. }
  358. }
  359. if (ImGui::CollapsingHeader("Animated Textile Viewer")) {
  360. if (numAnimatedTiles() > 0) {
  361. static int index = 0;
  362. static int tile = getFirstTileAnimation(index);
  363. if (ImGui::SliderInt("##animslide", &index, 0, numAnimatedTiles() - 1)) {
  364. tile = getFirstTileAnimation(index);
  365. }
  366. ImGui::SameLine();
  367. if (ImGui::Button("+##animplus", ImVec2(0, 0), true)) {
  368. if (index < (numAnimatedTiles() - 1))
  369. index++;
  370. else
  371. index = 0;
  372. tile = getFirstTileAnimation(index);
  373. }
  374. ImGui::SameLine();
  375. if (ImGui::Button("-##animminus", ImVec2(0, 0), true)) {
  376. if (index > 0)
  377. index--;
  378. else
  379. index = numAnimatedTiles() - 1;
  380. tile = getFirstTileAnimation(index);
  381. }
  382. if (index >= numAnimatedTiles()) {
  383. index = 0;
  384. tile = getFirstTileAnimation(index);
  385. }
  386. int next = getNextTileAnimation(index, tile);
  387. if (next == -1) {
  388. index = 0;
  389. tile = getFirstTileAnimation(index);
  390. }
  391. ImGui::SameLine();
  392. ImGui::Text("%d", tile);
  393. auto& t = getTile(tile);
  394. auto bm = getBufferManager(t.getTexture(), TextureStorage::GAME);
  395. ImVec2 size(ImGui::GetColumnWidth() * 2 / 3, ImGui::GetColumnWidth() * 2 / 3);
  396. auto uvA = t.getUV(0);
  397. auto uvB = t.getUV(2);
  398. ImVec2 uv1(uvA.x, uvA.y);
  399. ImVec2 uv2(uvB.x, uvB.y);
  400. ImGui::Image(bm, size, uv1, uv2);
  401. static int fr = 0;
  402. if (fr > 0) {
  403. fr--;
  404. } else {
  405. fr = RunTime::getFPS() / 5;
  406. tile = next;
  407. }
  408. } else {
  409. ImGui::Text("No animated textures are currently loaded...!");
  410. }
  411. }
  412. if (ImGui::CollapsingHeader("Sprite Sequence Viewer")) {
  413. if (getWorld().sizeSprite() <= 0) {
  414. ImGui::Text("Please load a level containing sprites!");
  415. } else {
  416. static int index = 0;
  417. static int sprite = 0;
  418. if (ImGui::SliderInt("##spriteslide", &index, 0, getWorld().sizeSpriteSequence() - 1)) {
  419. sprite = 0;
  420. }
  421. ImGui::SameLine();
  422. if (ImGui::Button("+##spriteplus", ImVec2(0, 0), true)) {
  423. if (index < (getWorld().sizeSpriteSequence() - 1))
  424. index++;
  425. else
  426. index = 0;
  427. sprite = 0;
  428. }
  429. ImGui::SameLine();
  430. if (ImGui::Button("-##spriteminus", ImVec2(0, 0), true)) {
  431. if (index > 0)
  432. index--;
  433. else
  434. index = getWorld().sizeSpriteSequence() - 1;
  435. sprite = 0;
  436. }
  437. if (index >= getWorld().sizeSpriteSequence()) {
  438. index = 0;
  439. sprite = 0;
  440. }
  441. if (sprite >= getWorld().getSpriteSequence(index).size()) {
  442. sprite = 0;
  443. }
  444. ImGui::SameLine();
  445. ImGui::Text("Sprite %d/%d", sprite + 1, getWorld().getSpriteSequence(index).size());
  446. auto& s = getWorld().getSprite(getWorld().getSpriteSequence(index).getStart() + sprite);
  447. auto bm = getBufferManager(s.getTexture(), TextureStorage::GAME);
  448. ImVec2 size(ImGui::GetColumnWidth() * 2 / 3, ImGui::GetColumnWidth() * 2 / 3);
  449. auto uv = s.getUVs();
  450. ImVec2 uv1(uv.x, uv.w);
  451. ImVec2 uv2(uv.z, uv.y);
  452. ImGui::Image(bm, size, uv1, uv2);
  453. static int fr = 0;
  454. if (fr > 0) {
  455. fr--;
  456. } else {
  457. fr = RunTime::getFPS() / 10;
  458. if (sprite < (getWorld().getSpriteSequence(index).size() - 1))
  459. sprite++;
  460. else
  461. sprite = 0;
  462. }
  463. }
  464. }
  465. }