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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "global.h"
  13. #include "Log.h"
  14. #include "RunTime.h"
  15. #include "utils/pcx.h"
  16. #include "utils/pixel.h"
  17. #include "utils/strings.h"
  18. #include "utils/tga.h"
  19. #include "TextureManager.h"
  20. #ifdef USING_PNG
  21. #include "utils/png.h"
  22. #endif
  23. TextureTileVertex::TextureTileVertex(uint8_t xc, uint8_t xp, uint8_t yc, uint8_t yp)
  24. : xCoordinate(xc), xPixel(xp), yCoordinate(yc), yPixel(yp) { }
  25. // ----------------------------------------------------------------------------
  26. TextureTile::~TextureTile() {
  27. while (!vertices.empty()) {
  28. delete vertices.at(vertices.size() - 1);
  29. vertices.pop_back();
  30. }
  31. }
  32. void TextureTile::add(TextureTileVertex* t) {
  33. vertices.push_back(t);
  34. }
  35. bool TextureTile::isTriangle() {
  36. assert(vertices.size() >= 3);
  37. if (vertices.size() == 3)
  38. return true;
  39. return ((vertices.at(3)->xPixel == 0)
  40. & (vertices.at(3)->xCoordinate == 0)
  41. & (vertices.at(3)->yPixel == 0)
  42. & (vertices.at(3)->yCoordinate == 0));
  43. }
  44. void TextureTile::display(float x, float y, float w, float h, float z) {
  45. getTextureManager().bindTextureId(texture);
  46. //! \fixme TR Rosetta Stone says this, but looks strange?
  47. /*
  48. if (attribute == 0) {
  49. // Ignore transparency
  50. glDisable(GL_BLEND);
  51. }
  52. */
  53. if (isTriangle())
  54. displayTriangle(x, y, w, h, z);
  55. else
  56. displayRectangle(x, y, w, h, z);
  57. /*
  58. if (attribute == 0) {
  59. glEnable(GL_BLEND);
  60. }
  61. */
  62. }
  63. void TextureTile::displayRectangle(float x, float y, float w, float h, float z) {
  64. float xmin = 256.0f, xmax = 0.0f;
  65. float ymin = 256.0f, ymax = 0.0f;
  66. for (int i = 0; i < 4; i++) {
  67. if (vertices.at(i)->xCoordinate == 255) {
  68. if (vertices.at(i)->xPixel > xmax)
  69. xmax = vertices.at(i)->xPixel;
  70. } else {
  71. if (vertices.at(i)->xPixel < xmin)
  72. xmin = vertices.at(i)->xPixel;
  73. }
  74. if (vertices.at(i)->yCoordinate == 255) {
  75. ymax = vertices.at(i)->yPixel;
  76. } else {
  77. ymin = vertices.at(i)->yPixel;
  78. }
  79. }
  80. glBegin(GL_QUADS);
  81. glTexCoord2f(xmin / 256.0f, ymin / 256.0f);
  82. glVertex3f(x, y, z);
  83. glTexCoord2f(xmax / 256.0f, ymin / 256.0f);
  84. glVertex3f(x + w, y, z);
  85. glTexCoord2f(xmax / 256.0f, ymax / 256.0f);
  86. glVertex3f(x + w, y + h, z);
  87. glTexCoord2f(xmin / 256.0f, ymax / 256.0f);
  88. glVertex3f(x, y + h, z);
  89. glEnd();
  90. }
  91. void TextureTile::displayTriangle(float x, float y, float w, float h, float z) {
  92. glBegin(GL_TRIANGLE_STRIP);
  93. for (int i = 0; i < 3; i++) {
  94. glTexCoord2f(vertices.at(i)->xPixel / 256.0f,
  95. vertices.at(i)->yPixel / 256.0f);
  96. if (vertices.at(i)->xCoordinate == 255) {
  97. if (vertices.at(i)->yCoordinate == 255) {
  98. glVertex3f(x + w, y + h, z);
  99. } else {
  100. glVertex3f(x + w, y, z);
  101. }
  102. } else {
  103. if (vertices.at(i)->yCoordinate == 255) {
  104. glVertex3f(x, y + h, z);
  105. } else {
  106. glVertex3f(x, y, z);
  107. }
  108. }
  109. }
  110. glEnd();
  111. }
  112. // ----------------------------------------------------------------------------
  113. TextureManager::~TextureManager() {
  114. while (mTextureIdsSystem.size() > 0) {
  115. unsigned int id = mTextureIdsSystem.at(mTextureIdsSystem.size() - 1);
  116. glDeleteTextures(1, &id);
  117. mTextureIdsSystem.pop_back();
  118. }
  119. clear();
  120. }
  121. void TextureManager::clear() {
  122. while (mTextureIdsGame.size() > 0) {
  123. unsigned int id = mTextureIdsGame.at(mTextureIdsGame.size() - 1);
  124. glDeleteTextures(1, &id);
  125. mTextureIdsGame.pop_back();
  126. }
  127. while (!tiles.empty()) {
  128. delete tiles.at(tiles.size() - 1);
  129. tiles.pop_back();
  130. }
  131. while (!animations.empty())
  132. animations.pop_back();
  133. }
  134. void TextureManager::addTile(TextureTile* t) {
  135. tiles.push_back(t);
  136. }
  137. int TextureManager::numTiles() {
  138. return tiles.size();
  139. }
  140. TextureTile& TextureManager::getTile(int index) {
  141. assert(index >= 0);
  142. assert(index < tiles.size());
  143. return *tiles.at(index);
  144. }
  145. void TextureManager::addAnimatedTile(int index, int tile) {
  146. while (index >= animations.size())
  147. animations.push_back(std::vector<int>());
  148. animations.at(index).push_back(tile);
  149. }
  150. int TextureManager::numAnimatedTiles() {
  151. return animations.size();
  152. }
  153. int TextureManager::getFirstTileAnimation(int index) {
  154. assert(index < animations.size());
  155. assert(animations.at(index).size() > 0);
  156. return animations.at(index).at(0);
  157. }
  158. int TextureManager::getNextTileAnimation(int tile) {
  159. for (int a = 0; a < animations.size(); a++) {
  160. for (int i = 0; i < animations.at(a).size(); i++) {
  161. if (animations.at(a).at(i) == tile) {
  162. if (i < (animations.at(a).size() - 1))
  163. return animations.at(a).at(i + 1);
  164. else
  165. return animations.at(a).at(0);
  166. }
  167. }
  168. }
  169. return -1;
  170. }
  171. std::vector<unsigned int>& TextureManager::getIds(TextureStorage s) {
  172. if (s == TextureStorage::GAME)
  173. return mTextureIdsGame;
  174. else
  175. return mTextureIdsSystem;
  176. }
  177. int TextureManager::initialize() {
  178. unsigned char* image = generateColorTexture(WHITE, 32, 32, 32);
  179. int res = loadBufferSlot(image, 32, 32, RGBA, 32, TextureStorage::SYSTEM, TEXTURE_WHITE);
  180. delete [] image;
  181. if (res < 0) {
  182. return -1;
  183. }
  184. //! \fixme Temporary?
  185. std::string filename = getRunTime().getPakDir() + "/tr2/TITLE.PCX";
  186. if (loadPCX(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  187. filename = getRunTime().getDataDir() + "/splash.tga";
  188. if (loadTGA(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
  189. return -2;
  190. }
  191. }
  192. return 0;
  193. }
  194. int TextureManager::loadBufferSlot(unsigned char* image,
  195. unsigned int width, unsigned int height,
  196. ColorMode mode, unsigned int bpp,
  197. TextureStorage s, int slot, bool filter) {
  198. assert(image != NULL);
  199. assert(width > 0);
  200. assert(height > 0);
  201. assert((mode == GREYSCALE) || (mode == RGB)
  202. || (mode == BGR) || (mode == ARGB)
  203. || (mode == RGBA) || (mode == BGRA));
  204. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  205. if (slot == -1)
  206. slot = getIds(s).size();
  207. while (getIds(s).size() <= slot) {
  208. unsigned int id;
  209. glGenTextures(1, &id);
  210. getIds(s).push_back(id);
  211. }
  212. unsigned int glcMode;
  213. switch (mode) {
  214. case GREYSCALE:
  215. glcMode = GL_LUMINANCE;
  216. break;
  217. case BGR:
  218. bgr2rgb24(image, width, height);
  219. glcMode = GL_RGB;
  220. break;
  221. case RGB:
  222. glcMode = GL_RGB;
  223. break;
  224. case ARGB:
  225. argb2rgba32(image, width, height);
  226. glcMode = GL_RGBA;
  227. break;
  228. case BGRA:
  229. bgra2rgba32(image, width, height);
  230. glcMode = GL_RGBA;
  231. break;
  232. case RGBA:
  233. glcMode = GL_RGBA;
  234. break;
  235. }
  236. glColor3ubv(WHITE);
  237. glEnable(GL_DEPTH_TEST);
  238. glShadeModel(GL_SMOOTH);
  239. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  240. glBindTexture(GL_TEXTURE_2D, getIds(s).at(slot));
  241. if (filter) {
  242. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  243. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  244. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  245. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  246. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  247. } else {
  248. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  249. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  250. }
  251. glTexImage2D(GL_TEXTURE_2D, 0, bpp / 8, width, height, 0, glcMode, GL_UNSIGNED_BYTE, image);
  252. return slot;
  253. }
  254. int TextureManager::numTextures(TextureStorage s) {
  255. return getIds(s).size();
  256. }
  257. void TextureManager::bindTextureId(unsigned int n, TextureStorage s) {
  258. assert(n < getIds(s).size());
  259. glEnable(GL_TEXTURE_2D);
  260. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  261. glBindTexture(GL_TEXTURE_2D, getIds(s).at(n));
  262. }
  263. int TextureManager::loadImage(const char* filename, TextureStorage s, int slot) {
  264. if (stringEndsWith(filename, ".pcx") || stringEndsWith(filename, ".PCX")) {
  265. return loadPCX(filename, s, slot);
  266. } else if (stringEndsWith(filename, ".png") || stringEndsWith(filename, ".PNG")) {
  267. return loadPNG(filename, s, slot);
  268. } else if (stringEndsWith(filename, ".tga") || stringEndsWith(filename, ".TGA")) {
  269. return loadTGA(filename, s, slot);
  270. } else {
  271. getLog() << "No known image file type? (" << filename << ")" << Log::endl;
  272. }
  273. return -1;
  274. }
  275. int TextureManager::loadPCX(const char* filename, TextureStorage s, int slot) {
  276. assert(filename != NULL);
  277. assert(filename[0] != '\0');
  278. unsigned char* image;
  279. unsigned int w, h, bpp;
  280. ColorMode c;
  281. int id = -1;
  282. int error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
  283. if (error == 0) {
  284. unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
  285. if (image2) {
  286. delete [] image;
  287. image = image2;
  288. }
  289. id = loadBufferSlot(image, w, h, c, bpp, s, slot);
  290. delete [] image;
  291. }
  292. return id;
  293. }
  294. int TextureManager::loadPNG(const char* filename, TextureStorage s, int slot) {
  295. #ifdef USING_PNG
  296. assert(filename != NULL);
  297. assert(filename[0] != '\0');
  298. if (pngCheck(filename) != 0) {
  299. return -1;
  300. }
  301. unsigned char* image;
  302. unsigned int w, h, bpp;
  303. ColorMode c;
  304. int id = -1;
  305. int error = pngLoad(filename, &image, &w, &h, &c, &bpp);
  306. if (error == 0) {
  307. unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
  308. if (image2) {
  309. delete [] image;
  310. image = image2;
  311. }
  312. id = loadBufferSlot(image, w, h, c, bpp, s, slot);
  313. delete [] image;
  314. }
  315. return id;
  316. #else
  317. getLog() << "No PNG support available (" << filename << ")" << Log::endl;
  318. return -1;
  319. #endif
  320. }
  321. int TextureManager::loadTGA(const char* filename, TextureStorage s, int slot) {
  322. assert(filename != NULL);
  323. assert(filename[0] != '\0');
  324. unsigned char* image;
  325. unsigned int w, h;
  326. char type;
  327. int id = -1;
  328. if (!tgaCheck(filename)) {
  329. tgaLoad(filename, &image, &w, &h, &type);
  330. unsigned char* image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
  331. if (image2) {
  332. delete [] image;
  333. image = image2;
  334. }
  335. if (image) {
  336. id = loadBufferSlot(image, w, h,
  337. (type == 2) ? RGBA : RGB,
  338. (type == 2) ? 32 : 24,
  339. s, slot);
  340. delete [] image;
  341. }
  342. }
  343. return id;
  344. }