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

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