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

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