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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "Console.h"
  14. #include "OpenRaider.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. TextureManager::TextureManager() {
  24. mFlags = 0;
  25. }
  26. TextureManager::~TextureManager() {
  27. reset();
  28. }
  29. void TextureManager::reset() {
  30. while (mTextureIds.size() > 0) {
  31. unsigned int id = mTextureIds.at(mTextureIds.size() - 1);
  32. glDeleteTextures(1, &id);
  33. mTextureIds.pop_back();
  34. }
  35. }
  36. int TextureManager::initialize() {
  37. unsigned char *image = generateColorTexture(WHITE, 32, 32, 32);
  38. loadBufferSlot(image, 32, 32, RGBA, 32, mTextureIds.size());
  39. delete [] image;
  40. //! \fixme Temporary
  41. std::string filename(getOpenRaider().getPakDir());
  42. filename += "/tr2/TITLE.PCX";
  43. if (loadPCX(filename.c_str()) < 0) {
  44. //! \fixme Error Checking. Return negative error code, check in calling place too
  45. filename = getOpenRaider().getDataDir();
  46. filename += "/splash.tga";
  47. loadTGA(filename.c_str());
  48. }
  49. return (mTextureIds.size() == 0) ? -1 : 0;
  50. }
  51. void TextureManager::setFlag(TextureFlag flag) {
  52. mFlags |= flag;
  53. }
  54. void TextureManager::clearFlag(TextureFlag flag) {
  55. mFlags &= ~flag;
  56. }
  57. int TextureManager::getTextureCount() {
  58. return mTextureIds.size();
  59. }
  60. void TextureManager::disableMultiTexture() {
  61. mFlags &= ~fUseMultiTexture;
  62. #ifdef MULTITEXTURE
  63. glDisable(GL_TEXTURE_2D);
  64. glActiveTextureARB(GL_TEXTURE0_ARB);
  65. #endif
  66. }
  67. void TextureManager::useMultiTexture(float aU, float aV, float bU, float bV) {
  68. if (!(mFlags & fUseMultiTexture))
  69. return;
  70. #ifdef MULTITEXTURE
  71. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, aU, aV);
  72. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
  73. #endif
  74. }
  75. void TextureManager::bindMultiTexture(int texture0, int texture1) {
  76. assert(texture0 >= 0);
  77. assert(texture1 >= 0);
  78. assert((unsigned int)texture0 < mTextureIds.size());
  79. assert((unsigned int)texture1 < mTextureIds.size());
  80. mFlags |= fUseMultiTexture;
  81. #ifdef MULTITEXTURE
  82. glActiveTextureARB(GL_TEXTURE0_ARB);
  83. glEnable(GL_TEXTURE_2D);
  84. glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture0));
  85. glActiveTextureARB(GL_TEXTURE1_ARB);
  86. glEnable(GL_TEXTURE_2D);
  87. glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture1));
  88. #endif
  89. }
  90. int TextureManager::loadBufferSlot(unsigned char *image,
  91. unsigned int width, unsigned int height,
  92. ColorMode mode, unsigned int bpp,
  93. unsigned int slot) {
  94. assert(image != NULL);
  95. assert(width > 0);
  96. assert(height > 0);
  97. assert((mode == GREYSCALE) || (mode == RGB)
  98. || (mode == BGR) || (mode == ARGB)
  99. || (mode == RGBA) || (mode == BGRA));
  100. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  101. while (mTextureIds.size() <= slot) {
  102. unsigned int id;
  103. glGenTextures(1, &id);
  104. mTextureIds.push_back(id);
  105. }
  106. unsigned int glcMode;
  107. switch (mode) {
  108. case GREYSCALE:
  109. glcMode = GL_LUMINANCE;
  110. break;
  111. case BGR:
  112. bgr2rgb24(image, width, height);
  113. glcMode = GL_RGB;
  114. break;
  115. case RGB:
  116. glcMode = GL_RGB;
  117. break;
  118. case ARGB:
  119. argb2rgba32(image, width, height);
  120. glcMode = GL_RGBA;
  121. break;
  122. case BGRA:
  123. bgra2rgba32(image, width, height);
  124. glcMode = GL_RGBA;
  125. break;
  126. case RGBA:
  127. glcMode = GL_RGBA;
  128. break;
  129. }
  130. glColor3ubv(WHITE);
  131. glClearColor(0.0, 0.0, 0.0, 0.0);
  132. glEnable(GL_DEPTH_TEST);
  133. glShadeModel(GL_SMOOTH);
  134. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  135. glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
  136. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  139. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  140. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  141. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  142. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  143. glTexImage2D(GL_TEXTURE_2D, 0, bpp / 8, width, height, 0, glcMode, GL_UNSIGNED_BYTE, image);
  144. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  145. return slot;
  146. }
  147. void TextureManager::bindTextureId(unsigned int n) {
  148. assert(n < mTextureIds.size());
  149. glEnable(GL_TEXTURE_2D);
  150. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  151. glBindTexture(GL_TEXTURE_2D, mTextureIds.at(n));
  152. }
  153. int TextureManager::loadImage(const char *filename) {
  154. if (stringEndsWith(filename, ".pcx") || stringEndsWith(filename, ".PCX")) {
  155. return loadPCX(filename);
  156. } else if (stringEndsWith(filename, ".png") || stringEndsWith(filename, ".PNG")) {
  157. return loadPNG(filename);
  158. } else if (stringEndsWith(filename, ".tga") || stringEndsWith(filename, ".TGA")) {
  159. return loadTGA(filename);
  160. } else {
  161. getConsole() << "No known image file type? (" << filename << ")" << Console::endl;
  162. }
  163. return -1;
  164. }
  165. int TextureManager::loadPCX(const char *filename) {
  166. assert(filename != NULL);
  167. assert(filename[0] != '\0');
  168. unsigned char *image;
  169. unsigned int w, h, bpp;
  170. ColorMode c;
  171. int id = -1;
  172. int error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
  173. if (error == 0) {
  174. unsigned char *image2 = scaleBuffer(image, &w, &h, bpp);
  175. if (image2) {
  176. delete [] image;
  177. image = image2;
  178. }
  179. id = loadBufferSlot(image, w, h, c, bpp, mTextureIds.size());
  180. delete [] image;
  181. }
  182. return id;
  183. }
  184. int TextureManager::loadPNG(const char *filename) {
  185. #ifdef USING_PNG
  186. assert(filename != NULL);
  187. assert(filename[0] != '\0');
  188. if (pngCheck(filename) != 0) {
  189. return -1;
  190. }
  191. unsigned char *image;
  192. unsigned int w, h, bpp;
  193. ColorMode c;
  194. int id = -1;
  195. int error = pngLoad(filename, &image, &w, &h, &c, &bpp);
  196. if (error == 0) {
  197. unsigned char *image2 = scaleBuffer(image, &w, &h, bpp);
  198. if (image2) {
  199. delete [] image;
  200. image = image2;
  201. }
  202. id = loadBufferSlot(image, w, h, c, bpp, mTextureIds.size());
  203. delete [] image;
  204. }
  205. return id;
  206. #else
  207. getConsole() << "No PNG support available (" << filename << ")" << Console::endl;
  208. return -1;
  209. #endif
  210. }
  211. int TextureManager::loadTGA(const char *filename) {
  212. assert(filename != NULL);
  213. assert(filename[0] != '\0');
  214. unsigned char *image;
  215. unsigned int w, h;
  216. char type;
  217. int id = -1;
  218. if (!tgaCheck(filename)) {
  219. tgaLoad(filename, &image, &w, &h, &type);
  220. unsigned char *image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
  221. if (image2) {
  222. delete [] image;
  223. image = image2;
  224. }
  225. if (image) {
  226. id = loadBufferSlot(image, w, h,
  227. (type == 2) ? RGBA : RGB,
  228. (type == 2) ? 32 : 24,
  229. mTextureIds.size());
  230. delete [] image;
  231. }
  232. }
  233. return id;
  234. }